From 6252e92fc4dde9eb1c288cab5a14e8dc70e52ece Mon Sep 17 00:00:00 2001 From: zyimm Date: Tue, 5 Jul 2022 09:10:25 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- src/Config.php | 52 ++++++++++++++++++++++++++++--- src/Listener/MultiEnvListener.php | 6 +++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index bc25b9b..dd878ca 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "require": { "hyperf/event": "^2.2", - "hyperf/framework": "^2.2", + "hyperf/framework": "^2.1|2.2", "vlucas/phpdotenv": "^5.4" } } diff --git a/src/Config.php b/src/Config.php index fc3d532..f6325af 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,28 +3,70 @@ namespace Zyimm\HyperfMultiEnv; use Dotenv\Dotenv; -use Dotenv\Repository\RepositoryBuilder; use Dotenv\Repository\Adapter; +use Dotenv\Repository\RepositoryBuilder; +use Hyperf\Config\ProviderConfig; +use Hyperf\Contract\ConfigInterface; +use Symfony\Component\Finder\Finder; class Config { private string $env; - public function __construct(string $env) + private ConfigInterface $config; + + private string $configPath = BASE_PATH.'/config/'; + + public function __construct(string $env, ConfigInterface $config) { $this->env = $env; + + $this->config = $config; } public function get() { // 加载env - if (file_exists(BASE_PATH . '/.env.' . $this->env)) { + if (file_exists(BASE_PATH.'/.env.'.$this->env)) { $repository = RepositoryBuilder::createWithNoAdapters() ->addReader(Adapter\PutenvAdapter::class) ->addWriter(Adapter\PutenvAdapter::class) ->make(); - - Dotenv::create($repository, [BASE_PATH], '.env.' . $this->env)->load(); + Dotenv::create($repository, [BASE_PATH], '.env.'.$this->env)->load(); + $this->update(); } } + + public function update() + { + $base = $this->read($this->configPath.'config.php'); + $server = $this->read($this->configPath.'server.php'); + $autoload = $this->scan([$this->configPath.'autoload']); + $all_config = array_merge_recursive(ProviderConfig::load(), $server, $base, ...$autoload); + foreach ($all_config as $key => $value) { + $this->config->set($key, $value); + } + } + + private function read(string $config_path): array + { + $config = []; + if (file_exists($config_path) && is_readable($config_path)) { + $config = require $config_path; + } + return is_array($config) ? $config : []; + } + + private function scan(array $paths) + { + $configs = []; + $finder = new Finder(); + $finder->files()->in($paths)->name('*.php'); + foreach ($finder as $file) { + $configs[] = [ + $file->getBasename('.php') => require $file->getRealPath(), + ]; + } + return $configs; + } } \ No newline at end of file diff --git a/src/Listener/MultiEnvListener.php b/src/Listener/MultiEnvListener.php index ffa76fe..de052ab 100644 --- a/src/Listener/MultiEnvListener.php +++ b/src/Listener/MultiEnvListener.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Zyimm\HyperfMultiEnv\Listener; +use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Annotation\Listener; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BootApplication; @@ -37,7 +38,10 @@ class MultiEnvListener implements ListenerInterface $env_path = BASE_PATH.'/.env.'.$env; if ($env !== null && $event instanceof BootApplication) { if (file_exists($env_path) && ApplicationContext::hasContainer()) { - (ApplicationContext::getContainer())->get(Config::class); + (ApplicationContext::getContainer())->make(Config::class, [ + $env, + di(ConfigInterface::class) + ])->get(); } } }