Support proxies with new class parameter#63
Support proxies with new class parameter#63laoneo wants to merge 2 commits intojoomla-framework:4.x-devfrom
Conversation
voronkovich
left a comment
There was a problem hiding this comment.
Suggested API without using the lazy flag looks more clean to me.
|
|
||
| if (\is_callable($value)) { | ||
| $this->factory = $value; | ||
| if ($proxyClass && class_exists($proxyClass, false) && PHP_VERSION_ID >= 80400) { |
There was a problem hiding this comment.
I think if class doesn't exist we should throw an exception.
|
It looks better, but still no go, sorry. Few issue:
As we already discussed, it can be much smaller and simpler helper: But because it just a helper, wrapper to feature that already can work, it has low priority, and can go in to any minor release. |
|
This has nothing to do with the provider.php file. It is an additional way to register a resource. You need an extra class parameter, because in most of the cases you register an interface but the class to proxy needs to be about the concrete implementation class. So you actually need two parameters for this, one is the key and the other the actual class to implement. That's why your suggestions do not work, as far as I can read the code. It can be done with a new lazy function to be bc, but it will end up with the copied code from the set class with the additional lazy code. We will discus this issue in the next maintainer meeting. |
Not really, $container
->lazy(Foobar::class, $factory)
->alias(FoobarInterface::class, Foobar::class); |
|
It looks wrong and complicated to me when I have to create an alias for an interface resource. |
Yours looks not easier 😉 Compare final use: What variant is more easy for Developer to guess what is going on (without looking in source code of the container). Third option would be what @HLeithner suggested:
I could do that, but better keep discussion in one place, decide which variant is optimal for us, and then go from there. |
|
I would love to discus this in a maintainer meeting, so having your pr would make things easier. |
|
I would suggest to use named arguments to improve readability: $container->set(
FoobarInterface::class,
fn () => new Foobar(),
shared: false,
protected: false,
proxyClass: Foobar::class,
); |
Joomla doesn't support named arguments and it's not part of the b/c policy which could lead to unwanted side effects. |
Alternative to #58. A new parameter is added which allows to define the class to proxy. A service can then be defined in such a way:
the callback function (second argument) will not be called before the first access will be done on the returned object.
Breaks backwards compatibility
Classes which extend the container and do override the set, protect, share and getResource functions must add the new parameter.