8000 [DependencyInjection] Document the different types of service arguments by javiereguiluz · Pull Request #21048 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Document the different types of service arguments #21048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 6.4
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
-
  • Loading branch information
javiereguiluz committed Jun 4, 2025
commit 1f682ca9450ae9c49f913849c64b9e0e6a00d30e
50 changes: 37 additions & 13 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,26 @@
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

Check failure on line 347 in service_container.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Yaml syntax] The constant "App\Service\AnotherService::SOME_CONSTANT" is not defined at line 15 (near "!php/const App\Service\AnotherService::SOME_CONSTANT").
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid this error, can we use a real constant from the codebase? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it to the DOCtor-RST allow list

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work, the error comes from the code-blocks checker

- !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
Expand All @@ -366,20 +373,30 @@

<services>
<service id="App\Service\SomeService">
<!-- arguments without a type are treated as strings -->
<!-- arguments without a type can be strings or numbers -->
<argument>Foo</argument>
<argument>7</argument>
<argument>3.14</argument>
<!-- explicitly declare a string argument -->
<argument type="string">Foo</argument>
<!-- booleans are passed as constants -->
<argument type="constant">true</argument>

<!-- constants can be built-in, user-defined, or Enums -->
<argument type="constant">true</argument>
<argument type="constant">E_ALL</argument>
<argument type="constant">PDO::FETCH_NUM</argument>
<argument type="constant">App\Service\AnotherService::SOME_CONSTANT</argument>
<argument type="constant">App\Config\SomeEnum::SomeCase</argument>

<!-- when not using autowiring, you can pass service arguments explicitly -->
<argument type="service"
id="some-service-id"
on-invalid="dependency_injection-ignore"/>

<!-- binary contents are passed encoded as base64 strings -->
<argument type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</argument>

<!-- collections (arrays) can include any type of argument -->
<argument type="collection">
<argument key="first" type="constant">true</argument>
Expand All @@ -388,7 +405,7 @@
</service>

<!-- ... -->
</services>

Check failure on line 408 in service_container.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[XML syntax] Premature end of data in tag container line 2

.. code-block:: php

Expand All @@ -402,18 +419,25 @@
$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',
]);
Expand Down
Loading
0