Skip to content

Commit

Permalink
新增支持.env环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Dec 3, 2018
1 parent d30c438 commit bb2c8bc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"psr/container": "~1.0",
"psr/http-message": "~1.0",
"psr/http-server-middleware": "~1.0",
"psr/simple-cache": "~1.0"
"psr/simple-cache": "~1.0",
"vlucas/phpdotenv": "~2.5"
},
"autoload": {
"psr-4" : {
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'Imi\HttpValidate',
'Imi\Enum',
'Imi\Lock',
'Imi\Config',
],
'atomics' => [
'session'
Expand Down
31 changes: 31 additions & 0 deletions src/Config/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Imi\Config\Dotenv;

use Imi\App;
use Imi\Config;
use Imi\Util\Imi;
use Imi\Bean\Annotation\Bean;

/**
* @Bean("Dotenv")
*/
class Dotenv extends \Dotenv\Dotenv
{
public function __construct()
{
parent::__construct(Imi::getNamespacePath(App::getNamespace()));
}

public function init()
{
foreach($_ENV as $name => $value)
{
$this->loader->clearEnvironmentVariable($name);
}
$this->overload();
foreach($_ENV as $name => $value)
{
Config::set($name, $value);
}
}
}
27 changes: 27 additions & 0 deletions src/Config/Listener/ImiInit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Imi\Config\Listener;

use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\Bean\Annotation\Listener;
use Imi\App;

/**
* @Listener(eventName="IMI.INITED",priority=PHP_INT_MAX)
* @Listener(eventName="IMI.INIT.WORKER.BEFORE",priority=PHP_INT_MAX)
*/
class ImiInit implements IEventListener
{
/**
* 事件处理方法
* @param EventParam $e
* @return void
*/
public function handle(EventParam $e)
{
// 加载 .env 配置
$dotenv = App::getBean('Dotenv');
$dotenv->init();
}

}

0 comments on commit bb2c8bc

Please sign in to comment.