Skip to content

Commit 783610c

Browse files
committed
[DependencuInjection] Document abstract arguments
1 parent 2415e00 commit 783610c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

service_container.rst

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

843+
Abstract service arguments
844+
--------------------------
845+
846+
Sometimes, when defining services in your Symfony applications, there are arguments
847+
that can't be added in config files. The reason is that their values can only be
848+
calculated at runtime in a :doc:`compiler pass </service_container/compiler_passes>`
849+
or :doc:`bundle extension </bundles/extension>`.
850+
851+
If value is not replaced a ``RuntimeException`` would be thrown.
852+
853+
.. configuration-block::
854+
855+
.. code-block:: yaml
856+
857+
# config/services.yaml
858+
services:
859+
# ...
860+
861+
App\Service\MyService:
862+
arguments:
863+
$rootNamespace: !abstract 'should be defined by Pass'
864+
865+
# ...
866+
867+
.. code-block:: xml
868+
869+
<!-- config/services.xml -->
870+
<?xml version="1.0" encoding="UTF-8" ?>
871+
<container xmlns="http://symfony.com/schema/dic/services"
872+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
873+
xsi:schemaLocation="http://symfony.com/schema/dic/services
874+
https://symfony.com/schema/dic/services/services-1.0.xsd">
875+
876+
<services>
877+
<service id="App\Service\MyService" class="App\Service\MyService">
878+
<argument key="$rootNamespace" type="abstract">should be defined by Pass</argument>
879+
</service>
880+
881+
<!-- ... -->
882+
</services>
883+
</container>
884+
885+
.. code-block:: php
886+
887+
// config/services.php
888+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
889+
890+
use App\Service\MyService;
891+
use Psr\Log\LoggerInterface;
892+
use Symfony\Component\DependencyInjection\Definition;
893+
use Symfony\Component\DependencyInjection\Reference;
894+
895+
return function(ContainerConfigurator $container) {
896+
$services = $container->services();
897+
898+
$services->set(MyService::class)
899+
->arg('$rootNamespace', abstract_arg('should be defined by Pass'))
900+
;
901+
902+
// ...
903+
};
904+
905+
In this case, if you don't replace the value, ``RuntimeException`` will be thrown
906+
with message ``Argument "$rootNamespace" of service "App\Service\MyService" is
907+
abstract: should be defined by Pass.``
908+
843909
.. _services-autowire:
844910

845911
The autowire Option

0 commit comments

Comments
 (0)