Skip to content

Commit

Permalink
RegisterListenersChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Jun 14, 2024
1 parent 27a1218 commit 98f3ecd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
2 changes: 1 addition & 1 deletion libraries/src/Event/RegisterListenersCheckerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @since __DEPLOY_VERSION__
*/
interface RegisterListenersCheckerInterface
interface SubscriberRegistrationCheckerInterface
{
/**
* Check whether the Subscriber (or event listener) should be registered.
Expand Down
4 changes: 2 additions & 2 deletions libraries/src/Plugin/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
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 @@ -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);
Expand Down
41 changes: 14 additions & 27 deletions plugins/system/guidedtours/src/Extension/GuidedTours.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

/**
Expand Down

0 comments on commit 98f3ecd

Please sign in to comment.