Skip to content

Commit 3ab1dc7

Browse files
Refactoring (#387)
1 parent fd9bd7d commit 3ab1dc7

39 files changed

+1046
-1022
lines changed

README.md

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ dotenv](https://github.com/bkeepers/dotenv).
1010
[![Build Status](https://travis-ci.org/vlucas/phpdotenv.svg?branch=master)](https://travis-ci.org/vlucas/phpdotenv)
1111

1212

13+
UPGRADING FROM V3
14+
-----------------
15+
16+
Version 4 seems some refactoring, and support for escaping dollars in values
17+
(https://github.com/vlucas/phpdotenv/pull/380). It is no longer possible to
18+
change immutability on the fly, and the `Loader` no longer is responsible for
19+
tracking immutability. It is now the responsibility of "repositories" to track
20+
this. One must explicitly decide if they want (im)mutability when constructing
21+
an instance of `Dotenv\Dotenv`.
22+
23+
For more details, please see the
24+
[release notes](https://github.com/vlucas/phpdotenv/releases/tag/v4.0.0) and
25+
the [upgrading guide](UPGRADING.md).
26+
27+
1328
UPGRADING FROM V2
1429
-----------------
1530

@@ -100,14 +115,14 @@ SECRET_KEY="abc123"
100115
You can then load `.env` in your application with:
101116

102117
```php
103-
$dotenv = Dotenv\Dotenv::create(__DIR__);
118+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
104119
$dotenv->load();
105120
```
106121

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

109124
```php
110-
$dotenv = Dotenv\Dotenv::create(__DIR__, 'myconfig');
125+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, 'myconfig');
111126
$dotenv->load();
112127
```
113128

@@ -143,34 +158,42 @@ CACHE_DIR="${BASE_DIR}/cache"
143158
TMP_DIR="${BASE_DIR}/tmp"
144159
```
145160

146-
### Immutability
161+
### Immutability and Repository Customization
147162

148-
By default, Dotenv will NOT overwrite existing environment variables that are
149-
already set in the environment.
150-
151-
If you want Dotenv to overwrite existing environment variables, use `overload`
152-
instead of `load`:
163+
Immutability refers to if Dotenv is allowed to overwrite existing environment
164+
variables. If you want Dotenv to overwrite existing environment variables,
165+
use `createMutable` instead of `createImmutable`:
153166

154167
```php
155-
$dotenv = Dotenv\Dotenv::create(__DIR__);
156-
$dotenv->overload();
168+
$dotenv = Dotenv\Dotenv::createMutable(__DIR__);
169+
$dotenv->load();
157170
```
158171

159-
### Loader Customization
160-
161-
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.
162-
163-
For example, if you want us to only ever fiddle with `$_ENV` and `putenv`, then you can setup Dotenv as follows:
172+
Behind the scenes, this is instructing the "repository" to allow immutability
173+
or not. By default, the repository is configured to allow overwriting existing
174+
values by default, which is relevent if one is calling the "create" method
175+
using the `RepositoryBuilder` to construct a more custom repository:
164176

165177
```php
166-
$factory = new Dotenv\Environment\DotenvFactory([
167-
new Dotenv\Environment\Adapter\EnvConstAdapter(),
168-
new Dotenv\Environment\Adapter\PutenvAdapter(),
169-
]);
170-
171-
$dotenv = Dotenv\Dotenv::create(__DIR__, null, $factory);
178+
$repository = Dotenv\Repository\RepositoryBuilder::create()
179+
->withReaders([
180+
new Dotenv\Repository\Adapter\EnvConstAdapter(),
181+
])
182+
->withWriters([
183+
new Dotenv\Repository\Adapter\EnvConstAdapter(),
184+
new Dotenv\Repository\Adapter\PutenvAdapter(),
185+
])
186+
->immutable()
187+
->get();
188+
189+
$dotenv = Dotenv\Dotenv::create($repository, __DIR__);
190+
$dotenv->load();
172191
```
173192

193+
The above example will write loaded values to `$_ENV` and `putenv`, but when
194+
interpolating environment variables, we'll only read from `$_ENV`. Moreover, it
195+
will never replace any variables already set before loading the file.
196+
174197

175198
Requiring Variables to be Set
176199
-----------------------------
@@ -305,11 +328,11 @@ PHP dotenv is licensed under [The BSD 3-Clause License](LICENSE).
305328
---
306329

307330
<div align="center">
308-
<b>
309-
<a href="https://tidelift.com/subscription/pkg/packagist-vlucas-phpdotenv?utm_source=packagist-vlucas-phpdotenv&utm_medium=referral&utm_campaign=readme">Get professional support for PHP dotenv with a Tidelift subscription</a>
310-
</b>
311-
<br>
312-
<sub>
313-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
314-
</sub>
331+
<b>
332+
<a href="https://tidelift.com/subscription/pkg/packagist-vlucas-phpdotenv?utm_source=packagist-vlucas-phpdotenv&utm_medium=referral&utm_campaign=readme">Get professional support for PHP dotenv with a Tidelift subscription</a>
333+
</b>
334+
<br>
335+
<sub>
336+
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
337+
</sub>
315338
</div>

UPGRADING.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
# Upgrading Guide
22

3+
## V3 to V4
4+
5+
V4 has again changed the way you initialize the `Dotenv` class. If you want immutable loading of environment variables, then replace `Dotenv::create` with `Dotenv::createImmutable`, and if you want mutable loading, replace `Dotenv::create` with `Dotenv::createMmutable` and `->overload()` with `->load()`. The `overload` method has been removed in faviour of specifying mutability at object construction.
6+
7+
The behaviour when parsing single quoted strings has now changed, to mimic the behaviour of bash. It is no longer possible to escape characters in single quoted strings, and everything is treated literally. As soon as the first single quote character is read, after the initial one, then the variable is treated as ending immediately at that point. When parsing unquoted or double quoted strings, it is now possible to escape dollar signs, to forcefully avoid variable interpolation. Escaping dollars is not mandated, in the sense that if a dollar is present, and not following by variable interpolation sytnax, this is allowed, and the dollar will be treated as a literal dollar. Finally, interpolation of variables is now performed right to left, instead of left to right, so it is possible to nest interpolations to allow using the value of a variable as the name of another for further interpolation.
8+
9+
The `getEnvironmentVariableNames` method is no longer available. This is because calls to `load()` (since v3.0.0) return an associative array of what was loaded, so `$dotenv->getEnvironmentVariableNames()` can be replaced with `array_keys($dotenv->load())`.
10+
11+
There have been various internal refactorings. Appart from what has already been mentioned, the only other changes likely to affect developers is:
12+
13+
1. The `Dotenv\Environment` namespace has been moved to `Dotenv\Repository`, the `Dotenv\Environment\Adapter\AdapterInterface` interface has been replaced by `Dotenv\Repository\Adapter\ReaderInterface` and `Dotenv\Repository\Adapter\WriterInterface`.
14+
2. The `Dotenv\Environment\DotenvFactory` has been (roughly) replaced by `Dotenv\Environment\RepositoryBuilder`, and `Dotenv\Environment\FactoryInterface` has been deleted.
15+
3. `Dotenv\Environment\AbstractVariables` has been replaced by `Dotenv\Repository\AbstractRepository`, `Dotenv\Environment\DotenvVariables` has been replaced by `Dotenv\Repository\AdapterRepository`, and `Dotenv\Environment\VariablesInterface` has been replaced by `Dotenv\Repository\RepositoryInterface`.
16+
4. The `Dotenv\Loader` class has been moved to `Dotenv\Loader\Loader`, and now has a different public interface. It no longer expects any parameters at construction, and implements only the new interface `Dotenv\Loader\LoaderInterface`. Its reponsibility has changed to purely taking raw env file content, and handing it off to the parser, dealing with variable interpolation, and sending off instructions to the repository to set variables. No longer can it be used as a way to read the environment by callers, and nor does it track immutability.
17+
5. The `Dotenv\Parser` and `Dotenv\Lines` classes have moved to `Dotenv\Loader\Parser` and `Dotenv\Loader\Lines`, respectively. `Dotenv\Loader\Parser::parse` now return has either `null` or `Dotenv\Loader\Value` objects as values, instead of `string`s. This is to support the new variable interpolation and dollar escaping features.
18+
6. The `Dotenv\Validator` constructor has changed from `__construct(array $variables, Loader $loader, $required = true)` to `__construct(RepositoryInterface $repository, array $variables, $required = true)`.
19+
20+
The example at the bottom of the below upgrading guide, in V4 now looks like:
21+
22+
```php
23+
<?php
24+
25+
use Dotenv\Dotenv;
26+
use Dotenv\Repository\Adapter\EnvConstAdapter;
27+
use Dotenv\Repository\Adapter\ServerConstAdapter;
28+
use Dotenv\Repository\RepositoryBuilder;
29+
30+
$adapters = [
31+
new EnvConstAdapter(),
32+
new ServerConstAdapter(),
33+
];
34+
35+
$repository = RepositoryBuilder::create()
36+
->withReaders($adapters)
37+
->withWriters($adapters)
38+
->immutable()
39+
->get();
40+
41+
Dotenv::create($repository, $path, null)->load();
42+
```
43+
44+
Since v3.2.0, it was easily possible to read a file and process variable interpolations, without actually "loading" the variables. This is still possible in v4.0.0. Example code that does this is as follows:
45+
46+
```php
47+
<?php
48+
49+
use Dotenv\Repository\Adapter\ArrayAdapter;
50+
use Dotenv\Repository\RepositoryBuilder;
51+
use Dotenv\Loader\Loader;
52+
53+
$adapters = [new ArrayAdapter()];
54+
55+
$repository = RepositoryBuilder::create()
56+
->withReaders($adapters)
57+
->withWriters($adapters)
58+
->get();
59+
60+
$variables = (new Loader())->load($repository, $content);
61+
```
62+
63+
Notice, that compared to v3, the loader no longer expects file paths in the constructor. Reading of the files is now managed by the `Dotenv\Dotenv` class. The loader is geuinely just loading the content into the repository.
64+
65+
Finally, we note that the minimum supported version of PHP has increased to 5.5.9, up from 5.4.0 in V3 and 5.3.9 in V2.
66+
367
## V2 to V3
468

569
V3 has changed the way you initialize the `Dotenv` class. Consequently, you will need to replace any occurrences of new Dotenv(...) with Dotenv::create(...), since our new native constructor takes a `Loader` instance now.
@@ -24,17 +88,19 @@ Value parsing has been modified in the following ways:
2488

2589
In double quoted strings, double quotes and backslashes need escaping with a backslash, and in single quoted strings, single quote and backslashes need escaping with a backslash. In v2.5.2, forgetting an escape can lead to odd results due to the regex running out of stack, but this was fixed in 2.6 and 3.3, with 2.6 allowing you to continue after an unescaped backslash, but 3.3 not.
2690

27-
Finally, it's possible to use phpdotenv V3 in a threaded environment, instructing it to not call any functions that are not tread-safe:
91+
It's possible to use phpdotenv V3 in a threaded environment, instructing it to not call any functions that are not tread-safe:
2892

2993
```php
3094
<?php
3195

96+
use Dotenv\Dotenv;
3297
use Dotenv\Environment\Adapter\EnvConstAdapter;
3398
use Dotenv\Environment\Adapter\ServerConstAdapter;
3499
use Dotenv\Environment\DotenvFactory;
35-
use Dotenv\Dotenv;
36100

37101
$factory = new DotenvFactory([new EnvConstAdapter(), new ServerConstAdapter()]);
38102

39103
Dotenv::create($path, null, $factory)->load();
40104
```
105+
106+
Finally, we note that the minimum supported version of PHP has increased from 5.3.9 to 5.4.0.

0 commit comments

Comments
 (0)