Skip to content

Commit d75f662

Browse files
committed
minor #21048 [DependencyInjection] Document the different types of service arguments (javiereguiluz)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Document the different types of service arguments This is needed so we can configure #20167 later. Commits ------- 642c4f2 [DependencyInjection] Document the different types of service arguments
2 parents fa468e4 + 642c4f2 commit d75f662

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
@@ -323,6 +323,127 @@ type-hints by running:
323323
324324
[...]
325325
326+
In addition to injecting services, you can also pass scalar values and collections
327+
as arguments of other services:
328+
329+
.. configuration-block::
330+
331+
.. code-block:: yaml
332+
333+
# config/services.yaml
334+
services:
335+
App\Service\SomeService:
336+
arguments:
337+
# string, numeric and boolean arguments can be passed "as is"
338+
- 'Foo'
339+
- true
340+
- 7
341+
- 3.14
342+
343+
# constants can be built-in, user-defined, or Enums
344+
- !php/const E_ALL
345+
- !php/const PDO::FETCH_NUM
346+
- !php/const Symfony\Component\HttpKernel\Kernel::VERSION
347+
- !php/const App\Config\SomeEnum::SomeCase
348+
349+
# when not using autowiring, you can pass service arguments explicitly
350+
- '@some-service-id' # the leading '@' tells this is a service ID, not a string
351+
- '@?some-service-id' # using '?' means to pass null if service doesn't exist
352+
353+
# binary contents are passed encoded as base64 strings
354+
- !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
355+
356+
# collections (arrays) can include any type of argument
357+
-
358+
first: !php/const true
359+
second: 'Foo'
360+
361+
.. code-block:: xml
362+
363+
<!-- config/services.xml -->
364+
<?xml version="1.0" encoding="UTF-8" ?>
365+
<container xmlns="http://symfony.com/schema/dic/services"
366+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
367+
xmlns:framework="http://symfony.com/schema/dic/symfony"
368+
xsi:schemaLocation="http://symfony.com/schema/dic/services
369+
https://symfony.com/schema/dic/services/services-1.0.xsd
370+
http://symfony.com/schema/dic/symfony
371+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
372+
373+
<services>
374+
<service id="App\Service\SomeService">
375+
<!-- arguments without a type can be strings or numbers -->
376+
<argument>Foo</argument>
377+
<argument>7</argument>
378+
<argument>3.14</argument>
379+
<!-- explicitly declare a string argument -->
380+
<argument type="string">Foo</argument>
381+
<!-- booleans are passed as constants -->
382+
<argument type="constant">true</argument>
383+
384+
<!-- constants can be built-in, user-defined, or Enums -->
385+
<argument type="constant">E_ALL</argument>
386+
<argument type="constant">PDO::FETCH_NUM</argument>
387+
<argument type="constant">Symfony\Component\HttpKernel\Kernel::VERSION</argument>
388+
<argument type="constant">App\Config\SomeEnum::SomeCase</argument>
389+
390+
<!-- when not using autowiring, you can pass service arguments explicitly -->
391+
<argument type="service"
392+
id="some-service-id"
393+
on-invalid="dependency_injection-ignore"/>
394+
395+
<!-- binary contents are passed encoded as base64 strings -->
396+
<argument type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</argument>
397+
398+
<!-- collections (arrays) can include any type of argument -->
399+
<argument type="collection">
400+
<argument key="first" type="constant">true</argument>
401+
<argument key="second" type="string">Foo</argument>
402+
</argument>
403+
</service>
404+
405+
<!-- ... -->
406+
</services>
407+
</container>
408+
409+
.. code-block:: php
410+
411+
// config/services.php
412+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
413+
414+
use Symfony\Component\DependencyInjection\ContainerInterface;
415+
use Symfony\Component\DependencyInjection\Reference;
416+
417+
return static function (ContainerConfigurator $container) {
418+
$services = $container->services();
419+
420+
$services->set(App\Service\SomeService::class)
421+
// string, numeric and boolean arguments can be passed "as is"
422+
->arg(0, 'Foo')
423+
->arg(1, true)
424+
->arg(2, 7)
425+
->arg(3, 3.14)
426+
427+
// constants: built-in, user-defined, or Enums
428+
->arg(4, E_ALL)
429+
->arg(5, \PDO::FETCH_NUM)
430+
->arg(6, Symfony\Component\HttpKernel\Kernel::VERSION)
431+
->arg(7, App\Config\SomeEnum::SomeCase)
432+
433+
// when not using autowiring, you can pass service arguments explicitly
434+
->arg(8, service('some-service-id')) # fails if service doesn't exist
435+
# passes null if service doesn't exist
436+
->arg(9, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
437+
438+
// collection with mixed argument types
439+
->arg(10, [
440+
'first' => true,
441+
'second' => 'Foo',
442+
]);
443+
444+
// ...
445+
};
446+
326447
Handling Multiple Services
327448
~~~~~~~~~~~~~~~~~~~~~~~~~~
328449

0 commit comments

Comments
 (0)