By kim.pepper on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
10.3.x
Introduced in version:
10.3.0
Issue links:
Description:
You can delay instantiation of performance heavy or conditionally used services using service closures. https://symfony.com/doc/current/service_container/service_closures.html
Previously, you could only use service closures by declaring services to be autowired, then using the #[AutowireServiceClosure('foo.service')] attribute.
Now you can use the !service_closure custom tag in services.yml files.
For example:
my_service:
class: Drupal\Example\ExampleService
arguments: [!service_closure '@another_service']
Then in you class constructor, your service param is defined as \Closure.
public function __construct(
protected \Closure $anotherServiceClosure,
) {
}
When you need to instantiate your service you call the closure like so:
protected function getAnotherService(): AnotherService {
return ($this->anotherServiceClosure)();
}
Impacts:
Module developers