Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1dea8a0

Browse files
committed
[DI] Document PHP-DSL service_closure() function
1 parentafa8a4f commit1dea8a0

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

‎service_container/service_closures.rst‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,32 @@ argument of type ``service_closure``:
7777
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
7878
7979
use App\Service\MyService;
80-
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
81-
use Symfony\Component\DependencyInjection\Reference;
8280
8381
return function (ContainerConfigurator $configurator) {
8482
$services = $configurator->services();
8583
8684
$services->set(MyService::class)
87-
->args([new ServiceClosureArgument(new Reference('mailer'))]);
85+
->args([service_closure('mailer')]);
86+
87+
// In case the dependency is optional
88+
// $services->set(MyService::class)
89+
// ->args([service_closure('mailer')->nullOnInvalid()]);
8890
};
91+
92+
Using Service Closures in Compiler Passes
93+
-----------------------------------------
94+
95+
In:doc:`compiler passes</service_container/compiler_passes>` you can create
96+
a service closure by wrapping the service reference into an instance of
97+
:class:`Symfony\\Component\\DependencyInjection\\Argument\\ServiceClosureArgument`::
98+
99+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
100+
use Symfony\Component\DependencyInjection\ContainerBuilder;
101+
use Symfony\Component\DependencyInjection\Reference;
102+
103+
public function process(ContainerBuilder $container): void
104+
{
105+
// ...
106+
107+
$myService->addArgument(new ServiceClosureArgument(new Reference('mailer')));
108+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp