Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
pfinal-nc committed Feb 13, 2019
1 parent ba6a105 commit 4bca3ed
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APP_NAME="PFINAL 0.1BT"

DB_DRIVER=mysql
DB_HOST=127.0.0.1
DB_DATABASE=pfinal
DB_USER=root
DB_PASSWORD=

SESSION_DRIVER=file

HTTP_REWRITE=0
11 changes: 11 additions & 0 deletions .env_examplde
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APP_NAME="PFINAL 0.1BT"

DB_DRIVER=mysql
DB_HOST=127.0.0.1
DB_DATABASE=pfinal
DB_USER=root
DB_PASSWORD=

SESSION_DRIVER=file

HTTP_REWRITE=0
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/vendor
composer.lock
/attachment
/tests
.env
34 changes: 34 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: 运营部
* Date: 2019/2/13
* Time: 11:32
*
*
* _ooOoo_
* o8888888o
* 88" . "88
* (| ^_^ |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . ___
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ========`-.____`-.___\_____/___.-`____.-'========
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永无BUG 永不修改
*
*/

return [
'app_name'=>'pfinal_club'
];
107 changes: 107 additions & 0 deletions src/build/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,111 @@ public function set($key, $name)
$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;
}

}
64 changes: 64 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use pf\config\Config;

/**
* Created by PhpStorm.
* User: 运营部
* Date: 2019/2/12
* Time: 13:11
*
*
* _ooOoo_
* o8888888o
* 88" . "88
* (| ^_^ |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . ___
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ========`-.____`-.___\_____/___.-`____.-'========
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永无BUG 永不修改
*
*/
class ConfigTest extends \PHPUnit\Framework\TestCase
{
public function setUp()
{
parent::setUp();
Config::env(dirname(__DIR__) . '/.env');
Config::loadFiles(__DIR__ . '/../config');
}

public function testAll()
{
$this->assertInternalType('array', Config::all());
}

public function testGetEnv()
{
$this->assertInternalType('string', Config::getEnv('APP_NAME'));
}

public function testGet()
{
$this->assertInternalType('string', Config::get('app.app_name'));
$this->assertNull(Config::get('app.debug'));
}

public function testHas()
{
$this->assertFalse(Config::has('app.debug'));
$this->assertTrue(Config::has('app.app_name'));
}
}

0 comments on commit 4bca3ed

Please sign in to comment.