8000 Merge branch '5.4' into 6.3 · symfony/symfony-docs@42c04a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42c04a3

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [DependencuInjection] Document abstract arguments
2 parents 5a9b3ce + 7f314f5 commit 42c04a3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

service_container.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,77 @@ argument for *any* service defined in this file! You can bind arguments by name
924924
The ``bind`` config can also be applied to specific services or when loading many
925925
services at once (i.e. :ref:`service-psr4-loader`).
926926

927+
Abstract Service Arguments
928+
--------------------------
929+
930+
Sometimes, the values of some service arguments can't be defined in the
931+
configuration files because they are calculated at runtime using a
932+
:doc:`compiler pass </service_container/compiler_passes>`
933+
or :doc:`bundle extension </bundles/extension>`.
934+
935+
In those cases, you can use the ``abstract`` argument type to define at least
936+
the name of the argument and some short description about its purpose:
937+
938+
.. configuration-block::
939+
940+
.. code-block:: yaml
941+
942+
# config/services.yaml
943+
services:
944+
# ...
945+
946+
App\Service\MyService:
947+
arguments:
948+
$rootNamespace: !abstract 'should be defined by Pass'
949+
950+
# ...
951+
952+
.. code-block:: xml
953+
954+
<!-- config/services.xml -->
955+
<?xml version="1.0" encoding="UTF-8" ?>
956+
<container xmlns="http://symfony.com/schema/dic/services"
957+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
958+
xsi:schemaLocation="http://symfony.com/schema/dic/services
959+
https://symfony.com/schema/dic/services/services-1.0.xsd">
960+
961+
<services>
962+
<service id="App\Service\MyService" class="App\Service\MyService">
963+
<argument key="$rootNamespace" type="abstract">should be defined by Pass</argument>
964+
</service>
965+
966+
<!-- ... -->
967+
</services>
968+
</container>
969+
970+
.. code-block:: php
971+
972+
// config/services.php
973+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
974+
975+
use App\Service\MyService;
976+
use Psr\Log\LoggerInterface;
977+
use Symfony\Component\DependencyInjection\Definition;
978+
use Symfony\Component\DependencyInjection\Reference;
979+
980+
return function(ContainerConfigurator $container) {
981+
$services = $container->services();
982+
983+
$services->set(MyService::class)
984+
->arg('$rootNamespace', abstract_arg('should be defined by Pass'))
985+
;
986+
987+
// ...
988+
};
989+
990+
If you don't replace the value of an abstract argument during runtime, a
991+
``RuntimeException`` will be thrown with a message like
992+
``Argument "$rootNamespace" of service "App\Service\MyService" is abstract: should be defined by Pass.``
993+
994+
.. versionadded:: 5.1
995+
996+
The abstract service arguments were introduced in Symfony 5.1.
997+
927998
.. _services-autowire:
928999

9291000
The autowire Option

0 commit comments

Comments
 (0)
0