From 0ff172def1db05fe98626e04579530761fbb7c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 18 May 2021 10:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Abstracts/Config.php | 3 +- System/Runtime.php | 75 ++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/System/Abstracts/Config.php b/System/Abstracts/Config.php index 488463b7..97458a9a 100644 --- a/System/Abstracts/Config.php +++ b/System/Abstracts/Config.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Snowflake\Abstracts; use Exception; +use Snowflake\Core\ArrayAccess; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; @@ -56,7 +57,7 @@ class Config extends Component if (empty($configs)) { return; } - $config->data = $configs; + $config->data = ArrayAccess::merge($config->data, $configs); } /** diff --git a/System/Runtime.php b/System/Runtime.php index 29f5bc6e..62014f29 100644 --- a/System/Runtime.php +++ b/System/Runtime.php @@ -17,35 +17,74 @@ class Runtime extends Command { - public string $command = 'runtime:builder'; + public string $command = 'runtime:builder'; - public string $description = 'create app file cache'; + public string $description = 'create app file cache'; - const CACHE_NAME = '.runtime.cache'; - const CONFIG_NAME = '.config.cache'; + const CACHE_NAME = '.runtime.cache'; + const CONFIG_NAME = '.config.cache'; /** * @param Input $dtl * @throws Exception */ - public function onHandler(Input $dtl) - { - // TODO: Implement onHandler() method. - $annotation = Snowflake::app()->getAnnotation(); + public function onHandler(Input $dtl) + { + // TODO: Implement onHandler() method. + $annotation = Snowflake::app()->getAnnotation(); - $runtime = storage(static::CACHE_NAME); + $runtime = storage(static::CACHE_NAME); - $configs = Snowflake::app()->getConfig()->getData(); - array_walk_recursive($configs, function (&$value, $key) { - if ($value instanceof \Closure) { - $value = null; - } - }); + $configs = $this->configEach(); + + Snowflake::writeFile(storage(static::CONFIG_NAME), serialize($configs)); + Snowflake::writeFile($runtime, serialize($annotation->getLoader())); + } + + + /** + * @return array + * @throws Exception + */ + public function configEach(): array + { + $array = []; + $configs = Snowflake::app()->getConfig(); + foreach ($configs->getData() as $key => $datum) { + if ($datum instanceof \Closure) { + continue; + } + if (is_array($datum)) { + $array[$key] = $this->arrayEach($datum); + } else { + $array[$key] = $datum; + } + } + return $array; + } + + + /** + * @param array $value + * @return array + */ + private function arrayEach(array $value): array + { + $array = []; + foreach ($value as $key => $item) { + if ($item instanceof \Closure) { + continue; + } + if (is_array($item)) { + $array[$key] = $this->arrayEach($item); + } else { + $array[$key] = $item; + } + } + return $array; + } - Snowflake::writeFile(storage(static::CONFIG_NAME), serialize($configs)); - Snowflake::writeFile($runtime, serialize($annotation->getLoader())); - } }