Feature PR-6.0-dev Pending

User tests: Successful: Unsuccessful:

avatar Fedik
Fedik
3 Mar 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:

  • #43395 Would be good if it get in to 5.x.

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: TBD
  • No documentation changes for manual.joomla.org needed
avatar Fedik Fedik - open - 3 Mar 2025
avatar Fedik Fedik - change - 3 Mar 2025
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 3 Mar 2025
Category Libraries Front End Plugins
avatar Fedik Fedik - change - 3 Mar 2025
The description was changed
avatar Fedik Fedik - edited - 3 Mar 2025
avatar Fedik Fedik - change - 3 Mar 2025
Labels Added: Feature PR-5.3-dev
avatar joomla-cms-bot joomla-cms-bot - change - 3 Mar 2025
Category Libraries Front End Plugins Libraries Front End Plugins Unit Tests
avatar Fedik Fedik - change - 3 Mar 2025
Labels Added: Unit/System Tests
23778ea 3 Mar 2025 avatar Fedik tst
c7fa33a 3 Mar 2025 avatar Fedik tst
avatar laoneo
laoneo - comment - 3 Mar 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?

avatar Fedik
Fedik - comment - 3 Mar 2025

How to?

avatar laoneo
laoneo - comment - 4 Mar 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.

avatar Fedik
Fedik - comment - 4 Mar 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 :)

avatar HLeithner
HLeithner - comment - 4 Mar 2025

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

avatar HLeithner HLeithner - change - 4 Mar 2025
Title
CMSPlugin: use Lazy Object feature
[6.0] CMSPlugin: use Lazy Object feature
avatar HLeithner HLeithner - edited - 4 Mar 2025
avatar joomla-cms-bot joomla-cms-bot - change - 4 Mar 2025
Category Libraries Front End Plugins Unit Tests Administration SQL com_admin Postgresql com_associations com_banners com_categories com_checkin com_config com_contact com_content com_contenthistory com_cpanel com_fields com_finder com_installer com_joomlaupdate
avatar voronkovich
voronkovich - comment - 5 Mar 2025

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:

avatar voronkovich
voronkovich - comment - 5 Mar 2025

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

avatar HLeithner HLeithner - change - 6 Mar 2025
Labels Added: PR-6.0-dev
Removed: Unit/System Tests PR-5.3-dev
avatar joomla-cms-bot joomla-cms-bot - change - 6 Mar 2025
Category Administration SQL com_admin Postgresql com_associations com_banners com_categories com_checkin com_config com_contact com_content com_contenthistory com_cpanel com_fields com_finder com_installer com_joomlaupdate Libraries Front End Plugins Unit Tests
avatar Fedik
Fedik - comment - 6 Mar 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.

avatar laoneo
laoneo - comment - 6 Mar 2025

I'm pretty sure that this can be achieved in a backwards compatible way or with a minimum of bc breaks which shouldn't affect the CMS code.

36fe142 9 Mar 2025 avatar Fedik tst
b76f77f 9 Mar 2025 avatar Fedik tst
avatar Fedik Fedik - change - 9 Mar 2025
Labels Added: Unit/System Tests
02486fb 12 Mar 2025 avatar Fedik fix
c16cc08 12 Mar 2025 avatar Fedik fix
avatar Fedik Fedik - change - 12 Mar 2025
The description was changed
avatar Fedik Fedik - edited - 12 Mar 2025
1ebfa85 12 Mar 2025 avatar Fedik fix
56ff36f 12 Mar 2025 avatar Fedik fix
b1edeba 12 Mar 2025 avatar Fedik fix
avatar joomla-cms-bot joomla-cms-bot - change - 12 Mar 2025
Category Libraries Front End Plugins Unit Tests Libraries Front End Plugins
eaad966 12 Mar 2025 avatar Fedik fix
avatar Fedik Fedik - change - 12 Mar 2025
Labels Removed: Unit/System Tests
8b60479 13 Mar 2025 avatar Fedik fix
3bcafcc 13 Mar 2025 avatar Fedik fix
avatar Fedik Fedik - change - 13 Mar 2025
The description was changed
avatar Fedik Fedik - edited - 13 Mar 2025
avatar Fedik Fedik - change - 13 Mar 2025
The description was changed
avatar Fedik Fedik - edited - 13 Mar 2025
avatar Fedik Fedik - change - 30 Mar 2025
The description was changed
avatar Fedik Fedik - edited - 30 Mar 2025
avatar HLeithner
HLeithner - comment - 17 Jun 2025

@laoneo fedir update the pr to use reflections, this would solve the b/c problem without introducing a new interface.

Add a Comment

Login with GitHub to post a comment