Skip to content

Commit

Permalink
更新方法
Browse files Browse the repository at this point in the history
  • Loading branch information
pfinal-nc committed Oct 8, 2019
1 parent 33c49f9 commit 9131fd7
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 168 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"pfinal-config"
],
"require": {
"php": ">=5.4.0"
"php": ">=7.0"
},
"autoload": {
"psr-4": {
Expand Down
359 changes: 193 additions & 166 deletions src/build/Base.php
Original file line number Diff line number Diff line change
@@ -1,167 +1,194 @@
<?php
/**
* 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 $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;
}

}
8 changes: 7 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit 9131fd7

Please sign in to comment.