joomla-cms icon indicating copy to clipboard operation
joomla-cms copied to clipboard

[5.x][RFC] CMSPlugin: Lazy subscriber interface and decorator

Open Fedik opened this issue 8 months ago • 0 comments

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):

  • https://github.com/joomla/joomla-cms/pull/43657

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
  • [x] Pull Request link for manual.joomla.org: https://github.com/joomla/Manual/pull/273
  • [ ] No documentation changes for manual.joomla.org needed

References:

  • https://github.com/joomla/joomla-cms/pull/43463

Fedik avatar Jun 15 '24 11:06 Fedik