You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`:
153
166
154
167
```php
155
-
$dotenv = Dotenv\Dotenv::create(__DIR__);
156
-
$dotenv->overload();
168
+
$dotenv = Dotenv\Dotenv::createMutable(__DIR__);
169
+
$dotenv->load();
157
170
```
158
171
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:
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
+
174
197
175
198
Requiring Variables to be Set
176
199
-----------------------------
@@ -305,11 +328,11 @@ PHP dotenv is licensed under [The BSD 3-Clause License](LICENSE).
305
328
---
306
329
307
330
<divalign="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.
Copy file name to clipboardExpand all lines: UPGRADING.md
+68-2Lines changed: 68 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,69 @@
1
1
# Upgrading Guide
2
2
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
+
3
67
## V2 to V3
4
68
5
69
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:
24
88
25
89
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.
26
90
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:
28
92
29
93
```php
30
94
<?php
31
95
96
+
use Dotenv\Dotenv;
32
97
use Dotenv\Environment\Adapter\EnvConstAdapter;
33
98
use Dotenv\Environment\Adapter\ServerConstAdapter;
34
99
use Dotenv\Environment\DotenvFactory;
35
-
use Dotenv\Dotenv;
36
100
37
101
$factory = new DotenvFactory([new EnvConstAdapter(), new ServerConstAdapter()]);
38
102
39
103
Dotenv::create($path, null, $factory)->load();
40
104
```
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