Problem/Motivation
As discussed in
š
DX: Creating lazy services is too difficult/obscure/bespoke/brittle
Active
the DX for our current lazy service proxies is poor. We tried using symfony lazy services in
š
Deprecate Drupal ProxyBuilder in favor of Symfony lazy services
Closed: won't fix
however that was not possible due to:
Sadly SF uses eval for the proxy classes which at least I can't see working with our serialized container solution
However we can now use service closures to lazy-load services which are supported by our serialized container.
Steps to reproduce
Proposed resolution
Replacy services tagged 'lazy' with service closures.
Prefer using autowiring and the #[AutowireServiceClosure]
attribute over the !service_closure
yaml command where possible.
https://symfony.com/doc/current/service_container/autowiring.html#genera...
This requires changing the constructor signature and adding a new method to invoke the closure like so:
Before:
public function __construct(
protected CronInterface $cron,
) {}
Service definition:
services:
my_service:
class: Foo\Bar
arguments: ['@cron']
After:
public function __construct(
protected \Closure $cronClosure,
) {}
protected function getCron(): CronInterface {
return ($this->cronClosure)();
}
Service definition:
services:
my_service:
class: Foo\Bar
arguments: [!service_closure '@cron']
Autowiring approach
Before:
public function __construct(
protected CronInterface $cron,
) {}
Service definition:
services:
my_service:
class: Foo\Bar
autowire: true
After:
public function __construct(
#[AutowireServiceClosure('cron')]
protected \Closure $cronClosure,
) {}
protected function getCron(): CronInterface {
return ($this->cronClosure)();
}
Service definition:
services:
my_service:
class: Foo\Bar
autowire: true
Remaining tasks
Current proxies under core/lib/Drupal/Core/ProxyClass
:
āāā Batch
ā āāā BatchStorage.php
āāā Config
ā āāā ConfigInstaller.php
āāā Cron.php
āāā Extension
ā āāā ModuleInstaller.php
āāā File
ā āāā MimeType
ā āāā ExtensionMimeTypeGuesser.php
ā āāā MimeTypeGuesser.php
āāā Lock
ā āāā DatabaseLockBackend.php
ā āāā PersistentDatabaseLockBackend.php
āāā Menu
ā āāā MenuActiveTrail.php
āāā PageCache
ā āāā ChainResponsePolicy.php
āāā ParamConverter
ā āāā AdminPathConfigEntityConverter.php
ā āāā MenuLinkPluginConverter.php
āāā Render
ā āāā BareHtmlPageRenderer.php
āāā Routing
āāā MatcherDumper.php
āāā RouteBuilder.php
Proxies in core modules:
core/modules/language/src/ProxyClass/LanguageConverter.php
core/modules/node/src/ProxyClass/ParamConverter/NodePreviewConverter.php
core/modules/views_ui/src/ProxyClass/ParamConverter/ViewUIConverter.php
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet