Skip to content

Commit 54cec64

Browse files
Refactored to use environment variables object
1 parent dcf11a4 commit 54cec64

19 files changed

+889
-196
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ SECRET_KEY="abc123"
8484
You can then load `.env` in your application with:
8585

8686
```php
87-
$dotenv = new Dotenv\Dotenv(__DIR__);
87+
$dotenv = Dotenv\Dotenv::create(__DIR__);
8888
$dotenv->load();
8989
```
9090

9191
Optionally you can pass in a filename as the second parameter, if you would like to use something other than `.env`
9292

9393
```php
94-
$dotenv = new Dotenv\Dotenv(__DIR__, 'myconfig');
94+
$dotenv = Dotenv\Dotenv::create(__DIR__, 'myconfig');
9595
$dotenv->load();
9696
```
9797

@@ -136,10 +136,26 @@ If you want Dotenv to overwrite existing environment variables, use `overload`
136136
instead of `load`:
137137

138138
```php
139-
$dotenv = new Dotenv\Dotenv(__DIR__);
139+
$dotenv = Dotenv\Dotenv::create(__DIR__);
140140
$dotenv->overload();
141141
```
142142

143+
### Loader Customization
144+
145+
Need us to not set `$_ENV` but not `$_SERVER`, or have other custom requirements? No problem! Simply pass a custom implementation of `Dotenv\Environment\FactoryInterface` to `Dotenv\Loader` on construction. In practice, you may not even need a custom implementation, since our default implementation allows you provide an array of `Dotenv\Environment\Adapter\AdapterInterface` for proxing the underlying calls to.
146+
147+
For example, if you want us to only ever fiddle with `$_ENV` and `putenv`, then you can setup Dotenv as follows:
148+
149+
```php
150+
$factory = new Dotenv\Environment\DotenvFactory([
151+
new Dotenv\Environment\Adapter\EnvConstAdapter(),
152+
new Dotenv\Environment\Adapter\PutenvAdapter(),
153+
]);
154+
155+
$dotenv = Dotenv\Dotenv::create(__DIR__, null, $factory);
156+
```
157+
158+
143159
Requiring Variables to be Set
144160
-----------------------------
145161

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^5.4 || ^7.0"
14+
"php": "^5.4 || ^7.0",
15+
"phpoption/phpoption": "^1.5"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0"

src/Dotenv.php

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Dotenv;
44

5+
use Dotenv\Environment\DotenvFactory;
6+
use Dotenv\Environment\FactoryInterface;
57
use Dotenv\Exception\InvalidPathException;
68

79
/**
@@ -13,36 +15,69 @@
1315
class Dotenv
1416
{
1517
/**
16-
* The file path.
18+
* The loader instance.
1719
*
18-
* @var string
20+
* @var \Dotenv\Loader
1921
*/
20-
protected $filePath;
22+
protected $loader;
2123

2224
/**
23-
* The loader instance.
25+
* Create a new dotenv instance.
26+
*
27+
* @param \Dotenv\Loader $loader
2428
*
25-
* @var \Dotenv\Loader|null
29+
* @return void
2630
*/
27-
protected $loader;
31+
public function __construct(Loader $loader)
32+
{
33+
$this->loader = $loader;
34+
}
2835

2936
/**
3037
* Create a new dotenv instance.
3138
*
32-
* @param string $path
33-
* @param string $file
39+
* @param string $path
40+
* @param string|null $file
41+
* @param \Dotenv\Environment\FactoryInterface|null $envFactory
3442
*
35-
* @return void
43+
* @return \Dotenv\Dotenv
44+
*/
45+
public static function create($path, $file = null, FactoryInterface $envFactory = null)
46+
{
47+
$loader = new Loader(
48+
self::getFilePath($path, $file),
49+
$envFactory ?: new DotenvFactory(),
50+
true
51+
);
52+
53+
return new Dotenv($loader);
54+
}
55+
56+
57+
/**
58+
* Returns the full path to the file.
59+
*
60+
* @param string $path
61+
* @param string|null $file
62+
*
63+
* @return string
3664
*/
37-
public function __construct($path, $file = '.env')
65+
private static function getFilePath($path, $file)
3866
{
39-
$this->filePath = $this->getFilePath($path, $file);
40-
$this->loader = new Loader($this->filePath, true);
67+
if (!is_string($file)) {
68+
$file = '.env';
69+
}
70+
71+
$filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
72+
73+
return $filePath;
4174
}
4275

4376
/**
4477
* Load environment file in given directory.
4578
*
79+
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
80+
*
4681
* @return array
4782
*/
4883
public function load()
@@ -68,37 +103,22 @@ public function safeLoad()
68103
/**
69104
* Load environment file in given directory.
70105
*
106+
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
107+
*
71108
* @return array
72109
*/
73110
public function overload()
74111
{
75112
return $this->loadData(true);
76113
}
77114

