Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x][RFC] CMSPlugin: Lazy subscriber interface and decorator #43658

Open
wants to merge 16 commits into
base: 5.2-dev
Choose a base branch
from
Prev Previous commit
Next Next commit
LazyServiceEventSubscriber
  • Loading branch information
Fedik committed Jun 14, 2024
commit 9fe5d0f9d83717607cbfdbf692e27fdc46888c74
28 changes: 15 additions & 13 deletions libraries/src/Event/LazyServiceEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ final class LazyServiceEventSubscriber implements PluginInterface
private $class;

/**
* List of subscribed events
* List of events, and listeners.
*
* @var array
* @since __DEPLOY_VERSION__
*/
private $subscribedEvents = [];
private $eventsAndListeners = [];

/**
* @var SubscriberInterface
Expand All @@ -69,23 +69,23 @@ public function __construct(ContainerInterface $container, string $class)
$this->class = $class;

if (!is_subclass_of($class, SubscriberInterface::class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not implement %s', $class, SubscriberInterface::class));
throw new \InvalidArgumentException(sprintf('Class %s does not implement %s', $class, SubscriberInterface::class));
}

$this->subscribedEvents = $class::getSubscribedEvents();
$this->eventsAndListeners = $class::getSubscribedEvents();
}

/**
* Retrieve the instance of Subscriber.
*
* @return \Joomla\Event\SubscriberInterface
* @return object
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*
* @since __DEPLOY_VERSION__
*/
public function getSubscriberInstance(): SubscriberInterface
public function getSubscriberInstance(): object
{
if (!$this->instance) {
$this->instance = $this->container->get($this->class);
Expand All @@ -95,15 +95,15 @@ public function getSubscriberInstance(): SubscriberInterface
}

/**
* Returns an array of events the subscriber will listen to.
* Returns an array of events and the listeners, the subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function getSubscribedEvents(): array
public function getEventsAndListeners(): array
{
return $this->subscribedEvents;
return $this->eventsAndListeners;
}

/**
Expand All @@ -115,15 +115,17 @@ public function getSubscribedEvents(): array
* @return mixed
*
* @throws \InvalidArgumentException
*
* @since __DEPLOY_VERSION__
*/
public function __call(string $eventName, array $arguments): mixed
{
if (!\array_key_exists($eventName, $this->subscribedEvents)) {
throw new \InvalidArgumentException(sprintf('Event "%s" not supported by "%s".', $eventName, $this->class));
}

$instance = $this->instance ?: $this->getSubscriberInstance();

if (!\is_callable([$instance, $eventName])) {
throw new \InvalidArgumentException(sprintf('Event "%s" not supported by %s.', $eventName, $this->class));
}

return \call_user_func_array([$instance, $eventName], $arguments);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/src/Plugin/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected static function import($plugin, $autocreate = true, DispatcherInterfac
}

if ($plugin instanceof LazyServiceEventSubscriber) {
foreach ($plugin->getSubscribedEvents() as $eventName => $params) {
foreach ($plugin->getEventsAndListeners() as $eventName => $params) {
if (\is_array($params)) {
$dispatcher->addListener($eventName, [$plugin, $params[0]], $params[1] ?? Priority::NORMAL);
} else {
Expand Down