Skip to content

Commit 17c5af7

Browse files
committed
feat(core): add Environment initializer
1 parent 1af376a commit 17c5af7

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed

packages/core/src/AppConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
*/
2828
public array $insightsProviders = [],
2929
) {
30-
$this->environment = $environment ?? Environment::fromEnv();
30+
$this->environment = $environment ?? Environment::guessFromEnvironment();
3131
$this->baseUri = $baseUri ?? env('BASE_URI') ?? '';
3232
}
3333
}

packages/core/src/Environment.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
use function Tempest\env;
88

9+
/**
10+
* Represents the environment in which the application is running.
11+
*/
912
enum Environment: string
1013
{
1114
case LOCAL = 'local';
1215
case STAGING = 'staging';
1316
case PRODUCTION = 'production';
14-
case CI = 'ci';
17+
case CONTINUOUS_INTEGRATION = 'ci';
1518
case TESTING = 'testing';
16-
case OTHER = 'other';
1719

1820
public function isProduction(): bool
1921
{
@@ -30,24 +32,27 @@ public function isLocal(): bool
3032
return $this === self::LOCAL;
3133
}
3234

33-
public function isCI(): bool
35+
public function isContinuousIntegration(): bool
3436
{
35-
return $this === self::CI;
37+
return $this === self::CONTINUOUS_INTEGRATION;
3638
}
3739

3840
public function isTesting(): bool
3941
{
4042
return $this === self::TESTING;
4143
}
4244

43-
public function isOther(): bool
45+
/**
46+
* Guesses the environment from the `ENVIRONMENT` environment variable.
47+
*/
48+
public static function guessFromEnvironment(): self
4449
{
45-
return $this === self::OTHER;
46-
}
50+
$value = env('ENVIRONMENT', default: 'local');
4751

48-
public static function fromEnv(): self
49-
{
50-
$value = env('ENVIRONMENT', 'local');
52+
// Can be removed after https://github.com/tempestphp/tempest-framework/pull/1836
53+
if (is_null($value)) {
54+
$value = 'local';
55+
}
5156

5257
return self::tryFrom($value) ?? throw new EnvironmentValueWasInvalid($value);
5358
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tempest\Core;
4+
5+
use Tempest\Container\Container;
6+
use Tempest\Container\Initializer;
7+
8+
final class EnvironmentInitalizer implements Initializer
9+
{
10+
public function initialize(Container $container): Environment
11+
{
12+
return $container->get(AppConfig::class)->environment;
13+
}
14+
}

packages/core/src/EnvironmentValueWasInvalid.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ final class EnvironmentValueWasInvalid extends Exception
1212
{
1313
public function __construct(string $value)
1414
{
15-
$possibleValues = arr(Environment::cases())->map(fn (Environment $environment) => $environment->value)->implode(', ');
15+
$possibleValues = arr(Environment::cases())
16+
->map(fn (Environment $environment) => $environment->value)
17+
->join();
1618

17-
parent::__construct("Invalid environment value `{$value}`, possible values are {$possibleValues}.");
19+
parent::__construct("Invalid environment [{$value}]. Possible values are {$possibleValues}.");
1820
}
1921
}

packages/core/src/FrameworkKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function event(object $event): self
223223

224224
public function registerEmergencyExceptionHandler(): self
225225
{
226-
$environment = Environment::fromEnv();
226+
$environment = Environment::guessFromEnvironment();
227227

228228
// During tests, PHPUnit registers its own error handling.
229229
if ($environment->isTesting()) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Tempest\Core\Tests;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Tempest\Container\GenericContainer;
8+
use Tempest\Core\Environment;
9+
use Tempest\Core\EnvironmentInitalizer;
10+
use Tempest\Core\EnvironmentValueWasInvalid;
11+
12+
final class EnvironmentTest extends TestCase
13+
{
14+
#[Test]
15+
public function default_is_local(): void
16+
{
17+
putenv('ENVIRONMENT=null');
18+
19+
$this->assertSame(Environment::LOCAL, Environment::guessFromEnvironment());
20+
}
21+
22+
#[Test]
23+
public function throws_on_unknown_value(): void
24+
{
25+
putenv('ENVIRONMENT=unknown');
26+
27+
$this->expectException(EnvironmentValueWasInvalid::class);
28+
29+
Environment::guessFromEnvironment();
30+
}
31+
32+
#[Test]
33+
public function can_be_resolved_from_container(): void
34+
{
35+
$container = new GenericContainer();
36+
$container->addInitializer(EnvironmentInitalizer::class);
37+
38+
putenv('ENVIRONMENT=staging');
39+
$this->assertSame(Environment::STAGING, $container->get(Environment::class));
40+
41+
// ensure it's not a singleton
42+
putenv('ENVIRONMENT=production');
43+
$this->assertSame(Environment::PRODUCTION, $container->get(Environment::class));
44+
}
45+
}

0 commit comments

Comments
 (0)