@@ -924,6 +924,73 @@ argument for *any* service defined in this file! You can bind arguments by name
924
924
The ``bind `` config can also be applied to specific services or when loading many
925
925
services at once (i.e. :ref: `service-psr4-loader `).
926
926
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
+
927
994
.. _services-autowire :
928
995
929
996
The autowire Option
0 commit comments