From 6b63a155cfb83066b9099c415463c9de935d895b Mon Sep 17 00:00:00 2001 From: Fedik Date: Sun, 9 Jun 2024 14:57:18 +0300 Subject: [PATCH 01/15] LazyServiceEventSubscriber --- .../src/Event/LazyServiceEventSubscriber.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 libraries/src/Event/LazyServiceEventSubscriber.php diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php new file mode 100644 index 0000000000000..b81b0f5d8875f --- /dev/null +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -0,0 +1,55 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\CMS\Event; + +use Psr\Container\ContainerInterface; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Class for lazy subscribers + * + * @since __DEPLOY_VERSION__ + */ +class LazyServiceEventSubscriber +{ + /** + * The service container + * + * @var ContainerInterface + * @since __DEPLOY_VERSION__ + */ + private $container; + + /** + * Listener class name + * + * @var string + * @since __DEPLOY_VERSION__ + */ + private $class; + + /** + * Constructor. + * + * @param \Psr\Container\ContainerInterface $container Container + * @param string $class Listener class name + * + * @since __DEPLOY_VERSION__ + */ + public function __construct(ContainerInterface $container, string $class) + { + $this->container = $container; + $this->class = $class; + } + +} From aabe564b7741375550b3c5948dfe83388b95102b Mon Sep 17 00:00:00 2001 From: Fedik Date: Sun, 9 Jun 2024 18:24:30 +0300 Subject: [PATCH 02/15] LazyServiceEventSubscriber --- .../src/Event/LazyServiceEventSubscriber.php | 100 +++++++++++++++++- libraries/src/Plugin/PluginHelper.php | 14 ++- plugins/system/debug/services/provider.php | 8 +- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index b81b0f5d8875f..8146361c82789 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -9,6 +9,9 @@ namespace Joomla\CMS\Event; +use Joomla\CMS\Extension\PluginInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Event\SubscriberInterface; use Psr\Container\ContainerInterface; // phpcs:disable PSR1.Files.SideEffects @@ -16,11 +19,11 @@ // phpcs:enable PSR1.Files.SideEffects /** - * Class for lazy subscribers + * Decorator for lazy subscribers be pulled from the service container when subscribed event dispatched. * * @since __DEPLOY_VERSION__ */ -class LazyServiceEventSubscriber +final class LazyServiceEventSubscriber implements PluginInterface { /** * The service container @@ -38,6 +41,20 @@ class LazyServiceEventSubscriber */ private $class; + /** + * List of subscribed events + * + * @var array + * @since __DEPLOY_VERSION__ + */ + private $subscribedEvents = []; + + /** + * @var SubscriberInterface + * @since __DEPLOY_VERSION__ + */ + private $instance; + /** * Constructor. * @@ -50,6 +67,85 @@ public function __construct(ContainerInterface $container, string $class) { $this->container = $container; $this->class = $class; + + if (!is_subclass_of($class, SubscriberInterface::class)) { + throw new \InvalidArgumentException(sprintf('Class "%s" does not implement %s', $class, SubscriberInterface::class)); + } + + $this->subscribedEvents = $class::getSubscribedEvents(); + } + + /** + * Retrieve the instance of Subscriber. + * + * @return \Joomla\Event\SubscriberInterface + * + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + * + * @since __DEPLOY_VERSION__ + */ + public function getSubscriberInstance(): SubscriberInterface + { + if (!$this->instance) { + $this->instance = $this->container->get($this->class); + } + + return $this->instance; + } + + /** + * Returns an array of events the subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function getSubscribedEvents(): array + { + return $this->subscribedEvents; + } + + /** + * Method to call the event listeners. + * + * @param string $eventName + * @param array $arguments + * + * @return mixed + * + * @throws \InvalidArgumentException + */ + public function __call(string $eventName, array $arguments): mixed + { + if (!\array_key_exists($eventName, $this->subscribedEvents)) { + throw new \InvalidArgumentException(sprintf('Event "%s" not supported by "%s".', $eventName, $this->class)); + } + + $instance = $this->instance ?: $this->getSubscriberInstance(); + + return \call_user_func_array([$instance, $eventName], $arguments); } + /** + * A dummy to complete the interface. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function registerListeners() + { + } + + /** + * A dummy to complete the interface. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setDispatcher(DispatcherInterface $dispatcher) + { + } } diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 9c3c591e311ae..ff167e9e2679c 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -10,9 +10,11 @@ namespace Joomla\CMS\Plugin; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; +use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; +use Joomla\Event\Priority; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -239,7 +241,17 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac return; } - $plugin->registerListeners(); + if ($plugin instanceof LazyServiceEventSubscriber) { + foreach ($plugin->getSubscribedEvents() as $eventName => $params) { + if (\is_array($params)) { + $dispatcher->addListener($eventName, [$plugin, $params[0]], $params[1] ?? Priority::NORMAL); + } else { + $dispatcher->addListener($eventName, [$plugin, $params]); + } + } + } else { + $plugin->registerListeners(); + } } /** diff --git a/plugins/system/debug/services/provider.php b/plugins/system/debug/services/provider.php index 540d9f98f52a9..047d0f956982a 100644 --- a/plugins/system/debug/services/provider.php +++ b/plugins/system/debug/services/provider.php @@ -10,6 +10,7 @@ \defined('_JEXEC') or die; +use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -32,7 +33,7 @@ public function register(Container $container): void { $container->set( - PluginInterface::class, + Debug::class, function (Container $container) { return new Debug( $container->get(DispatcherInterface::class), @@ -41,6 +42,11 @@ function (Container $container) { $container->get(DatabaseInterface::class) ); } + )->set( + PluginInterface::class, + function (Container $container) { + return new LazyServiceEventSubscriber($container, Debug::class); + } ); } }; From 9fe5d0f9d83717607cbfdbf692e27fdc46888c74 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 16:15:11 +0300 Subject: [PATCH 03/15] LazyServiceEventSubscriber --- .../src/Event/LazyServiceEventSubscriber.php | 28 ++++++++++--------- libraries/src/Plugin/PluginHelper.php | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index 8146361c82789..54814ee334fd3 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -42,12 +42,12 @@ final class LazyServiceEventSubscriber implements PluginInterface private $class; /** - * List of subscribed events + * List of events, and listeners. * * @var array * @since __DEPLOY_VERSION__ */ - private $subscribedEvents = []; + private $eventsAndListeners = []; /** * @var SubscriberInterface @@ -69,23 +69,23 @@ public function __construct(ContainerInterface $container, string $class) $this->class = $class; if (!is_subclass_of($class, SubscriberInterface::class)) { - throw new \InvalidArgumentException(sprintf('Class "%s" does not implement %s', $class, SubscriberInterface::class)); + throw new \InvalidArgumentException(sprintf('Class %s does not implement %s', $class, SubscriberInterface::class)); } - $this->subscribedEvents = $class::getSubscribedEvents(); + $this->eventsAndListeners = $class::getSubscribedEvents(); } /** * Retrieve the instance of Subscriber. * - * @return \Joomla\Event\SubscriberInterface + * @return object * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface * * @since __DEPLOY_VERSION__ */ - public function getSubscriberInstance(): SubscriberInterface + public function getSubscriberInstance(): object { if (!$this->instance) { $this->instance = $this->container->get($this->class); @@ -95,15 +95,15 @@ public function getSubscriberInstance(): SubscriberInterface } /** - * Returns an array of events the subscriber will listen to. + * Returns an array of events and the listeners, the subscriber will listen to. * * @return array * * @since __DEPLOY_VERSION__ */ - public function getSubscribedEvents(): array + public function getEventsAndListeners(): array { - return $this->subscribedEvents; + return $this->eventsAndListeners; } /** @@ -115,15 +115,17 @@ public function getSubscribedEvents(): array * @return mixed * * @throws \InvalidArgumentException + * + * @since __DEPLOY_VERSION__ */ public function __call(string $eventName, array $arguments): mixed { - if (!\array_key_exists($eventName, $this->subscribedEvents)) { - throw new \InvalidArgumentException(sprintf('Event "%s" not supported by "%s".', $eventName, $this->class)); - } - $instance = $this->instance ?: $this->getSubscriberInstance(); + if (!\is_callable([$instance, $eventName])) { + throw new \InvalidArgumentException(sprintf('Event "%s" not supported by %s.', $eventName, $this->class)); + } + return \call_user_func_array([$instance, $eventName], $arguments); } diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index ff167e9e2679c..cb4197b8fa141 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -242,7 +242,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac } if ($plugin instanceof LazyServiceEventSubscriber) { - foreach ($plugin->getSubscribedEvents() as $eventName => $params) { + foreach ($plugin->getEventsAndListeners() as $eventName => $params) { if (\is_array($params)) { $dispatcher->addListener($eventName, [$plugin, $params[0]], $params[1] ?? Priority::NORMAL); } else { From 494925726cbe1cc073d870e5b5d162baa1919748 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 16:34:22 +0300 Subject: [PATCH 04/15] LazyServiceEventSubscriber --- libraries/src/Plugin/PluginHelper.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index cb4197b8fa141..0db92ef89d0bd 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -243,10 +243,12 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac if ($plugin instanceof LazyServiceEventSubscriber) { foreach ($plugin->getEventsAndListeners() as $eventName => $params) { - if (\is_array($params)) { - $dispatcher->addListener($eventName, [$plugin, $params[0]], $params[1] ?? Priority::NORMAL); + if (\is_array($params) && !\is_callable($params)) { + $callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]]; + $dispatcher->addListener($eventName, $callback, $params[1] ?? Priority::NORMAL); } else { - $dispatcher->addListener($eventName, [$plugin, $params]); + $callback = !\is_string($params) && \is_callable($params) ? $params : [$plugin, $params]; + $dispatcher->addListener($eventName, $callback); } } } else { From c15c617bba0e6854258fe34aca5591d62a684aad Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:00:55 +0300 Subject: [PATCH 05/15] LazyServiceEventSubscriber --- .../Event/LazyEventSubscriberInterface.php | 57 +++++++++++++++++++ .../src/Event/LazyServiceEventSubscriber.php | 2 +- plugins/system/debug/services/provider.php | 4 +- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 libraries/src/Event/LazyEventSubscriberInterface.php diff --git a/libraries/src/Event/LazyEventSubscriberInterface.php b/libraries/src/Event/LazyEventSubscriberInterface.php new file mode 100644 index 0000000000000..33067525c6ca4 --- /dev/null +++ b/libraries/src/Event/LazyEventSubscriberInterface.php @@ -0,0 +1,57 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\CMS\Event; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Decorator for lazy subscribers be pulled from the service container when subscribed event dispatched. + * + * @since __DEPLOY_VERSION__ + */ +interface LazyEventSubscriberInterface +{ + /** + * Retrieve the instance of Subscriber. + * + * @return object + * + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + * + * @since __DEPLOY_VERSION__ + */ + public function getSubscriberInstance(): object; + + /** + * Returns an array of events and the listeners, the subscriber will listen to. + * + * The array keys are event names and the value can be: + * + * - The method name (of the subscriber instance) to call (priority defaults to 0) + * - An array composed of the method name (of the subscriber instance) to call and the priority + * - A callable instance to call + * - An array composed of the callable instance to call and the priority + * + * For instance: + * + * ['eventName' => 'methodName'] + * ['eventName' => ['methodName', $priority]] + * ['eventName' => $callable] + * ['eventName' => [$callable, $priority]] + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function getEventsAndListeners(): array; +} diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index 54814ee334fd3..50966f014b0aa 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -23,7 +23,7 @@ * * @since __DEPLOY_VERSION__ */ -final class LazyServiceEventSubscriber implements PluginInterface +final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, PluginInterface { /** * The service container diff --git a/plugins/system/debug/services/provider.php b/plugins/system/debug/services/provider.php index 047d0f956982a..5ad7954763395 100644 --- a/plugins/system/debug/services/provider.php +++ b/plugins/system/debug/services/provider.php @@ -32,7 +32,7 @@ */ public function register(Container $container): void { - $container->set( + $container->share( Debug::class, function (Container $container) { return new Debug( @@ -42,7 +42,7 @@ function (Container $container) { $container->get(DatabaseInterface::class) ); } - )->set( + )->share( PluginInterface::class, function (Container $container) { return new LazyServiceEventSubscriber($container, Debug::class); From 3a4e51106599af079360d41e1394a61705a11bff Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:07:24 +0300 Subject: [PATCH 06/15] LazyServiceEventSubscriber --- libraries/src/Event/LazyEventSubscriberInterface.php | 3 +-- libraries/src/Event/LazyServiceEventSubscriber.php | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/src/Event/LazyEventSubscriberInterface.php b/libraries/src/Event/LazyEventSubscriberInterface.php index 33067525c6ca4..e64926710d16b 100644 --- a/libraries/src/Event/LazyEventSubscriberInterface.php +++ b/libraries/src/Event/LazyEventSubscriberInterface.php @@ -25,8 +25,7 @@ interface LazyEventSubscriberInterface * * @return object * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \Throwable * * @since __DEPLOY_VERSION__ */ diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index 50966f014b0aa..36b5e9f1435d9 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -34,7 +34,7 @@ final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, private $container; /** - * Listener class name + * Subscriber class name, and id in the container * * @var string * @since __DEPLOY_VERSION__ @@ -59,7 +59,7 @@ final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, * Constructor. * * @param \Psr\Container\ContainerInterface $container Container - * @param string $class Listener class name + * @param string $class Subscriber class name, and id in the container * * @since __DEPLOY_VERSION__ */ @@ -80,8 +80,7 @@ public function __construct(ContainerInterface $container, string $class) * * @return object * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \Throwable * * @since __DEPLOY_VERSION__ */ From 07f3548a44d9f8add9889b9cb5c3c26d0b33db72 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:09:01 +0300 Subject: [PATCH 07/15] LazyServiceEventSubscriber --- libraries/src/Plugin/PluginHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 0db92ef89d0bd..0bc2adcf3e5b8 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -10,7 +10,7 @@ namespace Joomla\CMS\Plugin; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; -use Joomla\CMS\Event\LazyServiceEventSubscriber; +use Joomla\CMS\Event\LazyEventSubscriberInterface; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; @@ -241,7 +241,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac return; } - if ($plugin instanceof LazyServiceEventSubscriber) { + if ($plugin instanceof LazyEventSubscriberInterface) { foreach ($plugin->getEventsAndListeners() as $eventName => $params) { if (\is_array($params) && !\is_callable($params)) { $callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]]; From 38c59232157a175edcee1e092e73e2b9bb3cf7cc Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:28:52 +0300 Subject: [PATCH 08/15] LazyServiceEventSubscriber --- libraries/src/Plugin/PluginHelper.php | 1 + plugins/system/debug/services/provider.php | 10 ++-------- plugins/system/webauthn/services/provider.php | 10 ++++++++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 0bc2adcf3e5b8..4e6ff4a1c9ac5 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -242,6 +242,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac } if ($plugin instanceof LazyEventSubscriberInterface) { + dump($plugin); foreach ($plugin->getEventsAndListeners() as $eventName => $params) { if (\is_array($params) && !\is_callable($params)) { $callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]]; diff --git a/plugins/system/debug/services/provider.php b/plugins/system/debug/services/provider.php index 5ad7954763395..540d9f98f52a9 100644 --- a/plugins/system/debug/services/provider.php +++ b/plugins/system/debug/services/provider.php @@ -10,7 +10,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -32,8 +31,8 @@ */ public function register(Container $container): void { - $container->share( - Debug::class, + $container->set( + PluginInterface::class, function (Container $container) { return new Debug( $container->get(DispatcherInterface::class), @@ -42,11 +41,6 @@ function (Container $container) { $container->get(DatabaseInterface::class) ); } - )->share( - PluginInterface::class, - function (Container $container) { - return new LazyServiceEventSubscriber($container, Debug::class); - } ); } }; diff --git a/plugins/system/webauthn/services/provider.php b/plugins/system/webauthn/services/provider.php index c9517c55385b2..9ee8989a3cde7 100644 --- a/plugins/system/webauthn/services/provider.php +++ b/plugins/system/webauthn/services/provider.php @@ -12,6 +12,7 @@ use Joomla\Application\ApplicationInterface; use Joomla\Application\SessionAwareWebApplicationInterface; +use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -39,8 +40,8 @@ */ public function register(Container $container) { - $container->set( - PluginInterface::class, + $container->share( + Webauthn::class, function (Container $container) { $app = Factory::getApplication(); $session = $container->has('session') ? $container->get('session') : $this->getSession($app); @@ -72,6 +73,11 @@ function (Container $container) { return $plugin; } + )->share( + PluginInterface::class, + function (Container $container) { + return new LazyServiceEventSubscriber($container, Webauthn::class); + } ); } From f75c188b08ad2de81861c3081486c3998eeafa45 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:45:13 +0300 Subject: [PATCH 09/15] LazyServiceEventSubscriber --- plugins/system/schedulerunner/services/provider.php | 8 +++++++- plugins/system/tasknotification/services/provider.php | 8 +++++++- plugins/system/webauthn/services/provider.php | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/plugins/system/schedulerunner/services/provider.php b/plugins/system/schedulerunner/services/provider.php index a8ff358f768e1..75b30764a5f71 100644 --- a/plugins/system/schedulerunner/services/provider.php +++ b/plugins/system/schedulerunner/services/provider.php @@ -10,6 +10,7 @@ \defined('_JEXEC') or die; +use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -31,7 +32,7 @@ public function register(Container $container): void { $container->set( - PluginInterface::class, + ScheduleRunner::class, function (Container $container) { $plugin = new ScheduleRunner( $container->get(DispatcherInterface::class), @@ -41,6 +42,11 @@ function (Container $container) { return $plugin; } + )->set( + PluginInterface::class, + function (Container $container) { + return new LazyServiceEventSubscriber($container, ScheduleRunner::class); + } ); } }; diff --git a/plugins/system/tasknotification/services/provider.php b/plugins/system/tasknotification/services/provider.php index c0cfbcd564d85..b869e8cb9e6a2 100644 --- a/plugins/system/tasknotification/services/provider.php +++ b/plugins/system/tasknotification/services/provider.php @@ -10,6 +10,7 @@ \defined('_JEXEC') or die; +use Joomla\CMS\Event\LazyServiceEventSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -33,7 +34,7 @@ public function register(Container $container): void { $container->set( - PluginInterface::class, + TaskNotification::class, function (Container $container) { $plugin = new TaskNotification( $container->get(DispatcherInterface::class), @@ -45,6 +46,11 @@ function (Container $container) { return $plugin; } + )->set( + PluginInterface::class, + function (Container $container) { + return new LazyServiceEventSubscriber($container, TaskNotification::class); + } ); } }; diff --git a/plugins/system/webauthn/services/provider.php b/plugins/system/webauthn/services/provider.php index 9ee8989a3cde7..0349091826827 100644 --- a/plugins/system/webauthn/services/provider.php +++ b/plugins/system/webauthn/services/provider.php @@ -40,7 +40,7 @@ */ public function register(Container $container) { - $container->share( + $container->set( Webauthn::class, function (Container $container) { $app = Factory::getApplication(); @@ -73,7 +73,7 @@ function (Container $container) { return $plugin; } - )->share( + )->set( PluginInterface::class, function (Container $container) { return new LazyServiceEventSubscriber($container, Webauthn::class); From 1bc3f5432afdb4bc6cb7f720448699f690414ef1 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 17:49:36 +0300 Subject: [PATCH 10/15] LazyServiceEventSubscriber --- libraries/src/Plugin/PluginHelper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 4e6ff4a1c9ac5..0bc2adcf3e5b8 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -242,7 +242,6 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac } if ($plugin instanceof LazyEventSubscriberInterface) { - dump($plugin); foreach ($plugin->getEventsAndListeners() as $eventName => $params) { if (\is_array($params) && !\is_callable($params)) { $callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]]; From 08e5c6d09386bfe10c7891108e1688b47e16a943 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 19:32:32 +0300 Subject: [PATCH 11/15] SubscriberRegistrationCheckerInterface --- .../src/Event/LazyServiceEventSubscriber.php | 18 ++++++++++- ...SubscriberRegistrationCheckerInterface.php | 31 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 libraries/src/Event/SubscriberRegistrationCheckerInterface.php diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index 36b5e9f1435d9..c2f2474bc1ec5 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -23,7 +23,7 @@ * * @since __DEPLOY_VERSION__ */ -final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, PluginInterface +final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, SubscriberRegistrationCheckerInterface, PluginInterface { /** * The service container @@ -105,6 +105,22 @@ public function getEventsAndListeners(): array return $this->eventsAndListeners; } + /** + * Check whether the Subscriber (or event listener) should be registered. + * + * @return bool + * + * @since __DEPLOY_VERSION__ + */ + public function shouldRegisterListeners(): bool + { + if ($this->container->has(SubscriberRegistrationCheckerInterface::class)) { + return $this->container->get(SubscriberRegistrationCheckerInterface::class)->shouldRegisterListeners(); + } + + return true; + } + /** * Method to call the event listeners. * diff --git a/libraries/src/Event/SubscriberRegistrationCheckerInterface.php b/libraries/src/Event/SubscriberRegistrationCheckerInterface.php new file mode 100644 index 0000000000000..e7bbddc46d36e --- /dev/null +++ b/libraries/src/Event/SubscriberRegistrationCheckerInterface.php @@ -0,0 +1,31 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\CMS\Event; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Provides a method to check whether the Subscriber (or event listener) should be registered. + * + * @since __DEPLOY_VERSION__ + */ +interface SubscriberRegistrationCheckerInterface +{ + /** + * Check whether the Subscriber (or event listener) should be registered. + * + * @return bool + * + * @since __DEPLOY_VERSION__ + */ + public function shouldRegisterListeners(): bool; +} From 4d48408051e1d59431352d1c35e3d377fdb054fd Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 13:56:52 +0300 Subject: [PATCH 12/15] Revert "SubscriberRegistrationCheckerInterface" This reverts commit 08e5c6d09386bfe10c7891108e1688b47e16a943. --- .../src/Event/LazyServiceEventSubscriber.php | 18 +---------- ...SubscriberRegistrationCheckerInterface.php | 31 ------------------- 2 files changed, 1 insertion(+), 48 deletions(-) delete mode 100644 libraries/src/Event/SubscriberRegistrationCheckerInterface.php diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index c2f2474bc1ec5..36b5e9f1435d9 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -23,7 +23,7 @@ * * @since __DEPLOY_VERSION__ */ -final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, SubscriberRegistrationCheckerInterface, PluginInterface +final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, PluginInterface { /** * The service container @@ -105,22 +105,6 @@ public function getEventsAndListeners(): array return $this->eventsAndListeners; } - /** - * Check whether the Subscriber (or event listener) should be registered. - * - * @return bool - * - * @since __DEPLOY_VERSION__ - */ - public function shouldRegisterListeners(): bool - { - if ($this->container->has(SubscriberRegistrationCheckerInterface::class)) { - return $this->container->get(SubscriberRegistrationCheckerInterface::class)->shouldRegisterListeners(); - } - - return true; - } - /** * Method to call the event listeners. * diff --git a/libraries/src/Event/SubscriberRegistrationCheckerInterface.php b/libraries/src/Event/SubscriberRegistrationCheckerInterface.php deleted file mode 100644 index e7bbddc46d36e..0000000000000 --- a/libraries/src/Event/SubscriberRegistrationCheckerInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * @license GNU General Public License version 2 or later; see LICENSE.txt - */ - -namespace Joomla\CMS\Event; - -// phpcs:disable PSR1.Files.SideEffects -\defined('_JEXEC') or die; -// phpcs:enable PSR1.Files.SideEffects - -/** - * Provides a method to check whether the Subscriber (or event listener) should be registered. - * - * @since __DEPLOY_VERSION__ - */ -interface SubscriberRegistrationCheckerInterface -{ - /** - * Check whether the Subscriber (or event listener) should be registered. - * - * @return bool - * - * @since __DEPLOY_VERSION__ - */ - public function shouldRegisterListeners(): bool; -} From e039761e43c2c86dfaabd35aca1628486fdbe469 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 14:00:24 +0300 Subject: [PATCH 13/15] Stringable Interface --- libraries/src/Event/LazyServiceEventSubscriber.php | 2 +- ...entSubscriberInterface.php => LazySubscriberInterface.php} | 2 +- libraries/src/Plugin/PluginHelper.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename libraries/src/Event/{LazyEventSubscriberInterface.php => LazySubscriberInterface.php} (97%) diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceEventSubscriber.php index 36b5e9f1435d9..639b5080151e5 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceEventSubscriber.php @@ -23,7 +23,7 @@ * * @since __DEPLOY_VERSION__ */ -final class LazyServiceEventSubscriber implements LazyEventSubscriberInterface, PluginInterface +final class LazyServiceEventSubscriber implements LazySubscriberInterface, PluginInterface { /** * The service container diff --git a/libraries/src/Event/LazyEventSubscriberInterface.php b/libraries/src/Event/LazySubscriberInterface.php similarity index 97% rename from libraries/src/Event/LazyEventSubscriberInterface.php rename to libraries/src/Event/LazySubscriberInterface.php index e64926710d16b..a44e7b7624fb4 100644 --- a/libraries/src/Event/LazyEventSubscriberInterface.php +++ b/libraries/src/Event/LazySubscriberInterface.php @@ -18,7 +18,7 @@ * * @since __DEPLOY_VERSION__ */ -interface LazyEventSubscriberInterface +interface LazySubscriberInterface { /** * Retrieve the instance of Subscriber. diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 0bc2adcf3e5b8..3206ef2610766 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -10,7 +10,7 @@ namespace Joomla\CMS\Plugin; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; -use Joomla\CMS\Event\LazyEventSubscriberInterface; +use Joomla\CMS\Event\LazySubscriberInterface; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; @@ -241,7 +241,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac return; } - if ($plugin instanceof LazyEventSubscriberInterface) { + if ($plugin instanceof LazySubscriberInterface) { foreach ($plugin->getEventsAndListeners() as $eventName => $params) { if (\is_array($params) && !\is_callable($params)) { $callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]]; From 609ca8ebde0ae4f07b0dd46d3d88afc3b8782b74 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 14:33:26 +0300 Subject: [PATCH 14/15] rename --- ...zyServiceEventSubscriber.php => LazyServiceSubscriber.php} | 2 +- plugins/system/schedulerunner/services/provider.php | 4 ++-- plugins/system/tasknotification/services/provider.php | 4 ++-- plugins/system/webauthn/services/provider.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) rename libraries/src/Event/{LazyServiceEventSubscriber.php => LazyServiceSubscriber.php} (97%) diff --git a/libraries/src/Event/LazyServiceEventSubscriber.php b/libraries/src/Event/LazyServiceSubscriber.php similarity index 97% rename from libraries/src/Event/LazyServiceEventSubscriber.php rename to libraries/src/Event/LazyServiceSubscriber.php index 639b5080151e5..22044aab94b3e 100644 --- a/libraries/src/Event/LazyServiceEventSubscriber.php +++ b/libraries/src/Event/LazyServiceSubscriber.php @@ -23,7 +23,7 @@ * * @since __DEPLOY_VERSION__ */ -final class LazyServiceEventSubscriber implements LazySubscriberInterface, PluginInterface +final class LazyServiceSubscriber implements LazySubscriberInterface, PluginInterface { /** * The service container diff --git a/plugins/system/schedulerunner/services/provider.php b/plugins/system/schedulerunner/services/provider.php index 75b30764a5f71..a95799730991c 100644 --- a/plugins/system/schedulerunner/services/provider.php +++ b/plugins/system/schedulerunner/services/provider.php @@ -10,7 +10,7 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Event\LazyServiceEventSubscriber; +use Joomla\CMS\Event\LazyServiceSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -45,7 +45,7 @@ function (Container $container) { )->set( PluginInterface::class, function (Container $container) { - return new LazyServiceEventSubscriber($container, ScheduleRunner::class); + return new LazyServiceSubscriber($container, ScheduleRunner::class); } ); } diff --git a/plugins/system/tasknotification/services/provider.php b/plugins/system/tasknotification/services/provider.php index b869e8cb9e6a2..91fd41e94773f 100644 --- a/plugins/system/tasknotification/services/provider.php +++ b/plugins/system/tasknotification/services/provider.php @@ -10,7 +10,7 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Event\LazyServiceEventSubscriber; +use Joomla\CMS\Event\LazyServiceSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -49,7 +49,7 @@ function (Container $container) { )->set( PluginInterface::class, function (Container $container) { - return new LazyServiceEventSubscriber($container, TaskNotification::class); + return new LazyServiceSubscriber($container, TaskNotification::class); } ); } diff --git a/plugins/system/webauthn/services/provider.php b/plugins/system/webauthn/services/provider.php index 0349091826827..e35d3a135a1ba 100644 --- a/plugins/system/webauthn/services/provider.php +++ b/plugins/system/webauthn/services/provider.php @@ -12,7 +12,7 @@ use Joomla\Application\ApplicationInterface; use Joomla\Application\SessionAwareWebApplicationInterface; -use Joomla\CMS\Event\LazyServiceEventSubscriber; +use Joomla\CMS\Event\LazyServiceSubscriber; use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; @@ -76,7 +76,7 @@ function (Container $container) { )->set( PluginInterface::class, function (Container $container) { - return new LazyServiceEventSubscriber($container, Webauthn::class); + return new LazyServiceSubscriber($container, Webauthn::class); } ); } From 7f02736537bc0ffbe50fbca18c1625af7273790b Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 19:31:25 +0300 Subject: [PATCH 15/15] comment --- libraries/src/Event/LazyServiceSubscriber.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/src/Event/LazyServiceSubscriber.php b/libraries/src/Event/LazyServiceSubscriber.php index 22044aab94b3e..040f7cba6fb28 100644 --- a/libraries/src/Event/LazyServiceSubscriber.php +++ b/libraries/src/Event/LazyServiceSubscriber.php @@ -50,7 +50,9 @@ final class LazyServiceSubscriber implements LazySubscriberInterface, PluginInte private $eventsAndListeners = []; /** - * @var SubscriberInterface + * Subscriber instance. + * + * @var object * @since __DEPLOY_VERSION__ */ private $instance;