From c963feb7b8fbe4aaa858fefc9744aa8e60a43791 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 15:52:54 +0300 Subject: [PATCH 1/3] Info about Lazy Subscriber --- .../plugins/advanced-plugin-features.md | 66 +++++++++++++++++++ migrations/51-52/new-features.md | 9 +++ 2 files changed, 75 insertions(+) create mode 100644 docs/building-extensions/plugins/advanced-plugin-features.md diff --git a/docs/building-extensions/plugins/advanced-plugin-features.md b/docs/building-extensions/plugins/advanced-plugin-features.md new file mode 100644 index 00000000..70147ced --- /dev/null +++ b/docs/building-extensions/plugins/advanced-plugin-features.md @@ -0,0 +1,66 @@ +Advanced Plugin features +======================== + +## Lazy Subscriber + +The decorator `Joomla\CMS\Event\LazyServiceSubscriber` allows to instantiate the plugin with heavy dependencies only when the event is actually dispatched. +The plugin should implement `Joomla\Event\SubscriberInterface` to be able to use decorator. + +Note, that use of lazy decorator does not make sense when the plugin subscribed to the events that happening regularly, like `onAfterInitialise`. + +Additionally, the interface `Joomla\CMS\Event\LazySubscriberInterface` allows to use [`LazyServiceEventListener`](https://github.com/joomla-framework/event/blob/2.0-dev/src/LazyServiceEventListener.php), however the plugin should implement it on its own. + +### Example usage of the lazy subscriber decorator for the plugin with heavy dependencies: + +Code for `plugins/system/example/services/provider.php`. + +**Before, regular plugin service:** +```php +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, plugin service with LazyServiceSubscriber:** + +```php +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); + } + ); + } +} +``` diff --git a/migrations/51-52/new-features.md b/migrations/51-52/new-features.md index 246c7074..eac04723 100644 --- a/migrations/51-52/new-features.md +++ b/migrations/51-52/new-features.md @@ -16,3 +16,12 @@ This new feature adds a "total" counter at the bottom near the pagination in Joo displaying the number of items available after applying filters for easier item management. PR: [43575](https://github.com/joomla/joomla-cms/pull/43575) + + +#### New lazy decorator for Plugins + +Adding Lazy Subscriber interface and decorator. +The decorator allows to instantiate the plugin with heavy dependencies only when the event is actually dispatched. +PR: https://github.com/joomla/joomla-cms/pull/43658 + +More details here: [Lazy Subscriber](https://manual.joomla.org/docs/building-extensions/plugins/advanced-plugin-features#lazy-subscriber). From afe4acda18b4ad60240ac66f14eb08f007fabb33 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 15:57:33 +0300 Subject: [PATCH 2/3] Info about Lazy Subscriber --- docs/building-extensions/plugins/advanced-plugin-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/building-extensions/plugins/advanced-plugin-features.md b/docs/building-extensions/plugins/advanced-plugin-features.md index 70147ced..ea4afde8 100644 --- a/docs/building-extensions/plugins/advanced-plugin-features.md +++ b/docs/building-extensions/plugins/advanced-plugin-features.md @@ -6,7 +6,7 @@ Advanced Plugin features The decorator `Joomla\CMS\Event\LazyServiceSubscriber` allows to instantiate the plugin with heavy dependencies only when the event is actually dispatched. The plugin should implement `Joomla\Event\SubscriberInterface` to be able to use decorator. -Note, that use of lazy decorator does not make sense when the plugin subscribed to the events that happening regularly, like `onAfterInitialise`. +Note that the use of the lazy decorator does not make sense when the plugin is subscribed to events that happen regularly, like `onAfterInitialise`. Additionally, the interface `Joomla\CMS\Event\LazySubscriberInterface` allows to use [`LazyServiceEventListener`](https://github.com/joomla-framework/event/blob/2.0-dev/src/LazyServiceEventListener.php), however the plugin should implement it on its own. From 0254f2945693dc2bc13205f72173ec3ab3baca3b Mon Sep 17 00:00:00 2001 From: Fedik Date: Thu, 20 Jun 2024 18:14:13 +0300 Subject: [PATCH 3/3] upd --- docs/building-extensions/plugins/advanced-plugin-features.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/building-extensions/plugins/advanced-plugin-features.md b/docs/building-extensions/plugins/advanced-plugin-features.md index ea4afde8..9490041d 100644 --- a/docs/building-extensions/plugins/advanced-plugin-features.md +++ b/docs/building-extensions/plugins/advanced-plugin-features.md @@ -64,3 +64,7 @@ return new class () implements ServiceProviderInterface { } } ``` + +With the regular plugin service the dependencies are instantiated whenever the plugin is instantiated, ie when its plugin type is imported. + +With the Lazy Subscriber the plugin service and its dependencies are instantiated only whenever one of the events to which the plugin service subscribes is triggered.