Skip to content

feat: add the EnvironmentDetector utility class and environmentdetector service#10130

Open
paulbalandan wants to merge 6 commits intocodeigniter4:4.8from
paulbalandan:environment-detector
Open

feat: add the EnvironmentDetector utility class and environmentdetector service#10130
paulbalandan wants to merge 6 commits intocodeigniter4:4.8from
paulbalandan:environment-detector

Conversation

@paulbalandan
Copy link
Copy Markdown
Member

Description
This PR adds the EnvironmentDetector utility class as a simple mockable wrapper to accessing the ENVIRONMENT constant, allowing us to test previously unreachable code branches dependent on the current environment.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Apr 23, 2026
Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good change.

I added some suggestions to the phpdocs / user guide.

Comment thread system/Config/Services.php
Comment thread system/EnvironmentDetector.php
Comment thread user_guide_src/source/general/environments.rst
@paulbalandan paulbalandan force-pushed the environment-detector branch from 7231481 to 86fe9de Compare April 23, 2026 15:27
Comment on lines +41 to +48
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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe skip double trim()?

Suggested change
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;
}
public function __construct(?string $environment = null)
{
$environment = $environment !== null ? trim($environment) : ENVIRONMENT;
if ($environment === '') {
throw new InvalidArgumentException('Environment cannot be an empty string.');
}
$this->environment = $environment;
}


As an alternative to reading the ``ENVIRONMENT`` constant directly, CodeIgniter
provides the ``environmentdetector`` service, backed by the
:php:class:`CodeIgniter\\EnvironmentDetector` class. Because it is a shared
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that php:class is used to describe classes and provide links to their descriptions. In this case, it looks like an ordinary code mention and you can skip it.

In general, what was the primary point of describing classes and functions in documentation in a special style? It looks clumsy. If you follow the style, you need to manually perform the functions of phpDocumentor. Due to the possible migration from Sphinx, this has added to the problems.

* 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just environment? it would read more naturally as service('environment')->isProduction() etc.

the class name EnvironmentDetector was chosen because it may clash with the env command's class when importing in IDE.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it might be worth adding everything related to ENV to the service. And it will replace or complement the built-in DotEnv... At the moment, the "detector" clearly indicates its role.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll just rename to envdetector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants