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