Skip to content

Commit a71d205

Browse files
author
Karthik P
committed
Intial component code
1 parent 78d24f1 commit a71d205

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
composer.phar
20+
# composer lock file
21+
composer.lock
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# vagrant runtime
32+
/.vagrant
33+
*.cache*
34+
35+
# PHP tidy cache
36+
.phptidy-cache

Loader.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace cyberinferno\yii\phpdotenv;
4+
5+
use Dotenv\Dotenv;
6+
use yii\base\Component;
7+
8+
/**
9+
* Class Loader
10+
*
11+
* @property Dotenv $dotenv
12+
*
13+
* @package yii\phpdotenv
14+
*/
15+
class Loader extends Component
16+
{
17+
/**
18+
* @var string Use if custom environment variable directory
19+
*/
20+
public $file;
21+
/**
22+
* @var bool Whether to overload already existing environment variables
23+
*/
24+
public $overload = false;
25+
/**
26+
* @var Dotenv
27+
*/
28+
private $_dotenv;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function init()
34+
{
35+
if ($this->file !== null) {
36+
$this->_dotenv = new Dotenv(__DIR__, $this->file);
37+
} else {
38+
$this->_dotenv = new Dotenv(__DIR__);
39+
}
40+
if ($this->overload) {
41+
$this->_dotenv->overload();
42+
} else {
43+
$this->_dotenv->load();
44+
}
45+
}
46+
47+
/**
48+
* Get dotenv
49+
* @return Dotenv
50+
*/
51+
public function getDotenv()
52+
{
53+
return $this->_dotenv;
54+
}
55+
}

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Phpdotenv extension for Yii 2
2+
===============================================
3+
4+
This is a Yii2 extension for [vulcas/phpdotenv](https://github.com/vlucas/phpdotenv)
5+
6+
Installation
7+
------------
8+
9+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
10+
11+
Either run
12+
13+
```
14+
php composer.phar require cyberinferno/yii2-phpdotenv
15+
```
16+
17+
or add
18+
19+
```json
20+
"cyberinferno/yii2-phpdotenv": "~1.0.0"
21+
```
22+
23+
to the require section of your composer.json.
24+
25+
26+
Configuration
27+
-------------
28+
29+
Usage:
30+
31+
```php
32+
return [
33+
//....
34+
'components' => [
35+
'dotenv' => [
36+
'class' => 'cyberinferno\yii\phpdotenv\Loader',
37+
'file' => '.env', // Optional parameter if custom environment variable directory
38+
'overload' => false, // Optional parameter whether to overload already existing environment variables. Defaults to false
39+
],
40+
]
41+
];
42+
```
43+
Accessing ``Dotenv`` object:
44+
45+
```php
46+
$dotenv = \Yii::$app->dotenv->dotenv;
47+
```
48+
49+
For further information about methods available in ``Dotenv`` class refer [Phpdotevn README](https://github.com/vlucas/phpdotenv/blob/master/README.md)

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "cyberinferno/yii2-phpdotenv",
3+
"description": "phpdotenv Yii2 extension",
4+
"type": "yii2-extension",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Karthik Panjaje",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4.0",
14+
"yiisoft/yii2": "~2.0.0",
15+
"vlucas/phpdotenv": "^2.4"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"cyberinferno\\yii\\phpdotenv\\": ""
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)