From 310c46ca1b6bf0ba3b00e7d3f395ceb1d56410a4 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 4 Jun 2025 16:54:16 +0200 Subject: [PATCH 1/4] [DependencyInjection] Document the different types of service arguments --- service_container.rst | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/service_container.rst b/service_container.rst index c1f5dc7b5f4..98f5b7b608f 100644 --- a/service_container.rst +++ b/service_container.rst @@ -323,6 +323,104 @@ type-hints by running: [...] +In addition to injecting services, you can also pass scalar values and collections +as arguments of other services: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + App\Service\SomeService: + arguments: + # arguments without a type are treated as strings + - 'Foo' + # explicitly declare a string argument + - !str 'Foo' + # constants can be built-in, user-defined, or Enums + - !php/const true + - !php/const E_ALL + - !php/const PDO::FETCH_NUM + - !php/const App\Service\AnotherService::SOME_CONSTANT + - !php/const App\Config\SomeEnum::SomeCase + # when not using autowiring, you can pass service arguments explicitly + - '@some-service-id' # the leading '@' tells this is a service ID, not a string + - '@?some-service-id' # using '?' means to pass null if service doesn't exist + # collections (arrays) can include any type of argument + - + first: !php/const true + second: 'Foo' + + .. code-block:: xml + + + + + + + + + Foo + + Foo + + true + E_ALL + PDO::FETCH_NUM + App\Service\AnotherService::SOME_CONSTANT + App\Config\SomeEnum::SomeCase + + + + + true + Foo + + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use Symfony\Component\DependencyInjection\ContainerInterface; + use Symfony\Component\DependencyInjection\Reference; + + return static function (ContainerConfigurator $container) { + $services = $container->services(); + + $services->set(App\Service\SomeService::class) + // string arguments + ->arg(0, 'Foo') + // constants: built-in, user-defined, or Enums + ->arg(1, true) + ->arg(2, E_ALL) + ->arg(3, \PDO::FETCH_NUM) + ->arg(4, \App\Service\AnotherService::SOME_CONSTANT) + ->arg(5, \App\Config\SomeEnum::SomeCase) + // explicit service reference with on-invalid behavior + ->arg(6, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE)) + // collection with mixed argument types + ->arg(7, [ + 'first' => true, + 'second' => 'Foo', + ]); + + // ... + }; + Handling Multiple Services ~~~~~~~~~~~~~~~~~~~~~~~~~~ From f1f43a743b2c1d81d48c8f114816869fac46e735 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 4 Jun 2025 16:58:05 +0200 Subject: [PATCH 2/4] - --- service_container.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service_container.rst b/service_container.rst index 98f5b7b608f..835d52b436c 100644 --- a/service_container.rst +++ b/service_container.rst @@ -379,7 +379,7 @@ as arguments of other services: + on-invalid="dependency_injection-ignore"/> true From 1f682ca9450ae9c49f913849c64b9e0e6a00d30e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 4 Jun 2025 17:12:29 +0200 Subject: [PATCH 3/4] - --- service_container.rst | 50 ++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/service_container.rst b/service_container.rst index 835d52b436c..014931da826 100644 --- a/service_container.rst +++ b/service_container.rst @@ -334,19 +334,26 @@ as arguments of other services: services: App\Service\SomeService: arguments: - # arguments without a type are treated as strings + # string, numeric and boolean arguments can be passed "as is" - 'Foo' - # explicitly declare a string argument - - !str 'Foo' + - true + - 7 + - 3.14 + # constants can be built-in, user-defined, or Enums - !php/const true - !php/const E_ALL - !php/const PDO::FETCH_NUM - !php/const App\Service\AnotherService::SOME_CONSTANT - !php/const App\Config\SomeEnum::SomeCase + # when not using autowiring, you can pass service arguments explicitly - '@some-service-id' # the leading '@' tells this is a service ID, not a string - '@?some-service-id' # using '?' means to pass null if service doesn't exist + + # binary contents are passed encoded as base64 strings + - !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH + # collections (arrays) can include any type of argument - first: !php/const true @@ -366,20 +373,30 @@ as arguments of other services: - + Foo + 7 + 3.14 Foo + + true + true E_ALL PDO::FETCH_NUM App\Service\AnotherService::SOME_CONSTANT App\Config\SomeEnum::SomeCase + + + + VGhpcyBpcyBhIEJlbGwgY2hhciAH + true @@ -402,18 +419,25 @@ as arguments of other services: $services = $container->services(); $services->set(App\Service\SomeService::class) - // string arguments + // string, numeric and boolean arguments can be passed "as is" ->arg(0, 'Foo') - // constants: built-in, user-defined, or Enums ->arg(1, true) - ->arg(2, E_ALL) - ->arg(3, \PDO::FETCH_NUM) - ->arg(4, \App\Service\AnotherService::SOME_CONSTANT) - ->arg(5, \App\Config\SomeEnum::SomeCase) - // explicit service reference with on-invalid behavior - ->arg(6, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE)) + ->arg(2, 7) + ->arg(3, 3.14) + + // constants: built-in, user-defined, or Enums + ->arg(4, E_ALL) + ->arg(5, \PDO::FETCH_NUM) + ->arg(6, \App\Service\AnotherService::SOME_CONSTANT) + ->arg(7, \App\Config\SomeEnum::SomeCase) + + // when not using autowiring, you can pass service arguments explicitly + ->arg(8, service('some-service-id')) # fails if service doesn't exist + # passes null if service doesn't exist + ->arg(9, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE)) + // collection with mixed argument types - ->arg(7, [ + ->arg(10, [ 'first' => true, 'second' => 'Foo', ]); From 1d11816ffada596ae92a99a752d43511708e48cc Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 5 Jun 2025 08:18:17 +0200 Subject: [PATCH 4/4] Fix issues --- .doctor-rst.yaml | 1 + service_container.rst | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.doctor-rst.yaml b/.doctor-rst.yaml index 2f0d5852c7f..132bc39d689 100644 --- a/.doctor-rst.yaml +++ b/.doctor-rst.yaml @@ -122,3 +122,4 @@ whitelist: - 'End to End Tests (E2E)' - '.. versionadded:: 2.2.0' # Panther - '* Inline code blocks use double-ticks (````like this````).' + - '- !php/const App\Service\AnotherService::SOME_CONSTANT' diff --git a/service_container.rst b/service_container.rst index 014931da826..645717aecb4 100644 --- a/service_container.rst +++ b/service_container.rst @@ -341,7 +341,6 @@ as arguments of other services: - 3.14 # constants can be built-in, user-defined, or Enums - - !php/const true - !php/const E_ALL - !php/const PDO::FETCH_NUM - !php/const App\Service\AnotherService::SOME_CONSTANT @@ -383,7 +382,6 @@ as arguments of other services: true - true E_ALL PDO::FETCH_NUM App\Service\AnotherService::SOME_CONSTANT @@ -406,6 +404,7 @@ as arguments of other services: + .. code-block:: php @@ -428,8 +427,8 @@ as arguments of other services: // constants: built-in, user-defined, or Enums ->arg(4, E_ALL) ->arg(5, \PDO::FETCH_NUM) - ->arg(6, \App\Service\AnotherService::SOME_CONSTANT) - ->arg(7, \App\Config\SomeEnum::SomeCase) + ->arg(6, App\Service\AnotherService::SOME_CONSTANT) + ->arg(7, App\Config\SomeEnum::SomeCase) // when not using autowiring, you can pass service arguments explicitly ->arg(8, service('some-service-id')) # fails if service doesn't exist