|
| 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 | +} |
0 commit comments