This commit is contained in:
2020-09-03 15:23:11 +08:00
parent 5a038fb0b6
commit 5b4a1a5cd0
5 changed files with 86 additions and 42 deletions
+4 -2
View File
@@ -15,6 +15,7 @@ use HttpServer\Http\Response;
use HttpServer\Route\Router;
use HttpServer\Server;
use Snowflake\Annotation\Annotation;
use Snowflake\Cache\Memcached;
use Snowflake\Cache\Redis;
use Snowflake\Config;
use Snowflake\Di\Service;
@@ -24,7 +25,7 @@ use Snowflake\Exception\ComponentException;
use Snowflake\Exception\InitException;
use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Connection;
use Snowflake\Pool\RedisClient;
use Snowflake\Pool\Redis as SRedis;
use Snowflake\Snowflake;
use Snowflake\Event;
use Snowflake\Pool\Pool as SPool;
@@ -41,6 +42,7 @@ use Database\DatabasesProviders;
* @property Server $server
* @property DatabasesProviders $db
* @property Connection $connections
* @property Memcached $memcached
* @property Logger $logger
* @property Jwt $jwt
*/
@@ -335,7 +337,7 @@ abstract class BaseApplication extends Service
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => RedisClient::class],
'redis_connections' => ['class' => SRedis::class],
'pool' => ['class' => SPool::class],
'response' => ['class' => Response::class],
'request' => ['class' => Request::class],
+20 -26
View File
@@ -22,7 +22,16 @@ class Config extends Component
const ERROR_MESSAGE = 'The not find :key in app configs.';
public $data;
private $data;
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param $key
@@ -33,37 +42,22 @@ class Config extends Component
*/
public static function get($key, $try = FALSE, $default = null)
{
$config = Snowflake::app()->config;
$explode = explode('.', $key);
if (strpos($key, '.') === false) {
if (isset($config->data[$key])) {
return $config->data[$key];
} else if ($default !== null) {
return $default;
} else if ($try) {
$instance = Snowflake::app()->getConfig()->getData();
foreach ($explode as $index => $value) {
if (!isset($instance[$value]) && $try) {
throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE));
}
$instance = $instance[$value];
if (!is_array($instance) && $index + 1 < count($explode)) {
throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE));
}
return NULL;
}
$keys = explode('.', $key);
$parent = $config->data[array_shift($keys)] ?? null;
if (empty($parent)) {
if (empty($instance)) {
return $default;
}
if (empty($keys) || !is_array($parent)) {
return $parent;
}
foreach ($keys as $key) {
if (!isset($parent[$key])) {
return $parent;
}
$parent = $parent[$key];
if (empty($keys) || !is_array($parent)) {
return $parent;
}
}
return $parent;
return $instance;
}
/**