Skip to content

[DependencyInjection] Tell about #[AutowireInline] #20958

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 1 commit into
base: 7.3
Choose a base branch
from
Open
Changes from all commits
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
27 changes: 27 additions & 0 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,33 @@
}
}

Autowiring Anonymous Services Inline
------------------------------------

Similar to how one can define anonymous services inline using configuration files,
the :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireInline`
attribute allows declaring anonymous services inline next to their corresponding
arguments::

public function __construct(
#[AutowireInline(
factory: [ScopingHttpClient::class, 'forBaseUri'],
arguments: [
'$baseUri' => 'https://api.example.com',
'$defaultOptions' => [
'auth_bearer' => '%env(EXAMPLE_TOKEN)%',
],
]
)]
private HttpClientInterface $githubClient,
)

As you might have already figured out, this declaration instructs Symfony to inject an

Check failure on line 886 in service_container/autowiring.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected '}', expecting ';' or '{'
object created by calling the ``ScopingHttpClient::forBaseUri()`` factory with the
configured base URI and default options.

Of course, this is just an example and this attribute can be used to construct any kind of objects.

Autowiring Controller Action Methods
------------------------------------

Expand Down
Loading