From dfacf65708acaac28326dd33ba82c75f41ff0887 Mon Sep 17 00:00:00 2001 From: zhouyangyang Date: Sat, 18 Jun 2022 01:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 10 +++++++ composer.json | 6 ++++- src/Config.php | 32 ++++++++++++++++++++++ src/ConfigProvider.php | 35 ++++++++++++++++++++++++ src/Listener/MultiEnvListener.php | 44 +++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 src/Config.php create mode 100644 src/ConfigProvider.php create mode 100644 src/Listener/MultiEnvListener.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01a4aac --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.cache +*.log +.buildpath +.settings/ +.project +*.patch +.idea +.DS_Store +vendor/ +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index 32cbeda..aa0000d 100644 --- a/composer.json +++ b/composer.json @@ -12,5 +12,9 @@ "email": "zyimm@qq.com" } ], - "require": {} + "require": { + "hyperf/event": "^2.2", + "hyperf/framework": "^2.2", + "vlucas/phpdotenv": "^5.4" + } } diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..128aafa --- /dev/null +++ b/src/Config.php @@ -0,0 +1,32 @@ +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(); + } + } +} \ No newline at end of file diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php new file mode 100644 index 0000000..938fe8a --- /dev/null +++ b/src/ConfigProvider.php @@ -0,0 +1,35 @@ + [ + ], + 'commands' => [ + ], + 'listener' => [ + MultiEnvListener::class, + ], + 'annotations' => [ + 'scan' => [ + 'paths' => [ + __DIR__, + ], + 'ignore_annotations' => [ + 'mixin', + ], + ], + ], + 'publish' => [ + ] + ]; + } +} \ No newline at end of file diff --git a/src/Listener/MultiEnvListener.php b/src/Listener/MultiEnvListener.php new file mode 100644 index 0000000..ef9edff --- /dev/null +++ b/src/Listener/MultiEnvListener.php @@ -0,0 +1,44 @@ +get(Config::class); + } + } + } +} \ No newline at end of file