78-
/**
79-
* Returns the full path to the file.
80-
*
81-
* @param string $path
82-
* @param string $file
83-
*
84-
* @return string
85-
*/
86-
protected function getFilePath($path, $file)
87-
{
88-
if (!is_string($file)) {
89-
$file = '.env';
90-
}
91-
92-
$filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
93-
94-
return $filePath;
95-
}
96-
97115
/**
98116
* Actually load the data.
99117
*
100118
* @param bool $overload
101119
*
120+
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
121+
*
102122
* @return array
103123
*/
104124
protected function loadData($overload = false)
@@ -121,10 +141,10 @@ public function required($variable)
121141
/**
122142
* Get the list of environment variables declared inside the 'env' file.
123143
*
124-
* @return array
144+
* @return string[]
125145
*/
126146
public function getEnvironmentVariableNames()
127147
{
128-
return $this->loader->variableNames;
148+
return $this->loader->getEnvironmentVariableNames();
129149
}
130150
}

src/Environment/AbstractVariables.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Dotenv\Environment;
4+
5+
use ArrayAccess;
6+
7+
/**
8+
* This is the abstract variables implementation.
9+
*
10+
* Extend this as required, implementing "get", "set", and "clear".
11+
*/
12+
abstract class AbstractVariables implements VariablesInterface
13+
{
14+
/**
15+
* Are we immutable?
16+
*
17+
* @var bool
18+
*/
19+
private $immutable;
20+
21+
/**
22+
* Create a new environment variables instance.
23+
*
24+
* @param bool $immutable
25+
*
26+
* @return void
27+
*/
28+
public function __construct($immutable)
29+
{
30+
$this->immutable = $immutable;
31+
}
32+
33+
/**
34+
* Determine if the environment is immutable.
35+
*
36+
* @return bool
37+
*/
38+
public function isImmutable()
39+
{
40+
return $this->immutable;
41+
}
42+
43+
/**
44+
* Tells whether environment variable has been defined.
45+
*
46+
* @param string $name
47+
*
48+
* @return bool
49+
*/
50+
public function has($name)
51+
{
52+
return !is_null($this->get($name));
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function offsetExists($offset)
59+
{
60+
return $this->has($offset);
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function offsetGet($offset)
67+
{
68+
return $this->get($offset);
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function offsetSet($offset, $value)
75+
{
76+
$this->set($offset, $value);
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function offsetUnset($offset)
83+
{
84+
$this->clear($offset);
85+
}
86+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Dotenv\Environment\Adapter;
4+
5+
interface AdapterInterface
6+
{
7+
/**
8+
* Determines if the adapter is supported.
9+
*
10+
* @return bool
11+
*/
12+
public function isSupported();
13+
14+
/**
15+
* Get an environment variable, if it exists.
16+
*
17+
* @param string $name
18+
*
19+
* @return \PhpOption\Option
20+
*/
21+
public function get($name);
22+
23+
/**
24+
* Set an environment variable.
25+
*
26+
* @param string $name
27+
* @param string|null $value
28+
*
29+
* @return void
30+
*/
31+
public function set($name, $value = null);
32+
33+
/**
34+
* Clear an environment variable.
35+
*
36+
* @param string $name
37+
*
38+
* @return void
39+
*/
40+
public function clear($name);
41+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Dotenv\Environment\Adapter;
4+
5+
use PhpOption\None;
6+
7+
class ApacheAdapter implements AdapterInterface
8+
{
9+
/**
10+
* Determines if the adapter is supported.
11+
*
12+
* This happens if PHP is running as an Apache module.
13+
*
14+
* @return bool
15+
*/
16+
public function isSupported()
17+
{
18+
return function_exists('apache_getenv') && function_exists('apache_setenv');
19+
}
20+
21+
/**
22+
* Get an environment variable, if it exists.
23+
*
24+
* This is intentionally not implemented, since this adapter exists only as
25+
* a means to overwrite existing apache environment variables.
26+
*
27+
* @param string $name
28+
*
29+
* @return \PhpOption\Option
30+
*/
31+
public function get($name)
32+
{
33+
return None::create();
34+
}
35+
36+
/**
37+
* Set an environment variable.
38+
*
39+
* Only if an existing apache variable exists do we overwrite it.
40+
*
41+
* @param string $name
42+
* @param string|null $value
43+
*
44+
* @return void
45+
*/
46+
public function set($name, $value = null)
47+
{
48+
if (apache_getenv($name) !== false) {
49+
apache_setenv($name, $value);
50+
}
51+
}
52+
53+
/**
54+
* Clear an environment variable.
55+
*
56+
* @param string $name
57+
*
58+
* @return void
59+
*/
60+
public function clear($name)
61+
{
62+
// Nothing to do here.
63+
}
64+
}

0 commit comments

Comments
 (0)