Skip to content

Commit 9ceba53

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: [DependencyInjection] Document the different types of service arguments
2 parents 9d4e46f + c50a5f8 commit 9ceba53

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

service_container.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,127 @@ type-hints by running:
386386
387387
[...]
388388
389+
In addition to injecting services, you can also pass scalar values and collections
390+
as arguments of other services:
391+
392+
.. configuration-block::
393+
394+
.. code-block:: yaml
395+
396+
# config/services.yaml
397+
services:
398+
App\Service\SomeService:
399+
arguments:
400+
# string, numeric and boolean arguments can be passed "as is"
401+
- 'Foo'
402+
- true
403+
- 7
404+
- 3.14
405+
406+
# constants can be built-in, user-defined, or Enums
407+
- !php/const E_ALL
408+
- !php/const PDO::FETCH_NUM
409+
- !php/const Symfony\Component\HttpKernel\Kernel::VERSION
410+
- !php/const App\Config\SomeEnum::SomeCase
411+
412+
# when not using autowiring, you can pass service arguments explicitly
413+
- '@some-service-id' # the leading '@' tells this is a service ID, not a string
414+
- '@?some-service-id' # using '?' means to pass null if service doesn't exist
415+
416+
# binary contents are passed encoded as base64 strings
417+
- !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
418+
419+
# collections (arrays) can include any type of argument
420+
-
421+
first: !php/const true
422+
second: 'Foo'
423+
424+
.. code-block:: xml
425+
426+
<!-- config/services.xml -->
427+
<?xml version="1.0" encoding="UTF-8" ?>
428+
<container xmlns="http://symfony.com/schema/dic/services"
429+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
430+
xmlns:framework="http://symfony.com/schema/dic/symfony"
431+
xsi:schemaLocation="http://symfony.com/schema/dic/services
432+
https://symfony.com/schema/dic/services/services-1.0.xsd
433+
http://symfony.com/schema/dic/symfony
434+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
435+
436+
<services>
437+
<service id="App\Service\SomeService">
438+
<!-- arguments without a type can be strings or numbers -->
439+
<argument>Foo</argument>
440+
<argument>7</argument>
441+
<argument>3.14</argument>
442+
<!-- explicitly declare a string argument -->
443+
<argument type="string">Foo</argument>
444+
<!-- booleans are passed as constants -->
445+
<argument type="constant">true</argument>
446+
447+
<!-- constants can be built-in, user-defined, or Enums -->
448+
<argument type="constant">E_ALL</argument>
449+
<argument type="constant">PDO::FETCH_NUM</argument>
450+
<argument type="constant">Symfony\Component\HttpKernel\Kernel::VERSION</argument>
451+
<argument type="constant">App\Config\SomeEnum::SomeCase</argument>
452+
453+
<!-- when not using autowiring, you can pass service arguments explicitly -->
454+
<argument type="service"
455+
id="some-service-id"
456+
on-invalid="dependency_injection-ignore"/>
457+
458+
<!-- binary contents are passed encoded as base64 strings -->
459+
<argument type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</argument>
460+
461+
<!-- collections (arrays) can include any type of argument -->
462+
<argument type="collection">
463+
<argument key="first" type="constant">true</argument>
464+
<argument key="second" type="string">Foo</argument>
465+
</argument>
466+
</service>
467+
468+
<!-- ... -->
469+
</services>
470+
</container>
471+
472+
.. code-block:: php
473+
474+
// config/services.php
475+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
476+
477+
use Symfony\Component\DependencyInjection\ContainerInterface;
478+
use Symfony\Component\DependencyInjection\Reference;
479+
480+
return static function (ContainerConfigurator $container) {
481+
$services = $container->services();
482+
483+
$services->set(App\Service\SomeService::class)
484+
// string, numeric and boolean arguments can be passed "as is"
485+
->arg(0, 'Foo')
486+
->arg(1, true)
487+
->arg(2, 7)
488+
->arg(3, 3.14)
489+
490+
// constants: built-in, user-defined, or Enums
491+
->arg(4, E_ALL)
492+
->arg(5, \PDO::FETCH_NUM)
493+
->arg(6, Symfony\Component\HttpKernel\Kernel::VERSION)
494+
->arg(7, App\Config\SomeEnum::SomeCase)
495+
496+
// when not using autowiring, you can pass service arguments explicitly
497+
->arg(8, service('some-service-id')) # fails if service doesn't exist
498+
# passes null if service doesn't exist
499+
->arg(9, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
500+
501+
// collection with mixed argument types
502+
->arg(10, [
503+
'first' => true,
504+
'second' => 'Foo',
505+
]);
506+
507+
// ...
508+
};
509+
389510
Handling Multiple Services
390511
~~~~~~~~~~~~~~~~~~~~~~~~~~
391512

0 commit comments

Comments
 (0)