Skip to content

Conversation

@Fedik
Copy link
Member

@Fedik Fedik commented Jun 15, 2024

Summary of Changes

Adding LazySybscriber interface and decorator.
The decorator LazyServiceSubscriber allows to instantiate the plugin with heavy dependencies only when the event is actualy dispatched.
The interface LazySubscriberInterface also allows to use LazyServiceEventListener, hovewer the plugin should implement it on its own.

Example usage of the decorator for the plugin with heavy dependencies:

Code for plugins/system/example/services/provider.php.

Before:

return new class () implements ServiceProviderInterface {
  public function register(Container $container)
  {
    $container->set(
        PluginInterface::class,
        function (Container $container) {
            $heavyDependency1 = $container->get(Foo::class);
            $heavyDependency2 = $container->get(Bar::class);
            
            return new ExamplePlugin(
                $container->get(DispatcherInterface::class),
                (array) PluginHelper::getPlugin('system', 'example'),
                $heavyDependency1,
                $heavyDependency2
            );
        }
    );
  }
}

After:

return new class () implements ServiceProviderInterface {
  public function register(Container $container)
  {
    $container->set(
        ExamplePlugin::class,
        function (Container $container) {
            $heavyDependency1 = $container->get(Foo::class);
            $heavyDependency2 = $container->get(Bar::class);
            
            return new ExamplePlugin(
                $container->get(DispatcherInterface::class),
                (array) PluginHelper::getPlugin('system', 'example'),
                $heavyDependency1,
                $heavyDependency2
            );
        }
    )->set(
        PluginInterface::class,
        function (Container $container) {
            return new LazyServiceSubscriber($container, ExamplePlugin::class);
        }
    );
  }
}

The decorator can work very nicely with (in future):

But I will update it depend what first will be merged this PR or that PR.

Testing Instructions

Code review by maintainers.

Check that following plugins works as before:

ScheduleRunner
TaskNotification
Webauthn

Actual result BEFORE applying this Pull Request

Works

Expected result AFTER applying this Pull Request

Works

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: Info about Lazy Subscriber Manual#273
  • No documentation changes for manual.joomla.org needed

References:

@joomla-cms-bot joomla-cms-bot added PR-5.2-dev RFC Request for Comment labels Jun 15, 2024
@Fedik Fedik added the Feature label Jun 15, 2024
@Fedik Fedik changed the title [5.2][RFC] CMSPlugin: Lazy subscriber interface and decorator [5.x][RFC] CMSPlugin: Lazy subscriber interface and decorator Jun 15, 2024
@HLeithner HLeithner changed the base branch from 5.2-dev to 5.3-dev September 2, 2024 08:51
@HLeithner
Copy link
Member

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

@HLeithner HLeithner changed the title [5.x][RFC] CMSPlugin: Lazy subscriber interface and decorator [5.3] [RFC] CMSPlugin: Lazy subscriber interface and decorator Sep 2, 2024
@Hackwar Hackwar removed the PR-5.2-dev label Sep 3, 2024
@Fedik
Copy link
Member Author

Fedik commented Mar 2, 2025

I will redo this with Lazy Objects
Current approach is totally obsolete with PHP 8.4

@Fedik Fedik marked this pull request as draft March 2, 2025 12:57
@brianteeman
Copy link
Contributor

But we still support earlier versions of PHP don't we?

@laoneo
Copy link
Member

laoneo commented Mar 2, 2025

Would it not make more sense to have the whole DI container to support lazy objects? Like that, the plugins are only instantiated when actually used.

@Fedik
Copy link
Member Author

Fedik commented Mar 2, 2025

But we still support earlier versions of PHP don't we?

I doubt this PR will be merged in 1-2 years, we have plenty of time 😄

Would it not make more sense to have the whole DI container to support lazy objects

I need to see a code or an example to say something about it. In theory maybe "yes".
But currently I hardly imagine how it can be done.

@Fedik
Copy link
Member Author

Fedik commented Mar 3, 2025

Alternative pr is there:

@Fedik Fedik closed this Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature PR-5.3-dev RFC Request for Comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants