Skip to content

Commit 57deb81

Browse files
committed
Centralize runtime environment checks
1 parent a9f9e0a commit 57deb81

11 files changed

Lines changed: 321 additions & 69 deletions

File tree

src/Console/Output/GithubActionOutput.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace FastForward\DevTools\Console\Output;
2121

22-
use FastForward\DevTools\Environment\EnvironmentInterface;
22+
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
2323
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2424

2525
/**
@@ -35,11 +35,11 @@ final class GithubActionOutput
3535

3636
/**
3737
* @param ConsoleOutputInterface $output the console output used to emit workflow commands
38-
* @param EnvironmentInterface $environment reads runtime environment flags
38+
* @param RuntimeEnvironmentInterface $environment resolves runtime environment capabilities
3939
*/
4040
public function __construct(
4141
private readonly ConsoleOutputInterface $output,
42-
private readonly EnvironmentInterface $environment,
42+
private readonly RuntimeEnvironmentInterface $environment,
4343
) {}
4444

4545
/**
@@ -247,22 +247,8 @@ private function emit(string $command, string $message = '', array $properties =
247247
*/
248248
private function supportsWorkflowCommands(): bool
249249
{
250-
return $this->isTruthyEnvironmentFlag('GITHUB_ACTIONS')
251-
&& ! $this->isTruthyEnvironmentFlag('COMPOSER_TESTS_ARE_RUNNING');
252-
}
253-
254-
/**
255-
* Determines whether an environment flag is set to a truthy value.
256-
*
257-
* @param string $name the environment variable name
258-
*
259-
* @return bool true when the environment variable is truthy
260-
*/
261-
private function isTruthyEnvironmentFlag(string $name): bool
262-
{
263-
$value = $this->environment->get($name, '');
264-
265-
return null !== $value && '' !== $value && '0' !== $value;
250+
return $this->environment->isGithubActions()
251+
&& ! $this->environment->isComposerTestRun();
266252
}
267253

268254
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Fast Forward Development Tools for PHP projects.
7+
*
8+
* This file is part of fast-forward/dev-tools project.
9+
*
10+
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
11+
* @license https://opensource.org/licenses/MIT MIT License
12+
*
13+
* @see https://github.com/php-fast-forward/
14+
* @see https://github.com/php-fast-forward/dev-tools
15+
* @see https://github.com/php-fast-forward/dev-tools/issues
16+
* @see https://php-fast-forward.github.io/dev-tools/
17+
* @see https://datatracker.ietf.org/doc/html/rfc2119
18+
*/
19+
20+
namespace FastForward\DevTools\Environment;
21+
22+
/**
23+
* Resolves common runtime-environment flags used by DevTools integrations.
24+
*/
25+
final readonly class RuntimeEnvironment implements RuntimeEnvironmentInterface
26+
{
27+
/**
28+
* @param EnvironmentInterface $environment reads raw process environment variables
29+
*/
30+
public function __construct(
31+
private EnvironmentInterface $environment,
32+
) {}
33+
34+
/**
35+
* Returns whether a truthy environment flag is enabled.
36+
*
37+
* @param string $name the environment variable name
38+
*/
39+
public function isEnabled(string $name): bool
40+
{
41+
return \in_array(strtolower((string) $this->environment->get($name, '')), ['1', 'true', 'yes', 'on'], true);
42+
}
43+
44+
/**
45+
* Returns whether the current process runs in GitHub Actions.
46+
*/
47+
public function isGithubActions(): bool
48+
{
49+
return $this->isEnabled('GITHUB_ACTIONS');
50+
}
51+
52+
/**
53+
* Returns whether the current process runs in a CI environment.
54+
*/
55+
public function isCi(): bool
56+
{
57+
if ($this->isGithubActions()) {
58+
return true;
59+
}
60+
61+
return $this->isEnabled('CI');
62+
}
63+
64+
/**
65+
* Returns whether the Composer test suite runtime flag is enabled.
66+
*/
67+
public function isComposerTestRun(): bool
68+
{
69+
return $this->isEnabled('COMPOSER_TESTS_ARE_RUNNING');
70+
}
71+
}
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+
* Fast Forward Development Tools for PHP projects.
7+
*
8+
* This file is part of fast-forward/dev-tools project.
9+
*
10+
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
11+
* @license https://opensource.org/licenses/MIT MIT License
12+
*
13+
* @see https://github.com/php-fast-forward/
14+
* @see https://github.com/php-fast-forward/dev-tools
15+
* @see https://github.com/php-fast-forward/dev-tools/issues
16+
* @see https://php-fast-forward.github.io/dev-tools/
17+
* @see https://datatracker.ietf.org/doc/html/rfc2119
18+
*/
19+
20+
namespace FastForward\DevTools\Environment;
21+
22+
/**
23+
* Answers common runtime-environment questions from process environment flags.
24+
*/
25+
interface RuntimeEnvironmentInterface
26+
{
27+
/**
28+
* Returns whether a truthy environment flag is enabled.
29+
*
30+
* @param string $name the environment variable name
31+
*
32+
* @return bool true when the environment variable is enabled
33+
*/
34+
public function isEnabled(string $name): bool;
35+
36+
/**
37+
* Returns whether the current process runs in GitHub Actions.
38+
*/
39+
public function isGithubActions(): bool;
40+
41+
/**
42+
* Returns whether the current process runs in a CI environment.
43+
*/
44+
public function isCi(): bool;
45+
46+
/**
47+
* Returns whether the Composer test suite runtime flag is enabled.
48+
*/
49+
public function isComposerTestRun(): bool;
50+
}

src/Process/ProcessQueue.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Closure;
2323
use FastForward\DevTools\Console\Output\GithubActionOutput;
2424
use FastForward\DevTools\Console\Output\OutputCapabilityDetectorInterface;
25-
use FastForward\DevTools\Environment\EnvironmentInterface;
25+
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
2626
use ReflectionProperty;
2727
use Symfony\Component\Console\Input\ArrayInput;
2828
use Symfony\Component\Console\Output\ConsoleOutputInterface;
@@ -73,13 +73,13 @@ final class ProcessQueue implements ProcessQueueInterface
7373
/**
7474
* @param GithubActionOutput $githubActionOutput wraps grouped queue output in GitHub Actions logs when supported
7575
* @param ProcessEnvironmentConfiguratorInterface $environmentConfigurator
76-
* @param EnvironmentInterface $environment reads runtime environment flags
76+
* @param RuntimeEnvironmentInterface $environment resolves runtime environment capabilities
7777
* @param OutputCapabilityDetectorInterface $outputCapabilityDetector detects ANSI-capable output
7878
*/
7979
public function __construct(
8080
private readonly GithubActionOutput $githubActionOutput,
8181
private readonly ProcessEnvironmentConfiguratorInterface $environmentConfigurator,
82-
private readonly EnvironmentInterface $environment,
82+
private readonly RuntimeEnvironmentInterface $environment,
8383
private readonly OutputCapabilityDetectorInterface $outputCapabilityDetector,
8484
) {}
8585

