Skip to content

[DependencyInjection] Add enum env var processor documentation #16945

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions configuration/env_var_processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,49 @@ Symfony provides the following env var processors:
],
]);

``env(enum:FooEnum:BAR)``
Tries to convert an environment variable to an actual ``\BackedEnum`` value. This processor takes the fully qualified
name of the ``\BackedEnum`` as an argument.

.. code-block:: php

# App\Enum\Environment
enum Environment: string
{
case Development = 'dev';
case Production = 'prod';
}

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
parameters:
typed_env: '%env(enum:App\Enum\Environment:APP_ENV)%'

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<parameters>
<parameter key="typed_env">%env(enum:App\Enum\Environment:APP_ENV)%</parameter>
</parameters>
</container>

.. code-block:: php

// config/services.php
$container->setParameter('typed_env', '%env(enum:App\Enum\Environment:APP_ENV)%');

It is also possible to combine any number of processors:

.. configuration-block::
Expand Down