Skip to content

[6.1] CMSPlugin: use Lazy Object feature#45062

Merged
HLeithner merged 39 commits intojoomla:6.1-devfrom
Fedik:plugin-lazy-objects
Feb 9, 2026
Merged

[6.1] CMSPlugin: use Lazy Object feature#45062
HLeithner merged 39 commits intojoomla:6.1-devfrom
Fedik:plugin-lazy-objects

Conversation

@Fedik
Copy link
Member

@Fedik Fedik commented Mar 3, 2025

Summary of Changes

Alternative to #43658

Use of PHP Lazy Object feature for plugins.
This allows to instantiate the plugin only when event is triggered, and saving some resources (time and memory).

This does not applied automatically for every plugins, the plugin service provider need to be updated to support Lazy Object.

Few thing that need to figure out:

  1. Following code will trigger lazy object initialization, we need to get rid of it, somehow.

    if ($dispatcher && $plugin instanceof DispatcherAwareInterface) {
    $plugin->setDispatcher($dispatcher);
    }

  2. Current use of registerListeners() need to be deprecated. There is PR for it:

As next step we have to:
OR change the return type to boolean for registerListeners(),
OR replace it with new method, see the PR:

Testing Instructions

Apply patch, and test on PHP 8.x and 8.4
The following plugins should work as before:

  • schedulerunner
  • tasknotification
  • webauthn

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:
  • No documentation changes for docs.joomla.org needed
  • Pull Request link for manual.joomla.org: Plugins lazy loading Manual#585
  • No documentation changes for manual.joomla.org needed

@laoneo
Copy link
Member

laoneo commented Mar 3, 2025

As said already, this should be done in the DI container and not every plugin itself should implement this. Or do you see problems when the DI container does that?

@Fedik
Copy link
Member Author

Fedik commented Mar 3, 2025

How to?

@laoneo
Copy link
Member

laoneo commented Mar 4, 2025

Similar cod you have in the service provider, I would do here https://github.com/joomla-framework/di/blob/3.x-dev/src/ContainerResource.php#L160.

@Fedik
Copy link
Member Author

Fedik commented Mar 4, 2025

hm, yeah, that also could work, need to check. We need it to be flexible.

But I thought that you have some ready to use solution :)

@HLeithner HLeithner changed the base branch from 5.3-dev to 6.0-dev March 4, 2025 17:18
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 6.0-dev.

@HLeithner HLeithner changed the title CMSPlugin: use Lazy Object feature [6.0] CMSPlugin: use Lazy Object feature Mar 4, 2025
@rdeutz rdeutz removed the PR-5.3-dev label Mar 5, 2025
@voronkovich
Copy link
Contributor

I would suggest to add a simple shortcut for creating lazy proxies:

// Container
public function lazy(string $class, callable $factory): callable
{
    if (PHP_VERSION_ID < 80400) {
        return $factory;
    }

    return function () use ($class, $factory) {
        return (new \ReflectionClass($class))->newLazyProxy(fn() => $factory($this));
    };
}

Then defining lazy service would look like this:

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

Full implementation you can see here:

@voronkovich
Copy link
Contributor

I've created a PR with more advanced implementation: joomla-framework/di#58

@Fedik
Copy link
Member Author

Fedik commented Mar 6, 2025

@laoneo That is require changes in Container code with b/c breaks (need new flags and changed method signature).
@voronkovich That implementation does not look good.

I will keep it without DI, there more important issue that need to resolve to make it work.

@Fedik
Copy link
Member Author

Fedik commented Aug 12, 2025

It still fine, with introducing of "Reflection checker" it does not change existing behavior.

@Fedik Fedik marked this pull request as ready for review August 12, 2025 09:59
@muhme
Copy link
Contributor

muhme commented Aug 22, 2025

✅ Tested my random quotation module zitat-service.de with JBT: it installs from JED Web, is is configurable and working on frontend site

Fedik added 2 commits August 28, 2025 09:56
 Conflicts:
	plugins/system/cache/src/Extension/Cache.php
	plugins/system/schedulerunner/services/provider.php
	plugins/system/schemaorg/services/provider.php
	plugins/system/tasknotification/services/provider.php
@HLeithner HLeithner changed the base branch from 6.0-dev to 6.1-dev August 31, 2025 11:57
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 6.1-dev.

@HLeithner HLeithner changed the title [6.0] CMSPlugin: use Lazy Object feature [6.1] CMSPlugin: use Lazy Object feature Aug 31, 2025
@Fedik Fedik removed the PR-6.0-dev label Jan 17, 2026
@exlemor
Copy link

exlemor commented Feb 8, 2026

@Fedik - is this finalized and therefore testable? Will gladly test it for you if it is.

@Fedik
Copy link
Member Author

Fedik commented Feb 8, 2026

Yes it is, as for now.

@HLeithner
Copy link
Member

I tested it today with my own component and at the first look it worked but after playing with the subscribed events I found out that the plugin is not loaded if onAfterInitialise is used, it worked for onAfterInitialiseDocument...

@HLeithner
Copy link
Member

I have tested this item ✅ successfully on 97c0b79

Tested with own component and works as expected, I missed the part that we (php) only create an instance of the object access an array property/method.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/45062.

@tecpromotion
Copy link
Contributor

I have tested this item ✅ successfully on 97c0b79

Tested

@tecpromotion
Copy link
Contributor

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/45062.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Feb 9, 2026
@HLeithner HLeithner merged commit 037e66e into joomla:6.1-dev Feb 9, 2026
51 checks passed
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Feb 9, 2026
@HLeithner
Copy link
Member

Thanks

@HLeithner
Copy link
Member

HLeithner commented Feb 9, 2026

@Fedik would you be so kind and update the documentation manual for plugins.

I also updated all plugins in the cms in pr #46862

@Fedik Fedik deleted the plugin-lazy-objects branch February 9, 2026 19:44
@richard67 richard67 added this to the Joomla! 6.1.0 milestone Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.