@@ -404,7 +404,7 @@ private function runInOutputSection(string $label, OutputInterface $output, Clos
404404
private function shouldRenderLocalSection(OutputInterface $output): bool
405405
{
406406
return $this->outputCapabilityDetector->supportsAnsi($output)
407-
&& null === $this->environment->get('GITHUB_ACTIONS');
407+
&& ! $this->environment->isGithubActions();
408408
}
409409

410410
/**

src/SelfUpdate/VersionCheckNotifier.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace FastForward\DevTools\SelfUpdate;
2121

22-
use FastForward\DevTools\Environment\EnvironmentInterface;
22+
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
2323
use Throwable;
2424
use Symfony\Component\Console\Output\OutputInterface;
2525

@@ -30,11 +30,11 @@
3030
{
3131
/**
3232
* @param VersionCheckerInterface $versionChecker the checker used to resolve latest release metadata
33-
* @param EnvironmentInterface $environment the environment reader used to skip non-interactive CI checks
33+
* @param RuntimeEnvironmentInterface $environment resolves runtime environment capabilities
3434
*/
3535
public function __construct(
3636
private VersionCheckerInterface $versionChecker,
37-
private EnvironmentInterface $environment,
37+
private RuntimeEnvironmentInterface $environment,
3838
) {}
3939

4040
/**
@@ -70,22 +70,10 @@ public function notify(OutputInterface $output): void
7070
*/
7171
private function shouldSkipVersionCheck(): bool
7272
{
73-
foreach (['FAST_FORWARD_SKIP_VERSION_CHECK', 'GITHUB_ACTIONS', 'CI'] as $name) {
74-
if ($this->isTruthy($this->environment->get($name, ''))) {
75-
return true;
76-
}
73+
if ($this->environment->isCi()) {
74+
return true;
7775
}
7876

79-
return false;
80-
}
81-
82-
/**
83-
* Returns whether an environment value represents an enabled flag.
84-
*
85-
* @param string|null $value the environment value to inspect
86-
*/
87-
private function isTruthy(?string $value): bool
88-
{
89-
return \in_array(strtolower((string) $value), ['1', 'true', 'yes', 'on'], true);
77+
return $this->environment->isEnabled('FAST_FORWARD_SKIP_VERSION_CHECK');
9078
}
9179
}

src/ServiceProvider/DevToolsServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
use FastForward\DevTools\Console\Output\OutputCapabilityDetectorInterface;
4646
use FastForward\DevTools\Environment\Environment;
4747
use FastForward\DevTools\Environment\EnvironmentInterface;
48+
use FastForward\DevTools\Environment\RuntimeEnvironment;
49+
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
4850
use FastForward\DevTools\Filesystem\FinderFactory;
4951
use FastForward\DevTools\Filesystem\FinderFactoryInterface;
5052
use FastForward\DevTools\Filesystem\Filesystem;
@@ -132,6 +134,7 @@ public function getFactories(): array
132134
return [
133135
// Process
134136
EnvironmentInterface::class => get(Environment::class),
137+
RuntimeEnvironmentInterface::class => get(RuntimeEnvironment::class),
135138
ExtensionInterface::class => get(Extension::class),
136139
OutputCapabilityDetectorInterface::class => get(OutputCapabilityDetector::class),
137140
ProcessBuilderInterface::class => get(ProcessBuilder::class),
@@ -186,7 +189,7 @@ public function getFactories(): array
186189
->method('setFormatter', get(LogLevelOutputFormatter::class)),
187190
GithubActionOutput::class => create(GithubActionOutput::class)->constructor(
188191
get(ConsoleOutputInterface::class),
189-
get(EnvironmentInterface::class)
192+
get(RuntimeEnvironmentInterface::class)
190193
),
191194
ContextProcessorInterface::class => create(CompositeContextProcessor::class)->constructor([
192195
get(CommandInputProcessor::class),

tests/Console/DevToolsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use FastForward\DevTools\Console\Formatter\LogLevelOutputFormatter;
2626
use FastForward\DevTools\Console\Output\GithubActionOutput;
2727
use FastForward\DevTools\Environment\EnvironmentInterface;
28+
use FastForward\DevTools\Environment\RuntimeEnvironment;
2829
use FastForward\DevTools\Filesystem\FinderFactory;
2930
use FastForward\DevTools\Path\DevToolsPathResolver;
3031
use FastForward\DevTools\Path\WorkingProjectPathResolver;
@@ -70,6 +71,7 @@
7071
#[UsesClass(ClassReflection::class)]
7172
#[UsesClass(LogLevelOutputFormatter::class)]
7273
#[UsesClass(GithubActionOutput::class)]
74+
#[UsesClass(RuntimeEnvironment::class)]
7375
#[UsesClass(ColorPreservingProcessEnvironmentConfigurator::class)]
7476
#[UsesClass(CompositeProcessEnvironmentConfigurator::class)]
7577
#[UsesClass(ProcessBuilder::class)]

tests/Console/Logger/OutputFormatLoggerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use FastForward\DevTools\Console\Logger\Processor\CommandOutputProcessor;
2828
use FastForward\DevTools\Console\Logger\Processor\CompositeContextProcessor;
2929
use FastForward\DevTools\Console\Output\GithubActionOutput;
30-
use FastForward\DevTools\Environment\EnvironmentInterface;
30+
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
3131
use PHPUnit\Framework\Attributes\CoversClass;
3232
use PHPUnit\Framework\Attributes\Test;
3333
use PHPUnit\Framework\Attributes\UsesClass;
@@ -59,7 +59,7 @@ final class OutputFormatLoggerTest extends TestCase
5959
private ObjectProphecy $clock;
6060

6161
/**
62-
* @var ObjectProphecy<EnvironmentInterface>
62+
* @var ObjectProphecy<RuntimeEnvironmentInterface>
6363
*/
6464
private ObjectProphecy $environment;
6565

@@ -83,9 +83,9 @@ protected function setUp(): void
8383
$this->output = $this->prophesize(ConsoleOutputInterface::class);
8484
$this->errorOutput = $this->prophesize(OutputInterface::class);
8585
$this->clock = $this->prophesize(ClockInterface::class);
86-
$this->environment = $this->prophesize(EnvironmentInterface::class);
87-
$this->environment->get(Argument::type('string'), Argument::cetera())
88-
->willReturn(null);
86+
$this->environment = $this->prophesize(RuntimeEnvironmentInterface::class);
87+
$this->environment->isGithubActions()
88+
->willReturn(false);
8989

9090
$this->output->getErrorOutput()
9191
->willReturn($this->errorOutput->reveal());

0 commit comments

Comments
 (0)