mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
增加配置文件驱动接口,以便用户在遵循接口的前提下定义并使用自己的驱动,例如 YAML
This commit is contained in:
@@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
|
use think\config\driver\DriverInterface;
|
||||||
|
use think\config\driver\Ini;
|
||||||
|
|
||||||
class Config
|
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)) {
|
if ($driver === null) {
|
||||||
$type = substr(strrchr($config, '.'), 1);
|
$driver = new Ini();
|
||||||
}
|
}
|
||||||
|
|
||||||
$range = $range ? $range : self::$range;
|
$range = $range ? $range : self::$range;
|
||||||
$class = '\\think\\config\\driver\\' . strtolower($type);
|
self::set($driver->parse($config), '', $range);
|
||||||
self::set((new $class())->parse($config), '', $range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载配置文件
|
// 加载配置文件
|
||||||
|
|||||||
29
library/think/config/driver/DriverInterface.php
Normal file
29
library/think/config/driver/DriverInterface.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: dongww <dongww@gmail.com>
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace think\config\driver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件解析驱动接口
|
||||||
|
*
|
||||||
|
* @package think\config\driver
|
||||||
|
*/
|
||||||
|
interface DriverInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 解析配置文件或内容,并以字符串形式返回
|
||||||
|
*
|
||||||
|
* @param string $config 配置文件路径或字符串
|
||||||
|
*
|
||||||
|
* @return array 以数组形式返回配置
|
||||||
|
*/
|
||||||
|
public function parse($config);
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace think\config\driver;
|
namespace think\config\driver;
|
||||||
|
|
||||||
class Ini
|
class Ini implements DriverInterface
|
||||||
{
|
{
|
||||||
public function parse($config)
|
public function parse($config)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace think\config\driver;
|
namespace think\config\driver;
|
||||||
|
|
||||||
class Xml
|
class Xml implements DriverInterface
|
||||||
{
|
{
|
||||||
public function parse($config)
|
public function parse($config)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user