初次提交

This commit is contained in:
zhouyangyang 2022-06-18 01:19:07 +08:00
parent 58da0de8d6
commit dfacf65708
5 changed files with 126 additions and 1 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
*.cache
*.log
.buildpath
.settings/
.project
*.patch
.idea
.DS_Store
vendor/
composer.lock

View File

@ -12,5 +12,9 @@
"email": "zyimm@qq.com" "email": "zyimm@qq.com"
} }
], ],
"require": {} "require": {
"hyperf/event": "^2.2",
"hyperf/framework": "^2.2",
"vlucas/phpdotenv": "^5.4"
}
} }

32
src/Config.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Zyimm\HyperfMultiEnv;
use Dotenv\Dotenv;
use Dotenv\Repository\RepositoryBuilder;
use Hyperf\Config\ProviderConfig;
use Symfony\Component\Finder\Finder;
use Dotenv\Repository\Adapter;
class Config
{
private string $env;
public function __construct(string $env)
{
$this->env = $env;
}
public function get()
{
// Load env before config.
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();
}
}
}

35
src/ConfigProvider.php Normal file
View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Zyimm\HyperfMultiEnv;
use Zyimm\HyperfMultiEnv\Listener\MultiEnvListener;
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
],
'commands' => [
],
'listener' => [
MultiEnvListener::class,
],
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
'ignore_annotations' => [
'mixin',
],
],
],
'publish' => [
]
];
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Zyimm\HyperfMultiEnv\Listener;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zyimm\HyperfMultiEnv\Config;
/**
* @Listener
* Class MultiEnvListener
*/
class MultiEnvListener implements ListenerInterface
{
public function listen(): array
{
return [
BootApplication::class,
];
}
/**
* process
*
* @param object $event
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function process(object $event)
{
$env = env('APP_ENV');
$env_path = BASE_PATH.'/.env.'.$env;
if ($event instanceof BootApplication && $env !== null) {
if (file_exists($env_path) && ApplicationContext::hasContainer()) {
(ApplicationContext::getContainer())->get(Config::class);
}
}
}
}