Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions libraries/src/Event/SubscriberRegistrationCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @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;
}
6 changes: 6 additions & 0 deletions libraries/src/Plugin/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
17 changes: 16 additions & 1 deletion plugins/system/debug/src/Extension/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +57,7 @@
*
* @since 1.5
*/
final class Debug extends CMSPlugin implements SubscriberInterface
final class Debug extends CMSPlugin implements SubscriberInterface, SubscriberRegistrationCheckerInterface
{
use DatabaseAwareTrait;

Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 0 additions & 1 deletion plugins/system/guidedtours/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function (Container $container) {

$plugin = new GuidedTours(
(array) PluginHelper::getPlugin('system', 'guidedtours'),
$app->isClient('administrator')
);

$plugin->setApplication($app);
Expand Down
39 changes: 14 additions & 25 deletions plugins/system/guidedtours/src/Extension/GuidedTours.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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');
}

/**
Expand Down