Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Config] [Configuration] Add enum case as service parameter example #17910

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
14 changes: 14 additions & 0 deletions configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ reusable configuration value. By convention, parameters are defined under the
app.some_constant: !php/const GLOBAL_CONSTANT
app.another_constant: !php/const App\Entity\BlogPost::MAX_ITEMS

# Enum case as parameter values
app.some_enum: !php/enum App\Enum\PostState::Published

# ...

.. code-block:: xml
Expand Down Expand Up @@ -226,6 +229,9 @@ reusable configuration value. By convention, parameters are defined under the
<!-- PHP constants as parameter values -->
<parameter key="app.some_constant" type="constant">GLOBAL_CONSTANT</parameter>
<parameter key="app.another_constant" type="constant">App\Entity\BlogPost::MAX_ITEMS</parameter>

<!-- Enum case as parameter values -->
<parameter key="app.some_enum" type="enum">App\Enum\PostState::Published</parameter>
</parameters>

<!-- ... -->
Expand All @@ -237,6 +243,7 @@ reusable configuration value. By convention, parameters are defined under the
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Entity\BlogPost;
use App\Enum\PostState;

return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->parameters()
Expand All @@ -256,6 +263,9 @@ reusable configuration value. By convention, parameters are defined under the
// PHP constants as parameter values
->set('app.some_constant', GLOBAL_CONSTANT)
->set('app.another_constant', BlogPost::MAX_ITEMS);

// Enum case as parameter values
->set('app.some_enum', PostState::Published);
};

// ...
Expand All @@ -272,6 +282,10 @@ reusable configuration value. By convention, parameters are defined under the
[email protected]
</parameter>

.. versionadded:: 6.2

Passing an enum case as a service parameter was introduced in Symfony 6.2.

Once defined, you can reference this parameter value from any other
configuration file using a special syntax: wrap the parameter name in two ``%``
(e.g. ``%app.admin_email%``):
Expand Down