-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add the EnvironmentDetector utility class and environmentdetector service
#10130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.8
Are you sure you want to change the base?
Changes from all commits
b382689
e605c53
63847ef
623a106
9783bc2
86fe9de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| use CodeIgniter\Email\Email; | ||
| use CodeIgniter\Encryption\EncrypterInterface; | ||
| use CodeIgniter\Encryption\Encryption; | ||
| use CodeIgniter\EnvironmentDetector; | ||
| use CodeIgniter\Filters\Filters; | ||
| use CodeIgniter\Format\Format; | ||
| use CodeIgniter\Honeypot\Honeypot; | ||
|
|
@@ -259,6 +260,23 @@ public static function encrypter(?EncryptionConfig $config = null, $getShared = | |
| return $encryption->initialize($config); | ||
| } | ||
|
|
||
| /** | ||
| * Provides a simple way to determine the current environment | ||
| * of the application. | ||
| * | ||
| * Primarily intended for testing environment-specific branches by | ||
| * mocking this service. Mocking it does not modify the `ENVIRONMENT` | ||
| * constant. It only affects code paths that resolve and use this service. | ||
| */ | ||
| public static function environmentdetector(?string $environment = null, bool $getShared = true): EnvironmentDetector | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add to system/Config/BaseService.php: * @method static EnvironmentDetector environmentdetector(?string $environment = null, bool $getShared = true)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also shorten (19 > 11 chars) the name to envdetector, env_detector, although a clear description is more important. |
||
| { | ||
| if ($getShared) { | ||
| return static::getSharedInstance('environmentdetector', $environment); | ||
| } | ||
|
|
||
| return new EnvironmentDetector($environment); | ||
| } | ||
|
|
||
| /** | ||
| * The Exceptions class holds the methods that handle: | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| declare(strict_types=1); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * This file is part of CodeIgniter 4 framework. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * For the full copyright and license information, please view | ||||||||||||||||||||||||||||||||||||||
| * the LICENSE file that was distributed with this source code. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| namespace CodeIgniter; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Provides a simple way to determine the current environment of the application. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
|
paulbalandan marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| * Primarily intended as a mockable seam for testing environment-specific code | ||||||||||||||||||||||||||||||||||||||
| * paths that resolve this class via the `environmentdetector` service. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * It does not redefine the `ENVIRONMENT` constant. It affects only code paths | ||||||||||||||||||||||||||||||||||||||
| * that resolve and use this class, while code that still reads `ENVIRONMENT` | ||||||||||||||||||||||||||||||||||||||
| * directly keeps its current behavior. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * For custom environment names beyond the built-in production/development/testing, | ||||||||||||||||||||||||||||||||||||||
| * use {@see self::is()}. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * @see \CodeIgniter\EnvironmentDetectorTest | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| final readonly class EnvironmentDetector | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| private string $environment; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * @param non-empty-string|null $environment The environment to use, or null to | ||||||||||||||||||||||||||||||||||||||
| * fall back to the `ENVIRONMENT` constant. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| public function __construct(?string $environment = null) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if ($environment !== null && trim($environment) === '') { | ||||||||||||||||||||||||||||||||||||||
| throw new InvalidArgumentException('Environment cannot be an empty string.'); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $this->environment = $environment !== null ? trim($environment) : ENVIRONMENT; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe skip double
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function get(): string | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->environment; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Checks if the current environment matches any of the given environments. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * @param string ...$environments One or more environment names to check against. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| public function is(string ...$environments): bool | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return in_array($this->environment, $environments, true); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function isProduction(): bool | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->is('production'); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function isDevelopment(): bool | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->is('development'); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function isTesting(): bool | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->is('testing'); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.