Skip to content

Commit 4718cb6

Browse files
committed
Consider reading environment variables from console
1 parent 1978970 commit 4718cb6

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

config/settings.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
declare(strict_types=1);
44

55
use Dotenv\Exception\InvalidPathException;
6+
use FriendsOfTYPO3\CrowdinBase\Settings\EnvironmentVariables;
67
use FriendsOfTYPO3\CrowdinBase\Settings\Settings;
78
use FriendsOfTYPO3\CrowdinBase\Settings\PathResolver;
89

910
try {
1011
$dotenv = Dotenv\Dotenv::createImmutable(getcwd());
1112
$dotenv->load();
1213
} catch (InvalidPathException) {
13-
die("\n==> .env file not found! Please copy the .env.example in your project root folder to .env and set the configuration accordingly.\n\n");
14+
// Environment variables can also be defined in console, so there is no .env file necessary
15+
}
16+
17+
foreach (EnvironmentVariables::cases() as $variable) {
18+
if (getenv($variable->name) !== false) {
19+
$_ENV[$variable->name] = getenv($variable->name);
20+
}
1421
}
1522

1623
if (file_exists(__DIR__ . '/skippedProjects.php')) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FriendsOfTYPO3\CrowdinBase\Settings;
6+
7+
enum EnvironmentVariables
8+
{
9+
case CONFIGURATION_FILE;
10+
case CROWDIN_ACCESS_TOKEN;
11+
}

src/Settings/Settings.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
*/
1111
final readonly class Settings
1212
{
13-
private const array REQUIRED_ENV_KEYS = [
14-
'CONFIGURATION_FILE',
15-
'CROWDIN_ACCESS_TOKEN',
16-
];
17-
1813
/**
1914
* @param list<non-empty-string> $skippedProjects
2015
*/
@@ -30,9 +25,9 @@ private function __construct(
3025
*/
3126
public static function fromEnvironment(array $env, array $skippedProjects, PathResolver $pathResolver): self
3227
{
33-
foreach (self::REQUIRED_ENV_KEYS as $key) {
34-
if (!isset($env[$key])) {
35-
throw UndefinedEnvironmentVariableException::fromEnvironmentKey($key);
28+
foreach (EnvironmentVariables::cases() as $variable) {
29+
if (!isset($env[$variable->name])) {
30+
throw UndefinedEnvironmentVariableException::fromEnvironmentKey($variable->name);
3631
}
3732
}
3833

src/Settings/UndefinedEnvironmentVariableException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static function fromEnvironmentKey(string $key): self
1010
{
1111
return new self(
1212
\sprintf(
13-
'The environment variable "%s" is not defined in the .env file. Have a look into the .env.example file for more details.',
13+
'The environment variable "%s" is not defined as environment variable (either in .env file or as environment variable). Have a look into the .env.example file for more details.',
1414
$key,
1515
),
1616
1762853210

tests/Settings/UndefinedEnvironmentVariableExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function fromEnvironmentKey(): void
1919
$actual = UndefinedEnvironmentVariableException::fromEnvironmentKey('some-key');
2020

2121
self::assertSame(
22-
'The environment variable "some-key" is not defined in the .env file. Have a look into the .env.example file for more details.',
22+
'The environment variable "some-key" is not defined as environment variable (either in .env file or as environment variable). Have a look into the .env.example file for more details.',
2323
$actual->getMessage(),
2424
);
2525
self::assertSame(1762853210, $actual->getCode());

0 commit comments

Comments
 (0)