- Issue created by @godotislate
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.
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:
public function __construct(
protected CronInterface $cron,
) {}
Service definition:
services:
my_service:
class: Foo\Bar
arguments: ['@cron']
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']
public function __construct(
protected CronInterface $cron,
) {}
Service definition:
services:
my_service:
class: Foo\Bar
autowire: true
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
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
Active
11.0 š„
base system