Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Aug 30, 2020
1 parent 2df5e36 commit ede981e
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
composer.phar
/vendor/
composer.lock
.phpunit.result.cache

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "zhamao/config",
"description": "A simple json and php config provider supporting env variable",
"license": "Apache-2.0",
"authors": [
{
"name": "whale",
"email": "crazysnowcc@gamil.com"
}
],
"minimum-stability": "stable",
"require": {},
"require-dev": {
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
"ZM\\Config\\": "src/ZM/Config"
}
}
}
86 changes: 86 additions & 0 deletions src/ZM/Config/ZMConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php


namespace ZM\Config;


class ZMConfig
{
private static $path = ".";

private static $env = "";

private static $config = [];

public static $last_error = "";

public static function setDirectory($path = ".")
{
return self::$path = realpath($path);
}

public static function env($env = "")
{
self::$env = $env;
}

public static function get($name, $key = null)
{
if (isset(self::$config[$name])) $r = self::$config[$name];
else $r = self::loadConfig($name);
if($r === false) return false;
if($key !== null) return $r[$key] ?? null;
else return $r;
}

private static function loadConfig($name)
{
$ext = [".php", ".json"];
$env = ["", ".development", ".staging", ".production"];
foreach ($ext as $ext_name) {
if (self::$env === '') {
foreach ($env as $env_name) {
if (file_exists(self::$path . "/" . $name . $env_name . $ext_name)) {
return self::storeConfig($name, self::$path . "/" . $name . $env_name . $ext_name, $ext_name);
}
}
} else {
if (file_exists(self::$path . "/" . $name . "." . self::$env . $ext_name)) {
return self::storeConfig($name, self::$path . "/" . $name . "." . self::$env . $ext_name, $ext_name);
} elseif (file_exists(self::$path . "/" . $name . $ext_name)) {
return self::storeConfig($name, self::$path . "/" . $name . $ext_name, $ext_name);
} else {
self::$last_error = "你已指定环境 '" . self::$env . "', 但是配置文件 " . $name . "." . self::$env . "(.php/.json) 不存在,请检查";
return false;
}
}
}
self::$last_error = "未找到名称为 " . $name . " 的config文件,请检查文件名后缀是否为 \"json\"\"php\"";
return false;
}

private static function storeConfig($name, $string, $ext_name)
{
switch ($ext_name) {
case ".php":
$r = include_once $string;
if (is_array($r)) {
return self::$config[$name] = $r;
} else {
self::$last_error = "php配置文件include失败,请检查终端warning错误";
return false;
}
case ".json":
$r = json_decode(file_get_contents($string), true);
if (is_array($r)) {
return self::$config[$name] = $r;
} else {
self::$last_error = "json反序列化失败,请检查文件内容";
return false;
}
default:
self::$last_error = "内部错误";
return false;
}
}
}
40 changes: 40 additions & 0 deletions test/ZMConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php


use PHPUnit\Framework\TestCase;
use ZM\Config\ZMConfig;

class ZMConfigTest extends TestCase
{
public function setUp(): void
{
ZMConfig::setDirectory(__DIR__ . "/config");
}

public function testGet()
{
$this->assertIsArray(ZMConfig::get("test"));
}

public function testEnvSetting()
{
ZMConfig::env("production");
$this->assertEquals("production", ZMConfig::get("test")["hello"]);
}

public function testNonExistEnv()
{
ZMConfig::env("development");
$this->assertIsBool(ZMConfig::get("test"));
}

public function testGetKey() {
$this->assertEquals("world", ZMConfig::get("test", "hello"));
$this->assertNull(ZMConfig::get("test", "qwe"));
}

public function testJson() {
$this->assertIsArray(ZMConfig::get("global"));
$this->assertEquals("test", ZMConfig::get("global", "name"));
}
}
3 changes: 3 additions & 0 deletions test/config/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "test"
}
5 changes: 5 additions & 0 deletions test/config/test.production.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$config["hello"] = "production";

return $config;
5 changes: 5 additions & 0 deletions test/config/test.staging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$config["hello"] = "world";

return $config;

0 comments on commit ede981e

Please sign in to comment.