Skip to content

Commit 310c46c

Browse files
committed
[DependencyInjection] Document the different types of service arguments
1 parent f5af7bd commit 310c46c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

service_container.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,104 @@ 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+
# arguments without a type are treated as strings
338+
- 'Foo'
339+
# explicitly declare a string argument
340+
- !str 'Foo'
341+
# constants can be built-in, user-defined, or Enums
342+
- !php/const true
343+
- !php/const E_ALL
344+
- !php/const PDO::FETCH_NUM
345+
- !php/const App\Service\AnotherService::SOME_CONSTANT
346+
- !php/const App\Config\SomeEnum::SomeCase
347+
# when not using autowiring, you can pass service arguments explicitly
348+
- '@some-service-id' # the leading '@' tells this is a service ID, not a string
349+
- '@?some-service-id' # using '?' means to pass null if service doesn't exist
350+
# collections (arrays) can include any type of argument
351+
-
352+
first: !php/const true
353+
second: 'Foo'
354+
355+
.. code-block:: xml
356+
357+
<!-- config/services.xml -->
358+
<?xml version="1.0" encoding="UTF-8" ?>
359+
<container xmlns="http://symfony.com/schema/dic/services"
360+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
361+
xmlns:framework="http://symfony.com/schema/dic/symfony"
362+
xsi:schemaLocation="http://symfony.com/schema/dic/services
363+
https://symfony.com/schema/dic/services/services-1.0.xsd
364+
http://symfony.com/schema/dic/symfony
365+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
366+
367+
<services>
368+
<service id="App\Service\SomeService">
369+
<!-- arguments without a type are treated as strings -->
370+
<argument>Foo</argument>
371+
<!-- explicitly declare a string argument -->
372+
<argument type="string">Foo</argument>
373+
<!-- constants can be built-in, user-defined, or Enums -->
374+
<argument type="constant">true</argument>
375+
<argument type="constant">E_ALL</argument>
376+
<argument type="constant">PDO::FETCH_NUM</argument>
377+
<argument type="constant">App\Service\AnotherService::SOME_CONSTANT</argument>
378+
<argument type="constant">App\Config\SomeEnum::SomeCase</argument>
379+
<!-- when not using autowiring, you can pass service arguments explicitly -->
380+
<argument type="service"
381+
id="some-service-id"
382+
on-invalid="dependency_injection-ignore" />
383+
<!-- collections (arrays) can include any type of argument -->
384+
<argument type="collection">
385+
<argument key="first" type="constant">true</argument>
386+
<argument key="second" type="string">Foo</argument>
387+
</argument>
388+
</service>
389+
390+
<!-- ... -->
391+
</services>
392+
393+
.. code-block:: php
394+
395+
// config/services.php
396+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
397+
398+
use Symfony\Component\DependencyInjection\ContainerInterface;
399+
use Symfony\Component\DependencyInjection\Reference;
400+
401+
return static function (ContainerConfigurator $container) {
402+
$services = $container->services();
403+
404+
$services->set(App\Service\SomeService::class)
405+
// string arguments
406+
->arg(0, 'Foo')
407+
// constants: built-in, user-defined, or Enums
408+
->arg(1, true)
409+
->arg(2, E_ALL)
410+
->arg(3, \PDO::FETCH_NUM)
411+
->arg(4, \App\Service\AnotherService::SOME_CONSTANT)
412+
->arg(5, \App\Config\SomeEnum::SomeCase)
413+
// explicit service reference with on-invalid behavior
414+
->arg(6, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
415+
// collection with mixed argument types
416+
->arg(7, [
417+
'first' => true,
418+
'second' => 'Foo',
419+
]);
420+
421+
// ...
422+
};
423+
326424
Handling Multiple Services
327425
~~~~~~~~~~~~~~~~~~~~~~~~~~
328426

0 commit comments

Comments
 (0)