Skip to content

[DependencyInjection] Document abstract arguments #18633

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
66 changes: 66 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,72 @@ argument for *any* service defined in this file! You can bind arguments by name
The ``bind`` config can also be applied to specific services or when loading many
services at once (i.e. :ref:`service-psr4-loader`).

Abstract service arguments
--------------------------

Sometimes, when defining services in your Symfony applications, there are arguments
that can't be added in config files. The reason is that their values can only be
calculated at runtime in a :doc:`compiler pass </service_container/compiler_passes>`
or :doc:`bundle extension </bundles/extension>`.

If value is not replaced a ``RuntimeException`` would be thrown.

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# ...

App\Service\MyService:
arguments:
$rootNamespace: !abstract 'should be defined by Pass'

# ...

.. 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\Service\MyService" class="App\Service\MyService">
<argument key="$rootNamespace" type="abstract">should be defined by Pass</argument>
</service>

<!-- ... -->
</services>
</container>

.. code-block:: php

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

use App\Service\MyService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

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

$services->set(MyService::class)
->arg('$rootNamespace', abstract_arg('should be defined by Pass'))
;

// ...
};

In this case, if you don't replace the value, ``RuntimeException`` will be thrown
with message ``Argument "$rootNamespace" of service "App\Service\MyService" is
abstract: should be defined by Pass.``

.. _services-autowire:

The autowire Option
Expand Down