Skip to content

Commit

Permalink
Converted component to Yii2 bootstrap component
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik P committed Jun 27, 2017
1 parent a71d205 commit a09dc8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
40 changes: 26 additions & 14 deletions Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace cyberinferno\yii\phpdotenv;

use Dotenv\Dotenv;
use yii\base\Component;
use Yii;
use yii\base\BootstrapInterface;

/**
* Class Loader
Expand All @@ -12,12 +13,16 @@
*
* @package yii\phpdotenv
*/
class Loader extends Component
class Loader implements BootstrapInterface
{
/**
* @var string Use if custom environment variable directory
* @var string Environment variable file directory
*/
public $file;
public $path = '@vendor/../';
/**
* @var string Use if custom environment variable file
*/
public $file = '.env';
/**
* @var bool Whether to overload already existing environment variables
*/
Expand All @@ -30,17 +35,24 @@ class Loader extends Component
/**
* @inheritdoc
*/
public function init()
public function bootstrap($app)
{
if ($this->file !== null) {
$this->_dotenv = new Dotenv(__DIR__, $this->file);
} else {
$this->_dotenv = new Dotenv(__DIR__);
}
if ($this->overload) {
$this->_dotenv->overload();
} else {
$this->_dotenv->load();
try {
if ($this->path === null) {
$this->path = '@vendor/../';
}
if ($this->file === null) {
$this->file = '.env';
}
$this->path = Yii::getAlias($this->path);
$this->_dotenv = New Dotenv($this->path, $this->file);
if ($this->overload) {
$this->_dotenv->overload();
} else {
$this->_dotenv->load();
}
} catch (\Exception $e) {
\Yii::error('Could not load Dotenv file. ERROR: '.$e->getMessage());
}
}

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ php composer.phar require cyberinferno/yii2-phpdotenv
or add

```json
"cyberinferno/yii2-phpdotenv": "~1.0.0"
"cyberinferno/yii2-phpdotenv": "~2.0.0"
```

to the require section of your composer.json.
Expand All @@ -31,10 +31,11 @@ Usage:
```php
return [
//....
'components' => [
'dotenv' => [
'bootstrap' => [
[
'class' => 'cyberinferno\yii\phpdotenv\Loader',
'file' => '.env', // Optional parameter if custom environment variable directory
'path' => '@vendor/../', // Directory of the .env file
'file' => '.env', // Optional parameter if custom environment variable file
'overload' => false, // Optional parameter whether to overload already existing environment variables. Defaults to false
],
]
Expand Down

0 comments on commit a09dc8b

Please sign in to comment.