Skip to content

Commit

Permalink
Merge pull request #4 from YunYinORG/develop
Browse files Browse the repository at this point in the history
优化配置,添加日志和文件存储单元测试,php5.3断言屏蔽
  • Loading branch information
NewFuture authored Aug 21, 2016
2 parents ce170d8 + e24a59d commit 780ada8
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ before_script:
script: phpunit --coverage-text --configuration tests/phpunit.xml

# configure notifications (email, IRC, campfire etc)
notifications:
email: false
# notifications:
# email: false
2 changes: 1 addition & 1 deletion conf/app.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[common];公用配置
version = 2.4.3
version = 2.4.4
application.directory = APP_PATH "/app/"
application.library = APP_PATH "/library"
application.num_param = 'id' ;id形默认绑定参数 如 /User/123 =>绑定参数$id值未123
Expand Down
11 changes: 10 additions & 1 deletion library/Bootstrap/dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ public function _initAssert()
if (version_compare(PHP_VERSION, '7.0.0', '>=')) { //for php7

//判断环境
(-1 == ini_get('zend.assertions')) and exit("调试环境,请开启php7的断言,以便更早发现问题!\n<br>(<u>在php.ini 中的设置 zend.assertions = 1 开启断言【推荐】</u>;或者在 conf/app.ini 中设置 assert.active = 0 关闭此警告【不推荐】。)\n<br>In development environment, please open assertion for php7 to debug ! \n<br> (set 'zend.assertions = 1' in [php.ini][recommended]; or set 'assert.active = 0' in [conf/app.ini] to ignore this [not recommender].)");
if (-1 == ini_get('zend.assertions')) {
exit("调试环境,请开启php7的断言,以便更早发现问题!\n<br>(<u>在php.ini 中的设置 zend.assertions = 1 开启断言【推荐】</u>;或者在 conf/app.ini 中设置 assert.active = 0 关闭此警告【不推荐】。)\n<br>In development environment, please open assertion for php7 to debug ! \n<br> (set 'zend.assertions = 1' in [php.ini][recommended]; or set 'assert.active = 0' in [conf/app.ini] to ignore this [not recommender].)");
}
//PHP7配置
ini_set('zend.assertions', 1);//开启断言
ini_set('assert.exception', 0);//关闭异常
} elseif (version_compare(PHP_VERSION, '5.4.8', '<')) {
//低版本(php5.3)关闭断言
assert_options(ASSERT_QUIET_EVAL, true);
assert_options(ASSERT_WARNING, false);
assert_options(ASSERT_BAIL, false);
assert_options(ASSERT_ACTIVE, false);
return;
}

