diff --git a/service_container.rst b/service_container.rst index 5c33d16e069..a4abf4ce2f7 100644 --- a/service_container.rst +++ b/service_container.rst @@ -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 ` +or :doc:`bundle 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 + + + + + + + + should be defined by Pass + + + + + + + .. 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