配置写入

This commit is contained in:
zyimm 2022-07-05 09:10:25 +08:00
parent 6bfdccdc6f
commit 6252e92fc4
3 changed files with 53 additions and 7 deletions

View File

@ -19,7 +19,7 @@
}, },
"require": { "require": {
"hyperf/event": "^2.2", "hyperf/event": "^2.2",
"hyperf/framework": "^2.2", "hyperf/framework": "^2.1|2.2",
"vlucas/phpdotenv": "^5.4" "vlucas/phpdotenv": "^5.4"
} }
} }

View File

@ -3,28 +3,70 @@
namespace Zyimm\HyperfMultiEnv; namespace Zyimm\HyperfMultiEnv;
use Dotenv\Dotenv; use Dotenv\Dotenv;
use Dotenv\Repository\RepositoryBuilder;
use Dotenv\Repository\Adapter; use Dotenv\Repository\Adapter;
use Dotenv\Repository\RepositoryBuilder;
use Hyperf\Config\ProviderConfig;
use Hyperf\Contract\ConfigInterface;
use Symfony\Component\Finder\Finder;
class Config class Config
{ {
private string $env; 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->env = $env;
$this->config = $config;
} }
public function get() public function get()
{ {
// 加载env // 加载env
if (file_exists(BASE_PATH . '/.env.' . $this->env)) { if (file_exists(BASE_PATH.'/.env.'.$this->env)) {
$repository = RepositoryBuilder::createWithNoAdapters() $repository = RepositoryBuilder::createWithNoAdapters()
->addReader(Adapter\PutenvAdapter::class) ->addReader(Adapter\PutenvAdapter::class)
->addWriter(Adapter\PutenvAdapter::class) ->addWriter(Adapter\PutenvAdapter::class)
->make(); ->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;
}
} }

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Zyimm\HyperfMultiEnv\Listener; namespace Zyimm\HyperfMultiEnv\Listener;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Event\Annotation\Listener; use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication; use Hyperf\Framework\Event\BootApplication;
@ -37,7 +38,10 @@ class MultiEnvListener implements ListenerInterface
$env_path = BASE_PATH.'/.env.'.$env; $env_path = BASE_PATH.'/.env.'.$env;
if ($env !== null && $event instanceof BootApplication) { if ($env !== null && $event instanceof BootApplication) {
if (file_exists($env_path) && ApplicationContext::hasContainer()) { if (file_exists($env_path) && ApplicationContext::hasContainer()) {
(ApplicationContext::getContainer())->get(Config::class); (ApplicationContext::getContainer())->make(Config::class, [
$env,
di(ConfigInterface::class)
])->get();
} }
} }
} }