Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add documentation for using expressions as service factories#16516

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
wouterj merged 1 commit intosymfony:6.1fromjvasseur:expr-factory
May 1, 2022
Merged
Changes fromall 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
51 changes: 51 additions & 0 deletionsservice_container/expression_language.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -125,3 +125,54 @@ via a ``container`` variable. Here's another example:

Expressions can be used in ``arguments``, ``properties``, as arguments with
``configurator`` and as arguments to ``calls`` (method calls).

You can also use expressions as service factories:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
App\Mailer:
factory: "@=parameter('some_param') ? service('some_service') : arg(0)"
arguments:
- '@some_other_service'

.. 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"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="App\Mailer">
<factory expression="parameter('some_param') ? service('some_service') : arg(0)"/>
<argument type="service" id="some_other_service"/>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Mailer;

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set(Mailer::class)
->factory(expr("parameter('some_param') ? service('some_service') : arg(0)"))
->args([service('some_other_service')]);
};

In this context, you have access to the ``arg`` function that allows getting the value of arguments passed to the factory.

.. versionadded:: 6.1

Using expressions as factories was introduced in Symfony 6.1.

[8]ページ先頭

©2009-2025 Movatter.jp