From 27a12184773e7903a15d8d0a2e2f71ff0567af7b Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 18:57:56 +0300 Subject: [PATCH 1/5] RegisterListenersChecker --- .../RegisterListenersCheckerInterface.php | 31 +++++++++++++++++++ libraries/src/Plugin/PluginHelper.php | 7 +++++ 2 files changed, 38 insertions(+) create mode 100644 libraries/src/Event/RegisterListenersCheckerInterface.php diff --git a/libraries/src/Event/RegisterListenersCheckerInterface.php b/libraries/src/Event/RegisterListenersCheckerInterface.php new file mode 100644 index 0000000000000..16fa10d215300 --- /dev/null +++ b/libraries/src/Event/RegisterListenersCheckerInterface.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 RegisterListenersCheckerInterface +{ + /** + * Check whether the Subscriber (or event listener) should be registered. + * + * @return bool + * + * @since __DEPLOY_VERSION__ + */ + public function shouldRegisterListeners(): bool; +} diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 9c3c591e311ae..17c0584f217fd 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -10,6 +10,7 @@ namespace Joomla\CMS\Plugin; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; +use Joomla\CMS\Event\RegisterListenersCheckerInterface; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; @@ -239,6 +240,12 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac return; } + // Check whether we should register the subscriber in current runtime + if ($plugin instanceof RegisterListenersCheckerInterface && !$plugin->shouldRegisterListeners()) { + dump($plugin); + return; + } + $plugin->registerListeners(); } From 98f3ecdda6701eac91a4f072ded0de488f3a5c8a Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 19:01:13 +0300 Subject: [PATCH 2/5] RegisterListenersChecker --- .../RegisterListenersCheckerInterface.php | 2 +- libraries/src/Plugin/PluginHelper.php | 4 +- .../system/guidedtours/services/provider.php | 1 - .../guidedtours/src/Extension/GuidedTours.php | 41 +++++++------------ 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/libraries/src/Event/RegisterListenersCheckerInterface.php b/libraries/src/Event/RegisterListenersCheckerInterface.php index 16fa10d215300..e7bbddc46d36e 100644 --- a/libraries/src/Event/RegisterListenersCheckerInterface.php +++ b/libraries/src/Event/RegisterListenersCheckerInterface.php @@ -18,7 +18,7 @@ * * @since __DEPLOY_VERSION__ */ -interface RegisterListenersCheckerInterface +interface SubscriberRegistrationCheckerInterface { /** * Check whether the Subscriber (or event listener) should be registered. diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 17c0584f217fd..27258ddf9708a 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\RegisterListenersCheckerInterface; +use Joomla\CMS\Event\SubscriberRegistrationCheckerInterface; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; @@ -241,7 +241,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac } // Check whether we should register the subscriber in current runtime - if ($plugin instanceof RegisterListenersCheckerInterface && !$plugin->shouldRegisterListeners()) { + if ($plugin instanceof SubscriberRegistrationCheckerInterface && !$plugin->shouldRegisterListeners()) { dump($plugin); return; } diff --git a/plugins/system/guidedtours/services/provider.php b/plugins/system/guidedtours/services/provider.php index 8e30afd6b0b47..c846c7c775681 100644 --- a/plugins/system/guidedtours/services/provider.php +++ b/plugins/system/guidedtours/services/provider.php @@ -39,7 +39,6 @@ function (Container $container) { $plugin = new GuidedTours( $container->get(DispatcherInterface::class), (array) PluginHelper::getPlugin('system', 'guidedtours'), - $app->isClient('administrator') ); $plugin->setApplication($app); diff --git a/plugins/system/guidedtours/src/Extension/GuidedTours.php b/plugins/system/guidedtours/src/Extension/GuidedTours.php index 4bcc765ad39fc..551ef7b9f0822 100644 --- a/plugins/system/guidedtours/src/Extension/GuidedTours.php +++ b/plugins/system/guidedtours/src/Extension/GuidedTours.php @@ -10,13 +10,13 @@ namespace Joomla\Plugin\System\GuidedTours\Extension; +use Joomla\CMS\Event\SubscriberRegistrationCheckerInterface; use Joomla\CMS\Language\Text; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Session\Session; use Joomla\Component\Guidedtours\Administrator\Extension\GuidedtoursComponent; use Joomla\Component\Guidedtours\Administrator\Model\TourModel; -use Joomla\Event\DispatcherInterface; use Joomla\Event\Event; use Joomla\Event\SubscriberInterface; @@ -29,7 +29,7 @@ * * @since 4.3.0 */ -final class GuidedTours extends CMSPlugin implements SubscriberInterface +final class GuidedTours extends CMSPlugin implements SubscriberInterface, SubscriberRegistrationCheckerInterface { /** * A mapping for the step types @@ -59,43 +59,30 @@ final class GuidedTours extends CMSPlugin implements SubscriberInterface ]; /** - * An internal flag whether plugin should listen any event. - * - * @var bool - * - * @since 4.3.0 - */ - protected static $enabled = false; - - /** - * Constructor + * function for getSubscribedEvents : new Joomla 4 feature * - * @param DispatcherInterface $dispatcher The object to observe - * @param array $config An optional associative array of configuration settings. - * @param boolean $enabled An internal flag whether plugin should listen any event. + * @return array * * @since 4.3.0 */ - public function __construct(DispatcherInterface $dispatcher, array $config = [], bool $enabled = false) + public static function getSubscribedEvents(): array { - self::$enabled = $enabled; - - parent::__construct($dispatcher, $config); + return [ + 'onAjaxGuidedtours' => 'startTour', + 'onBeforeCompileHead' => 'onBeforeCompileHead', + ]; } /** - * function for getSubscribedEvents : new Joomla 4 feature + * Check whether the Subscriber should be registered. * - * @return array + * @return bool * - * @since 4.3.0 + * @since __DEPLOY_VERSION__ */ - public static function getSubscribedEvents(): array + public function shouldRegisterListeners(): bool { - return self::$enabled ? [ - 'onAjaxGuidedtours' => 'startTour', - 'onBeforeCompileHead' => 'onBeforeCompileHead', - ] : []; + return $this->getApplication()->isClient('administrator'); } /** From f83ff973d3f3b8179aa4b4b9ccd3ecdbf85fc64b Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 19:01:48 +0300 Subject: [PATCH 3/5] SubscriberRegistrationCheckerInterface --- ...erInterface.php => SubscriberRegistrationCheckerInterface.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libraries/src/Event/{RegisterListenersCheckerInterface.php => SubscriberRegistrationCheckerInterface.php} (100%) diff --git a/libraries/src/Event/RegisterListenersCheckerInterface.php b/libraries/src/Event/SubscriberRegistrationCheckerInterface.php similarity index 100% rename from libraries/src/Event/RegisterListenersCheckerInterface.php rename to libraries/src/Event/SubscriberRegistrationCheckerInterface.php From b9a2f237f2383295463617fad6ff66baa6cf92da Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 19:06:04 +0300 Subject: [PATCH 4/5] SubscriberRegistrationCheckerInterface --- plugins/system/debug/src/Extension/Debug.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/system/debug/src/Extension/Debug.php b/plugins/system/debug/src/Extension/Debug.php index e17756cf82ad6..1debd1ae3e324 100644 --- a/plugins/system/debug/src/Extension/Debug.php +++ b/plugins/system/debug/src/Extension/Debug.php @@ -17,6 +17,7 @@ use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Document\HtmlDocument; use Joomla\CMS\Event\Plugin\AjaxEvent; +use Joomla\CMS\Event\SubscriberRegistrationCheckerInterface; use Joomla\CMS\Log\Log; use Joomla\CMS\Log\LogEntry; use Joomla\CMS\Log\Logger\InMemoryLogger; @@ -53,7 +54,7 @@ * * @since 1.5 */ -final class Debug extends CMSPlugin implements SubscriberInterface +final class Debug extends CMSPlugin implements SubscriberInterface, SubscriberRegistrationCheckerInterface { use DatabaseAwareTrait; @@ -223,6 +224,20 @@ public function __construct(DispatcherInterface $dispatcher, array $config, CMSA } } + /** + * Check whether the Subscriber should be registered. + * + * @return bool + * + * @since __DEPLOY_VERSION__ + */ + public function shouldRegisterListeners(): bool + { + $app = $this->getApplication(); + + return $app->get('debug') || $app->get('debug_lang'); + } + /** * Add an assets for debugger. * From 6caec2639deb635b55c671479b1e66d9078d997c Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 14 Jun 2024 19:07:27 +0300 Subject: [PATCH 5/5] SubscriberRegistrationCheckerInterface --- 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 27258ddf9708a..dd596ab75b66e 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 // Check whether we should register the subscriber in current runtime if ($plugin instanceof SubscriberRegistrationCheckerInterface && !$plugin->shouldRegisterListeners()) { - dump($plugin); return; }