Skip to content

[3.x] Lazy services#58

Closed
voronkovich wants to merge 10 commits intojoomla-framework:3.x-devfrom
PopArtDesign:lazy-services
Closed

[3.x] Lazy services#58
voronkovich wants to merge 10 commits intojoomla-framework:3.x-devfrom
PopArtDesign:lazy-services

Conversation

@voronkovich
Copy link
Contributor

Pull Request for Issue joomla/joomla-cms#45062

This PR add an ability to define lazy services using a new PHP 8.4 feature: https://www.php.net/manual/en/language.oop5.lazy-objects.php

Summary of Changes

A new shortcut method lazy() was added:

$container->lazy(
    Foo:class,
    function($container) {
        return new Foo($container->get('bar'));
    }
);

// Proxy object
$foo = $conainer->get(Foo::class);

// Factory is called here
$foo->someMethod(); 

Testing Instructions

Unit tests are provided.

Documentation Changes Required

Yes.

@HLeithner
Copy link
Contributor

I don't really see the benefit of knowing if a object is lazy, if the container or a consumer needs to know it (what I'm not sure if it is needed) it could be achieved with a reflection and the isUninitializedLazyObject() method.

What would be the use case for this implementation?

@voronkovich
Copy link
Contributor Author

About use case for this feature you can read here: joomla/joomla-cms#45062

This allows to instantiate the plugin only when event is triggered, and saving some resources (time and memory).

We can achieve it by defining plugins as lazy.

Also you can read about lazy services here: https://symfony.com/doc/current/service_container/lazy_services.html

@voronkovich
Copy link
Contributor Author

The ContainerResource class is final and it's marked as internal, so changing it wouldn't cause the BC break.

@voronkovich voronkovich changed the title Lazy services [3.x] Lazy services Mar 13, 2025
@laoneo
Copy link
Contributor

laoneo commented Mar 13, 2025

To simplify the whole pr, I would only add a lazy function and then create an insteance of resource there. The rest can be reverted I guess as it complicates the whole thing. Also in the resource I would only add the really needed functions in a first implementation. If we need more along the way we can always add them when needed.

@laoneo
Copy link
Contributor

laoneo commented Mar 13, 2025

But I like that you added unit tests to the pr.

@voronkovich
Copy link
Contributor Author

@laoneo, I've added the changes you requested. But I'm not sure if I got them right.

@laoneo
Copy link
Contributor

laoneo commented Mar 14, 2025

That's exactly how I intended it.

@laoneo
Copy link
Contributor

laoneo commented Apr 25, 2025

I made a more lightweight version in #63. Can you have a look if I missed something?

@voronkovich
Copy link
Contributor Author

Closed in favor of #63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants