Skip to content
Closed
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
43 changes: 43 additions & 0 deletions libraries/src/Event/DynamicSubscriberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Part of the Joomla Framework Event Package
*
* @copyright Copyright (C) 2005 - 2024 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\CMS\Event;

/**
* Interface for event subscribers.
*
* The difference from SubscriberInterface is that it uses non-static method,
* which allows subscriber to configure listeners depending on runtime.
* Additionally, it supports callables.
*
* @since __DEPLOY_VERSION__
*/
interface DynamicSubscriberInterface
{
/**
* Returns an array of events this subscriber will listen to.
*
* The array keys are event names and the value can be:
*
* - The method name to call (priority defaults to 0)
* - An array composed of the method name to call and the priority
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => $callable)
* * array('eventName' => array($callable, $priority))
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function getSubscribedEvents(): array;
}
19 changes: 18 additions & 1 deletion libraries/src/Plugin/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
namespace Joomla\CMS\Plugin;

use Joomla\CMS\Cache\Exception\CacheExceptionInterface;
use Joomla\CMS\Event\DynamicSubscriberInterface;
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;
Expand Down Expand Up @@ -239,7 +241,22 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac
return;
}

$plugin->registerListeners();
// Check for dynamic Subscriber
if ($plugin instanceof DynamicSubscriberInterface) {
foreach ($plugin->getSubscribedEvents() as $eventName => $params) {
if (\is_array($params)) {
$callback = !\is_string($params[0]) && \is_callable($params[0]) ? $params[0] : [$plugin, $params[0]];
$dispatcher->addListener($eventName, $callback, $params[1] ?? Priority::NORMAL);
} else {
$callback = !\is_string($params) && \is_callable($params) ? $params : [$plugin, $params];
$dispatcher->addListener($eventName, $callback);
}
}
} else {
// Register regular Subscribers
// @TODO: Starting from 7.0 it should use $dispatcher->addSubscriber($plugin);, for plugins which implements SubscriberInterface.
$plugin->registerListeners();
}
}

/**
Expand Down
7 changes: 2 additions & 5 deletions plugins/system/guidedtours/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public function register(Container $container)
$container->set(
PluginInterface::class,
function (Container $container) {
$app = Factory::getApplication();

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

$plugin->setApplication($app);
$plugin->setApplication(Factory::getApplication());

$wa = $container->get(WebAssetRegistry::class);

Expand Down
34 changes: 4 additions & 30 deletions plugins/system/guidedtours/src/Extension/GuidedTours.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

namespace Joomla\Plugin\System\GuidedTours\Extension;

use Joomla\CMS\Event\DynamicSubscriberInterface;
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;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -29,7 +28,7 @@
*
* @since 4.3.0
*/
final class GuidedTours extends CMSPlugin implements SubscriberInterface
final class GuidedTours extends CMSPlugin implements DynamicSubscriberInterface
{
/**
* A mapping for the step types
Expand Down Expand Up @@ -58,41 +57,16 @@ final class GuidedTours extends CMSPlugin implements SubscriberInterface
GuidedtoursComponent::STEP_INTERACTIVETYPE_SELECT => 'select',
];

/**
* An internal flag whether plugin should listen any event.
*
* @var bool
*
* @since 4.3.0
*/
protected static $enabled = false;

/**
* Constructor
*
* @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.
*
* @since 4.3.0
*/
public function __construct(DispatcherInterface $dispatcher, array $config = [], bool $enabled = false)
{
self::$enabled = $enabled;

parent::__construct($dispatcher, $config);
}

/**
* function for getSubscribedEvents : new Joomla 4 feature
*
* @return array
*
* @since 4.3.0
*/
public static function getSubscribedEvents(): array
public function getSubscribedEvents(): array
{
return self::$enabled ? [
return $this->getApplication()->isClient('administrator') ? [
'onAjaxGuidedtours' => 'startTour',
'onBeforeCompileHead' => 'onBeforeCompileHead',
] : [];
Expand Down
27 changes: 14 additions & 13 deletions plugins/system/schedulerunner/src/Extension/ScheduleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Joomla\Plugin\System\ScheduleRunner\Extension;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\DynamicSubscriberInterface;
use Joomla\CMS\Event\Model;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
Expand All @@ -24,7 +25,6 @@
use Joomla\Component\Scheduler\Administrator\Task\Task;
use Joomla\Event\Event;
use Joomla\Event\EventInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -42,7 +42,7 @@
*
* @since 4.1.0
*/
final class ScheduleRunner extends CMSPlugin implements SubscriberInterface
final class ScheduleRunner extends CMSPlugin implements DynamicSubscriberInterface
{
/**
* Length of auto-generated webcron key.
Expand All @@ -61,27 +61,28 @@ final class ScheduleRunner extends CMSPlugin implements SubscriberInterface
*
* @throws \Exception
*/
public static function getSubscribedEvents(): array
public function getSubscribedEvents(): array
{
$config = ComponentHelper::getParams('com_scheduler');
$app = Factory::getApplication();
$app = $this->getApplication();
$isSite = $app->isClient('site');
$isAdmin = $app->isClient('administrator');
$mapping = [];

$mapping = [];
if ($isSite || $isAdmin) {
$config = ComponentHelper::getParams('com_scheduler');

if ($app->isClient('site') || $app->isClient('administrator')) {
$mapping['onBeforeCompileHead'] = 'injectLazyJS';
$mapping['onAjaxRunSchedulerLazy'] = 'runLazyCron';

// Only allowed in the frontend
if ($app->isClient('site')) {
if ($config->get('webcron.enabled')) {
$mapping['onAjaxRunSchedulerWebcron'] = 'runWebCron';
}
} elseif ($app->isClient('administrator')) {
if ($isAdmin) {
// Only allowed in the administrator
$mapping['onContentPrepareForm'] = 'enhanceSchedulerConfig';
$mapping['onExtensionBeforeSave'] = 'generateWebcronKey';

$mapping['onAjaxRunSchedulerTest'] = 'runTestCron';
} elseif ($isSite && $config->get('webcron.enabled')) {
// Only allowed in the frontend
$mapping['onAjaxRunSchedulerWebcron'] = 'runWebCron';
}
}

Expand Down