From e24a59d5156981b5c621f6a89adf3af2fcd0c893 Mon Sep 17 00:00:00 2001 From: NewFuture Date: Sat, 20 Aug 2016 22:54:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E6=B8=85=E7=A9=BA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit config 兼容和文件存储单元测试 文件存储单元测试 文件存储单元测试 fixed文件存储单元测试 aessert 断言 php5.3屏蔽 update dev and conf --- .travis.yml | 4 +- conf/app.ini | 2 +- library/Bootstrap/dev.php | 11 +++- library/Config.php | 22 ++++---- library/Logger.php | 14 +++++- library/Storage/File.php | 28 +++++++---- tests/ini/yaf.dev.ini | 7 +++ tests/ini/yaf.product.ini | 8 ++- tests/library/Storage/FileTest.php | 81 ++++++++++++++++++++++++++++++ 9 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 tests/library/Storage/FileTest.php diff --git a/.travis.yml b/.travis.yml index 8505765..eadf551 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/conf/app.ini b/conf/app.ini index 55f1043..809a734 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -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 diff --git a/library/Bootstrap/dev.php b/library/Bootstrap/dev.php index 729fe1e..d94bf76 100644 --- a/library/Bootstrap/dev.php +++ b/library/Bootstrap/dev.php @@ -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
(在php.ini 中的设置 zend.assertions = 1 开启断言【推荐】;或者在 conf/app.ini 中设置 assert.active = 0 关闭此警告【不推荐】。)\n
In development environment, please open assertion for php7 to debug ! \n
(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
(在php.ini 中的设置 zend.assertions = 1 开启断言【推荐】;或者在 conf/app.ini 中设置 assert.active = 0 关闭此警告【不推荐】。)\n
In development environment, please open assertion for php7 to debug ! \n
(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); diff --git a/library/Config.php b/library/Config.php index 99beb1a..fa32eae 100644 --- a/library/Config.php +++ b/library/Config.php @@ -6,6 +6,7 @@ class Config { private static $_config = null; + private static $_secret = null; /** * 获取配置 @@ -17,10 +18,11 @@ 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; } /** @@ -28,7 +30,7 @@ public static function get($key = null, $default = null) * @method secret * @param [string] $name [配置名] * @param [string] $key [键值] - * @return [midex] [description] + * @return [mixed] [结果] * @author NewFuture * @example * Config::getSecrect('encrypt') 获取取私密配置中的encrypt所有配置 @@ -36,15 +38,9 @@ public static function get($key = null, $default = null) */ 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); } } diff --git a/library/Logger.php b/library/Logger.php index b829547..bba6c8b 100644 --- a/library/Logger.php +++ b/library/Logger.php @@ -1,4 +1,6 @@ toArray(); $config['type'] = strtolower($config['type']); $config['allow'] = explode(',', strtoupper($config['allow'])); } @@ -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 diff --git a/library/Storage/File.php b/library/Storage/File.php index da73326..2d9e64f 100644 --- a/library/Storage/File.php +++ b/library/Storage/File.php @@ -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); } @@ -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); } /** @@ -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); + } } diff --git a/tests/ini/yaf.dev.ini b/tests/ini/yaf.dev.ini index 0fbc13b..6b9425c 100755 --- a/tests/ini/yaf.dev.ini +++ b/tests/ini/yaf.dev.ini @@ -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 diff --git a/tests/ini/yaf.product.ini b/tests/ini/yaf.product.ini index b658493..b226529 100755 --- a/tests/ini/yaf.product.ini +++ b/tests/ini/yaf.product.ini @@ -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 diff --git a/tests/library/Storage/FileTest.php b/tests/library/Storage/FileTest.php new file mode 100644 index 0000000..1c0fb14 --- /dev/null +++ b/tests/library/Storage/FileTest.php @@ -0,0 +1,81 @@ +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); + } +}