diff --git a/.env b/.env new file mode 100644 index 0000000..076a191 --- /dev/null +++ b/.env @@ -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 diff --git a/.env_examplde b/.env_examplde new file mode 100644 index 0000000..076a191 --- /dev/null +++ b/.env_examplde @@ -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 diff --git a/.gitignore b/.gitignore index 9fcd5ec..16ef6a2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /vendor composer.lock /attachment -/tests \ No newline at end of file +.env \ No newline at end of file diff --git a/config/app.php b/config/app.php new file mode 100644 index 0000000..e1eb587 --- /dev/null +++ b/config/app.php @@ -0,0 +1,34 @@ +_/___.' >'"". + * | | : `- \`.;`\ _ /`;.`/ - ` : | | + * \ \ `-. \_ __\ /__ _/ .-` / / + * ========`-.____`-.___\_____/___.-`____.-'======== + * `=---=' + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * 佛祖保佑 永无BUG 永不修改 + * + */ + +return [ + 'app_name'=>'pfinal_club' +]; \ No newline at end of file diff --git a/src/build/Base.php b/src/build/Base.php index 6fedd68..8109665 100644 --- a/src/build/Base.php +++ b/src/build/Base.php @@ -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; + } + } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..7e85bbf --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,64 @@ +_/___.' >'"". + * | | : `- \`.;`\ _ /`;.`/ - ` : | | + * \ \ `-. \_ __\ /__ _/ .-` / / + * ========`-.____`-.___\_____/___.-`____.-'======== + * `=---=' + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * 佛祖保佑 永无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')); + } +}