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; +} diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index c2ea73d5c6d89..9196140022db7 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\SubscriberRegistrationCheckerInterface; use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; @@ -239,6 +240,11 @@ protected static function import($plugin, $autocreate = true, ?DispatcherInterfa return; } + // Check whether we should register the subscriber in current runtime + if ($plugin instanceof SubscriberRegistrationCheckerInterface && !$plugin->shouldRegisterListeners()) { + return; + } + // @TODO: Starting from 7.0 it should use $dispatcher->addSubscriber($plugin); for plugins which implement SubscriberInterface. $plugin->registerListeners(); } diff --git a/plugins/system/debug/src/Extension/Debug.php b/plugins/system/debug/src/Extension/Debug.php index df589e67c5f6a..5f66657d8cfd6 100644 --- a/plugins/system/debug/src/Extension/Debug.php +++ b/plugins/system/debug/src/Extension/Debug.php @@ -21,6 +21,7 @@ use Joomla\CMS\Event\Application\BeforeCompileHeadEvent; use Joomla\CMS\Event\Application\BeforeRespondEvent; 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; @@ -56,7 +57,7 @@ * * @since 1.5 */ -final class Debug extends CMSPlugin implements SubscriberInterface +final class Debug extends CMSPlugin implements SubscriberInterface, SubscriberRegistrationCheckerInterface { use DatabaseAwareTrait; @@ -227,6 +228,20 @@ public function __construct(array $config, CMSApplicationInterface $app, Databas } } + /** + * 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. * diff --git a/plugins/system/guidedtours/services/provider.php b/plugins/system/guidedtours/services/provider.php index a573b33eab419..f817704674798 100644 --- a/plugins/system/guidedtours/services/provider.php +++ b/plugins/system/guidedtours/services/provider.php @@ -38,7 +38,6 @@ function (Container $container) { $plugin = new GuidedTours( (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 3d2bee1837748..016391652e11c 100644 --- a/plugins/system/guidedtours/src/Extension/GuidedTours.php +++ b/plugins/system/guidedtours/src/Extension/GuidedTours.php @@ -12,6 +12,7 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Date\Date; +use Joomla\CMS\Event\SubscriberRegistrationCheckerInterface; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\CMSPlugin; @@ -32,7 +33,7 @@ * * @since 4.3.0 */ -final class GuidedTours extends CMSPlugin implements SubscriberInterface +final class GuidedTours extends CMSPlugin implements SubscriberInterface, SubscriberRegistrationCheckerInterface { use DatabaseAwareTrait; @@ -64,42 +65,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 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(array $config = [], bool $enabled = false) + public static function getSubscribedEvents(): array { - self::$enabled = $enabled; - - parent::__construct($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'); } /**