From 9131fd718253cca7c7ec6db19f6cd3c12e72b49c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=9F=8E?= Date: Tue, 8 Oct 2019 14:05:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- src/build/Base.php | 359 +++++++++++++++++++++++-------------------- tests/ConfigTest.php | 8 +- 3 files changed, 201 insertions(+), 168 deletions(-) diff --git a/composer.json b/composer.json index 8f80876..0a6fcaf 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "pfinal-config" ], "require": { - "php": ">=5.4.0" + "php": ">=7.0" }, "autoload": { "psr-4": { diff --git a/src/build/Base.php b/src/build/Base.php index 71d7fc4..da052c1 100644 --- a/src/build/Base.php +++ b/src/build/Base.php @@ -1,167 +1,194 @@ _/___.' >'"". - * | | : `- \`.;`\ _ /`;.`/ - ` : | | - * \ \ `-. \_ __\ /__ _/ .-` / / - * ========`-.____`-.___\_____/___.-`____.-'======== - * `=---=' - * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - * 佛祖保佑 永无BUG 永不修改 - * - */ - -namespace pf\config\build; - -class Base -{ - protected static $items = []; - protected static $env = []; - - public function batch(array $config) - { - foreach ($config as $k => $v) { - $this->set($k, $v); - } - return true; - } - - public function set($key, $name) - { - $tmp = &self::$items; - $config = explode('.', $key); - foreach ((array)$config as $d) { - if (!isset($tmp[$d])) { - $tmp[$d] = []; - } - $tmp = &$tmp[$d]; - } - $tmp = $name; - return true; - } - - /** - * 环境所需配置 - * @param string $file - * @return $this - */ - public function env($file = '.env') - { - //var_dump($file);exit; - if (is_file($file)) { - $content = file_get_contents($file); - preg_match_all('/(.+?)=(.+)/', $content, $env, PREG_SET_ORDER); - if ($env) { - foreach ($env as $e) { - self::$env[$e[1]] = $e[2]; - } - } - } else { - die("The configuration file is missing. env, please see if there is a configuration file. Refer to the. env_example file for the content of the configuration file.\n"); - } - return $this; - } - - /** - * 加载所有的自定义配置文件 - * @param $dir - */ - public function loadFiles($dir) - { - //var_dump($dir);exit(); - foreach (glob($dir . '/*') as $f) { - $info = pathinfo($f); - $this->set($info['filename'], include $f); - } - } - - /** - * 所有的自定义配置文件 - * @return array - */ - public function all() - { - return self::$items; - } - - /** - * 获取配置项 - * @param $name - * @return mixed - */ - public static function getEnv($name) - { - if (isset(self::$env[$name])) { - return self::$env[$name]; - } else { - die("This configuration item does not exist\n"); - } - - } - - /** - * 获取自定义配置的内容 - * @param $key - * @param null $default - * @return array|mixed|null - */ - public function get($key, $default = null) - { - $tmp = self::$items; - $config = explode('.', trim($key, '.')); - if (count($config) > 0) { - foreach ((array)$config as $d) { - if (isset($tmp[$d])) { - $tmp = $tmp[$d]; - } else { - return $default; - } - } - } - return $tmp; - } - - /** - * 判断配置项是否存在 - * @param $key - * @return bool - */ - public function has($key) - { - $tmp = self::$items; - $config = explode('.', trim($key, '.')); - if (count($config) > 0) { - foreach ((array)$config as $d) { - if (isset($tmp[$d])) { - $tmp = $tmp[$d]; - } else { - return false; - } - } - } - return true; - } - public function setItems($items) - { - return self::$items = $items; - } - -} + /** + * Created by PhpStorm. + * User: 南丞 + * Date: 2019/2/12 + * Time: 13:01 + * + * + * _ooOoo_ + * o8888888o + * 88" . "88 + * (| ^_^ |) + * O\ = /O + * ____/`---'\____ + * .' \\| |// `. + * / \\||| : |||// \ + * / _||||| -:- |||||- \ + * | | \\\ - /// | | + * | \_| ''\---/'' | | + * \ .-\__ `-` ___/-. / + * ___`. .' /--.--\ `. . ___ + * ."" '< `.___\_<|>_/___.' >'"". + * | | : `- \`.;`\ _ /`;.`/ - ` : | | + * \ \ `-. \_ __\ /__ _/ .-` / / + * ========`-.____`-.___\_____/___.-`____.-'======== + * `=---=' + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * 佛祖保佑 永无BUG 永不修改 + * + */ + + namespace pf\config\build; + + class Base + { + protected static $items = []; + protected static $env = []; + + public function batch(array $config) + { + foreach ($config as $k => $v) { + $this->set($k, $v); + } + + return true; + } + + public function set($key, $name) + { + $tmp = &self::$items; + $config = explode('.', $key); + foreach ((array)$config as $d) { + if (!isset($tmp[$d])) { + $tmp[$d] = []; + } + $tmp = &$tmp[$d]; + } + $tmp = $name; + + return true; + } + + /** + * 环境所需配置 + * @param string $file + * @return $this + */ + public function env($file = '.env') + { + //var_dump($file);exit; + if (is_file($file)) { + $content = file_get_contents($file); + preg_match_all('/(.+?)=(.+)/', $content, $env, PREG_SET_ORDER); + if ($env) { + foreach ($env as $e) { + self::$env[$e[1]] = $e[2]; + } + } + } else { + die("The configuration file is missing. env, please see if there is a configuration file. Refer to the. env_example file for the content of the configuration file.\n"); + } + + return $this; + } + + /** + * 加载所有的自定义配置文件 + * @param $dir + */ + public function loadFiles($dir) + { + //var_dump($dir);exit(); + foreach (glob($dir.'/*') as $f) { + $info = pathinfo($f); + $this->set($info['filename'], include $f); + } + } + + /** + * 所有的自定义配置文件 + * @return array + */ + public function all() + { + return self::$items; + } + + /** + * 获取配置项 + * @param $name + * @return mixed + */ + public static function getEnv($name) + { + if (isset(self::$env[$name])) { + return self::$env[$name]; + } else { + die("This configuration item does not exist\n"); + } + + } + + /** + * 获取自定义配置的内容 + * @param $key + * @param null $default + * @return array|mixed|null + */ + public function get($key, $default = null) + { + $tmp = self::$items; + $config = explode('.', trim($key, '.')); + if (count($config) > 0) { + foreach ((array)$config as $d) { + if (isset($tmp[$d])) { + $tmp = $tmp[$d]; + } else { + return $default; + } + } + } + + return $tmp; + } + + /** + * 排队字段获取数据 + * + * @param string $key 获取键名 + * @param array $extName 排除的字段 + * + * @return array + */ + public function getExtName($key, array $extName) + { + $config = $this->get($key); + $data = []; + foreach ((array)$config as $k => $v) { + if (!in_array($k, $extName)) { + $data[$k] = $v; + } + } + + return $data; + } + + /** + * 判断配置项是否存在 + * @param $key + * @return bool + */ + public function has($key) + { + $tmp = self::$items; + $config = explode('.', trim($key, '.')); + if (count($config) > 0) { + foreach ((array)$config as $d) { + if (isset($tmp[$d])) { + $tmp = $tmp[$d]; + } else { + return false; + } + } + } + + return true; + } + + public function setItems($items) + { + return self::$items = $items; + } + + } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 7e85bbf..91783d2 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -55,7 +55,13 @@ public function testGet() $this->assertInternalType('string', Config::get('app.app_name')); $this->assertNull(Config::get('app.debug')); } - + + public function testGetExtName() + { + $config = Config::getExtName('database', ['write', 'read']); + $this->assertNotTrue(isset($config['write'])); + } + public function testHas() { $this->assertFalse(Config::has('app.debug'));