From 093023cf13452b2a02030ddc547bb1d9b5e233eb Mon Sep 17 00:00:00 2001 From: dongww Date: Wed, 16 Dec 2015 18:36:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=A9=B1=E5=8A=A8=E6=8E=A5=E5=8F=A3=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E4=BE=BF=E7=94=A8=E6=88=B7=E5=9C=A8=E9=81=B5=E5=BE=AA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E5=89=8D=E6=8F=90=E4=B8=8B=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=B9=B6=E4=BD=BF=E7=94=A8=E8=87=AA=E5=B7=B1=E7=9A=84=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8=EF=BC=8C=E4=BE=8B=E5=A6=82=20YAML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/config.php | 21 ++++++++++---- .../think/config/driver/DriverInterface.php | 29 +++++++++++++++++++ library/think/config/driver/ini.php | 2 +- library/think/config/driver/xml.php | 2 +- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 library/think/config/driver/DriverInterface.php diff --git a/library/think/config.php b/library/think/config.php index 092d9478..16ae684f 100644 --- a/library/think/config.php +++ b/library/think/config.php @@ -11,6 +11,9 @@ namespace think; +use think\config\driver\DriverInterface; +use think\config\driver\Ini; + class Config { // 配置参数 @@ -27,15 +30,21 @@ class Config } } - // 解析其他格式的配置参数 - public static function parse($config, $type = '', $range = '') + /** + * 解析配置文件或内容 + * + * @param string $config 配置文件路径或内容 + * @param string $range 作用域 + * @param DriverInterface|null $driver 配置解析驱动 + */ + public static function parse($config, $range = '', DriverInterface $driver = null) { - if (empty($type)) { - $type = substr(strrchr($config, '.'), 1); + if ($driver === null) { + $driver = new Ini(); } + $range = $range ? $range : self::$range; - $class = '\\think\\config\\driver\\' . strtolower($type); - self::set((new $class())->parse($config), '', $range); + self::set($driver->parse($config), '', $range); } // 加载配置文件 diff --git a/library/think/config/driver/DriverInterface.php b/library/think/config/driver/DriverInterface.php new file mode 100644 index 00000000..1147ee82 --- /dev/null +++ b/library/think/config/driver/DriverInterface.php @@ -0,0 +1,29 @@ + +// +---------------------------------------------------------------------- + +namespace think\config\driver; + +/** + * 配置文件解析驱动接口 + * + * @package think\config\driver + */ +interface DriverInterface +{ + /** + * 解析配置文件或内容,并以字符串形式返回 + * + * @param string $config 配置文件路径或字符串 + * + * @return array 以数组形式返回配置 + */ + public function parse($config); +} diff --git a/library/think/config/driver/ini.php b/library/think/config/driver/ini.php index 44324dee..f5caf6d3 100644 --- a/library/think/config/driver/ini.php +++ b/library/think/config/driver/ini.php @@ -11,7 +11,7 @@ namespace think\config\driver; -class Ini +class Ini implements DriverInterface { public function parse($config) { diff --git a/library/think/config/driver/xml.php b/library/think/config/driver/xml.php index a0ad8402..9c060b18 100644 --- a/library/think/config/driver/xml.php +++ b/library/think/config/driver/xml.php @@ -11,7 +11,7 @@ namespace think\config\driver; -class Xml +class Xml implements DriverInterface { public function parse($config) {