assert_options(ASSERT_ACTIVE, true);
Expand Down
22 changes: 9 additions & 13 deletions library/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Config
{
private static $_config = null;
private static $_secret = null;

/**
* 获取配置
Expand All @@ -17,34 +18,29 @@ class Config
*/
public static function get($key = null, $default = null)
{
if (null===($value = self::getConfig()->get($key))) {
return $default;
if (!$config=&Config::$_config) {
$config=Yaf_Application::app()->getConfig();
}
return is_object($value) ? $value->toArray():$value;
$value = $config -> get($key);
return null===$value ? $default : $value;
}

/**
* 获取私密配置
* @method secret
* @param [string] $name [配置名]
* @param [string] $key [键值]
* @return [midex] [description]
* @return [mixed] [结果]
* @author NewFuture
* @example
* Config::getSecrect('encrypt') 获取取私密配置中的encrypt所有配置
* Config::getSecrect('encrypt','key') 获取取私密配置中的encrypt配置的secret值
*/
public static function getSecret($name = '', $key = null)
{
if ($path = self::getConfig()->get('secret_config_path')) {
$secretConfig = new Yaf_Config_Ini($path, $name);
return $key ? $secretConfig->get($key) : $secretConfig->toArray();
if (!$secret=&Config::$_secret) {
$secret = new Yaf_Config_Ini(Config::get('secret_config_path'));
}
}

/*获取配置*/
private static function getConfig()
{
return self::$_config ?: (self::$_config = Yaf_Application::app()->getConfig());
return $key?$secret->get($name)->get($key):$secret->get($name);
}
}
14 changes: 13 additions & 1 deletion library/Logger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Storage\File as File;

/**
* 日志记录
* 遵循 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
Expand Down Expand Up @@ -50,7 +52,7 @@ public static function write($msg, $level = 'NOTICE')

if (!$config=&Logger::$_conf) {
//读取配置信息
$config=Config::get('log');
$config=Config::get('log')->toArray();
$config['type'] = strtolower($config['type']);
$config['allow'] = explode(',', strtoupper($config['allow']));
}
Expand All @@ -70,6 +72,16 @@ public static function write($msg, $level = 'NOTICE')
}
}

/**
* 清空日志(仅对文件模式有效)
*/
public static function clear()
{
if ('file'=== Config::get('log.type')) {
File::cleanDir(Config::get('runtime').DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR);
}
}

/**
* 获取写入流
* @method getFile
Expand Down
28 changes: 19 additions & 9 deletions library/Storage/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function set($name, $value)
$cache = array('e' => $expire, 'c' => $value);
$value = serialize($cache);
}

assert('is_scalar($value)', '保存的数据应该是基本类型');
$filename = $this->_dir . $name . '.php';
return file_put_contents($filename, $value)&&chmod($filename, File::$mode);
}
Expand Down Expand Up @@ -84,14 +84,7 @@ public function delete($name)
*/
public function flush()
{
$dir = $this->_dir;
/*获取全部文件*/
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
foreach ($files as $f) {
@unlink($dir . $f);
}
File::cleanDir($this->_dir);
}

/**
Expand All @@ -109,4 +102,21 @@ public function __construct($dir, $serialized = false)
$this->_dir = $dir.DIRECTORY_SEPARATOR;
$this->_serialized = $serialized;
}

/**
* 清空目录
* @param [type] $dir [存储目录]
* @author NewFuture
*/
public static function cleanDir($dir)
{
/*获取全部文件*/
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
foreach ($files as &$f) {
@unlink($dir . $f);
}
unset($files);
}
}
7 changes: 7 additions & 0 deletions tests/ini/yaf.dev.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
assert.active = 1
assert.bail = 1
assert.warning = 1
assert.quiet_eval = 0
assert.exception = 1
zend.assertions = 1

extension=yaf.so
[yaf]
yaf.environ=dev
Expand Down
8 changes: 7 additions & 1 deletion tests/ini/yaf.product.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
extension=yaf.so
assert.active = 0
assert.bail = 0
assert.warning = 0
assert.quiet_eval = 1
assert.exception = 0
zend.assertions = -1

extension=yaf.so
[yaf]
yaf.environ=product
yaf.cache_config=1
Expand Down
81 changes: 81 additions & 0 deletions tests/library/Storage/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace tests\library\Storage;

use \Storage\File as File;
use \Yaf_Application as Application;
use \PHPUnit_Framework_TestCase as TestCase;

class FileTest extends TestCase
{
const TEST_STRING='yyf file storage test';
protected static $env;
protected static $dir;
protected static $file;

public static function setUpBeforeClass()
{
static::$dir=APP_PATH.DIRECTORY_SEPARATOR.'runtime'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR;
static::$file=new File(static::$dir);
static::$env= Application::app()->environ();
}

/**
* @requires OS Linux
*/
public function testDirMode()
{
$mode=('dev'===static::$env)?File::$mode:0700;
clearstatcache();
$this->assertSame(fileperms(static::$dir)&$mode, $mode);
}

public function testSet()
{
$name=uniqid('test_set', true);
static::$file->set($name, FileTest::TEST_STRING);
$filename=static::$dir.$name.'.php';
$this->assertFileExists($filename);
clearstatcache();
$this->assertSame(fileperms($filename)&File::$mode, File::$mode);
$this->assertStringEqualsFile($filename, FileTest::TEST_STRING);
return $name;
}

/**
* @depends testSet
*/
public function testGet($name)
{
$str=static::$file->get($name);
$this->assertSame($str, FileTest::TEST_STRING);
$this->assertSame(static::$file->get(uniqid('_rand_', true)), null);
}

/**
* @depends testSet
*/
public function testDelete($name)
{
static::$file->delete($name);
$this->assertFileNotExists(static::$dir.$name.'.php');
$this->assertSame(static::$file->get($name), null);
}

/**
* @depends testDelete
*/
public function testFlush()
{
for ($i=0; $i < 10; $i++) {
static::$file->set('test_'.uniqid(rand(1000, 10000)), rand());
}
static::$file->flush();
$this->assertCount(2, scandir(static::$dir));
}

public static function tearDownAfterClass()
{
File::cleanDir(static::$dir);
rmdir(static::$dir);
}
}

0 comments on commit 780ada8

Please sign in to comment.