You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FuncService is a convenient way to create a simple invocable service. One use-case for this is a function that retrieves some configuration at call time, which could help facilitate lazy loading. Let's say, I have a block that, when rendered on a page, must fetch some data from an API, and display it. To render, it needs some configuration that may not yet be available at application load time - because it has yet to be fetched; but it has no use for it before, and thus loading this configuration (e.g. from the API) must not happen until and unless the block renders. With the new ability to specify whole service definitions as dependencies, it could be as simple as:
[
'my-block-service' => newConstructor(MyBlock::class, [
newFuncService(['service-that-is-only-required-during-rendering'], fn(ServiceType$service): ServiceType => $service),
]),
];
classMyBlock
{
/** * @param callable(): ServiceType $getServiceForRendering Retrieves the service that's necessary to render the block */publicfunction__construct(protectedcallable$getServiceForRendering)
{
// Service is not yet loaded; only its "getter"
}
publicfunctionrender()
{
$service = ($this->getServiceForRendering)();
// Do something with the service, which is only retrieved during rendering
}
}
The Problem
FuncServiceresolves dependencies when it is resolved, rather than when the callable it returns is invoked. This necessitates that service-that-is-only-required-during-rendering is available during MyBlock's initialisation, which is way before it is used, if ever.
Suggested Solution
Make FuncService resolve dependencies when invoked, rather than when it is resolved.
The text was updated successfully, but these errors were encountered:
This is a valid use case. But I'd rather not change the current behavior of FuncService and instead create a new helper specifically for this task. It could even help reduce the verbosity of using FuncService by only requiring a service ID.
That came to my mind, and LazyService is indeed a fitting name. However, the aspect of "when to resolve dependencies" seems very applicable to the existing FuncService as well.
How about adding a flag to the constructor of FuncService?
I'm pro adding LazyService as well, regardless. If FuncService is modified, maybe it can be used under the hood somehow.
Usecase
FuncService
is a convenient way to create a simple invocable service. One use-case for this is a function that retrieves some configuration at call time, which could help facilitate lazy loading. Let's say, I have a block that, when rendered on a page, must fetch some data from an API, and display it. To render, it needs some configuration that may not yet be available at application load time - because it has yet to be fetched; but it has no use for it before, and thus loading this configuration (e.g. from the API) must not happen until and unless the block renders. With the new ability to specify whole service definitions as dependencies, it could be as simple as:The Problem
FuncService
resolves dependencies when it is resolved, rather than when the callable it returns is invoked. This necessitates thatservice-that-is-only-required-during-rendering
is available duringMyBlock
's initialisation, which is way before it is used, if ever.Suggested Solution
Make
FuncService
resolve dependencies when invoked, rather than when it is resolved.The text was updated successfully, but these errors were encountered: