Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit 144ee46

Browse files
authored
Merge pull request #2 from ergebnis/feature/production
Enhancement: Add Production
2 parents 532acdd + 15b7b8b commit 144ee46

11 files changed

Lines changed: 444 additions & 6 deletions

File tree

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ on:
99
- "master"
1010

1111
env:
12-
MIN_COVERED_MSI: 97
13-
MIN_MSI: 97
12+
MIN_COVERED_MSI: 99
13+
MIN_MSI: 98
1414
REQUIRED_PHP_EXTENSIONS: "mbstring"
1515

1616
jobs:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
1111
### Added
1212

1313
* Added `Test`, which allows backing up, restoring, and safely modifying environment variables in test environments ([#1]), by [@localheinz]
14+
* Added `Production`, which allows reading, setting, and unsetting environment variables in production environments ([#2]), by [@localheinz]
1415

1516
[c0c63bb...master]: https://github.com/ergebnis/environment-variables/compare/c0c63bb...master
1617

1718
[#1]: https://github.com/ergebnis/environment-variables/pull/1
19+
[#2]: https://github.com/ergebnis/environment-variables/pull/2
1820

1921
[@localheinz]: https://github.com/localheinz

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
MIN_COVERED_MSI:=97
2-
MIN_MSI:=97
1+
MIN_COVERED_MSI:=98
2+
MIN_MSI:=98
33

44
.PHONY: it
55
it: coding-standards dependency-analysis static-code-analysis tests ## Runs the coding-standards, dependency-analysis, static-code-analysis, and tests targets

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ $ composer require ergebnis/environment-variables
2121

2222
## Usage
2323

24+
### `Ergebnis\Environment\Variables\Production`
25+
26+
If you want to read, set, and unset environment variables in an object-oriented way in a production environment, you can use [`Ergebnis\Environment\Variables\Production`](src/Production.php):
27+
28+
```php
29+
use Ergebnis\Environment\Variables;
30+
31+
final class BuildEnvironment
32+
{
33+
private $environmentVariables;
34+
35+
public function __construct(Variables\Production $environmentVariables)
36+
{
37+
$this->environmentVariables = $environmentVariables;
38+
}
39+
40+
public function isGitHubActions(): bool
41+
{
42+
return $this->environmentVariables->has('GITHUB_ACTIONS')
43+
&& 'true' === $this->environmentVariables->get('GITHUB_ACTIONS');
44+
}
45+
46+
public function isTravisCi(): bool
47+
{
48+
return $this->environmentVariables->has('TRAVIS')
49+
&& 'true' === $this->environmentVariables->get('TRAVIS');
50+
}
51+
}
52+
```
53+
2454
### `Ergebnis\Environment\Variables\Test`
2555

2656
If your tests depend on environment variables, you have the following challenges:

src/Exception/InvalidName.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/environment-variables
12+
*/
13+
14+
namespace Ergebnis\Environment\Variables\Exception;
15+
16+
final class InvalidName extends \InvalidArgumentException implements Exception
17+
{
18+
public static function create(string $name): self
19+
{
20+
return new self(\sprintf(
21+
'Name cannot be empty or untrimmed, but "%s" is.',
22+
$name
23+
));
24+
}
25+
}

src/Production.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/environment-variables
12+
*/
13+
14+
namespace Ergebnis\Environment\Variables;
15+
16+
final class Production implements Variables
17+
{
18+
public function has(string $name): bool
19+
{
20+
if ('' === $name || \trim($name) !== $name) {
21+
throw Exception\InvalidName::create($name);
22+
}
23+
24+
return false !== \getenv($name);
25+
}
26+
27+
public function get(string $name)
28+
{
29+
if ('' === $name || \trim($name) !== $name) {
30+
throw Exception\InvalidName::create($name);
31+
}
32+
33+
return \getenv($name);
34+
}
35+
36+
public function set(string $name, string $value): void
37+
{
38+
if ('' === $name || \trim($name) !== $name) {
39+
throw Exception\InvalidName::create($name);
40+
}
41+
42+
\putenv(\sprintf(
43+
'%s=%s',
44+
$name,
45+
$value
46+
));
47+
}
48+
49+
public function unset(string $name): void
50+
{
51+
if ('' === $name || \trim($name) !== $name) {
52+
throw Exception\InvalidName::create($name);
53+
}
54+
55+
\putenv($name);
56+
}
57+
}

src/Variables.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/environment-variables
12+
*/
13+
14+
namespace Ergebnis\Environment\Variables;
15+
16+
interface Variables
17+
{
18+
/**
19+
* @param string $name
20+
*
21+
* @throws Exception\InvalidName
22+
*
23+
* @return bool
24+
*/
25+
public function has(string $name): bool;
26+
27+
/**
28+
* @param string $name
29+
*
30+
* @throws Exception\InvalidName
31+
*
32+
* @return bool|string
33+
*/
34+
public function get(string $name);
35+
36+
/**
37+
* @param string $name
38+
* @param string $value
39+
*
40+
* @throws Exception\InvalidName
41+
*/
42+
public function set(string $name, string $value): void;
43+
44+
/**
45+
* @param string $name
46+
*
47+
* @throws Exception\InvalidName
48+
*/
49+
public function unset(string $name): void;
50+
}

test/DataProvider/Name.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/environment-variables
12+
*/
13+
14+
namespace Ergebnis\Environment\Variables\Test\DataProvider;
15+
16+
use Ergebnis\Test\Util\Helper;
17+
18+
final class Name
19+
{
20+
use Helper;
21+
22+
/**
23+
* @return \Generator<string, array<string>>
24+
*/
25+
public static function invalid(): \Generator
26+
{
27+
$values = [
28+
'string-blank' => ' ',
29+
'string-empty' => '',
30+
'string-untrimmed' => \sprintf(
31+
' %s ',
32+
self::faker()->sentence
33+
),
34+
];
35+
36+
foreach ($values as $key => $value) {
37+
yield $key => [
38+
$value,
39+
];
40+
}
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/environment-variables
12+
*/
13+
14+
namespace Ergebnis\Environment\Variables\Test\Unit\Exception;
15+
16+
use Ergebnis\Environment\Variables\Exception\InvalidName;
17+
use Ergebnis\Test\Util\Helper;
18+
use PHPUnit\Framework;
19+
20+
/**
21+
* @internal
22+
*
23+
* @covers \Ergebnis\Environment\Variables\Exception\InvalidName
24+
*/
25+
final class InvalidNameTest extends Framework\TestCase
26+
{
27+
use Helper;
28+
29+
public function testCreateReturnsException(): void
30+
{
31+
$name = \sprintf(
32+
' %s ',
33+
self::faker()->word
34+
);
35+
36+
$exception = InvalidName::create($name);
37+
38+
$message = \sprintf(
39+
'Name cannot be empty or untrimmed, but "%s" is.',
40+
$name
41+
);
42+
43+
self::assertSame($message, $exception->getMessage());
44+
}
45+
}

0 commit comments

Comments
 (0)