Skip to content

Commit 61b1554

Browse files
committed
add docs
1 parent 1ea843f commit 61b1554

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ Others
256256

257257
- **Float and Double Casting:** Added support for precision and rounding mode when casting to float or double in entities.
258258
- Float and Double casting now throws ``CastException::forInvalidFloatRoundingMode()`` if an rounding mode other than up, down, even or odd is provided.
259+
- **Environment:** Added ``CodeIgniter\EnvironmentDetector`` class and corresponding ``environmentdetector`` service as a mockable wrapper around the ``ENVIRONMENT`` constant.
260+
Framework internals that previously compared ``ENVIRONMENT`` directly now go through this service, making environment-specific branches reachable in tests via ``Services::injectMock()``. See :ref:`environment-detector-service`.
259261

260262
***************
261263
Message Changes

user_guide_src/source/general/environments.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ You can also check the current environment by ``spark env`` command:
145145
146146
php spark env
147147
148+
.. _environment-detector-service:
149+
150+
The ``environmentdetector`` service
151+
===================================
152+
153+
.. versionadded:: 4.8.0
154+
155+
As an alternative to reading the ``ENVIRONMENT`` constant directly, CodeIgniter
156+
provides the ``environmentdetector`` service, backed by the
157+
:php:class:`CodeIgniter\\EnvironmentDetector` class. Because it is a shared
158+
service, it can be mocked in tests (via ``Services::injectMock()``) to exercise
159+
environment-specific branches without having to redefine the ``ENVIRONMENT``
160+
constant.
161+
162+
.. literalinclude:: environments/001.php
163+
164+
Passing a value to the constructor overrides the detected environment; passing
165+
``null`` (the default) falls back to the ``ENVIRONMENT`` constant. An empty or
166+
whitespace-only string throws ``CodeIgniter\Exceptions\InvalidArgumentException``.
167+
148168
*************************************
149169
Effects on Default Framework Behavior
150170
*************************************
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$env = service('environmentdetector');
4+
5+
// Get the current environment name.
6+
echo $env->get();
7+
8+
// Check against the three built-in environments.
9+
$env->isProduction();
10+
$env->isDevelopment();
11+
$env->isTesting();
12+
13+
// Match any one of several environments (useful for custom names).
14+
$env->is('production', 'staging');

0 commit comments

Comments
 (0)