Commaaa2
This commit is contained in:
@@ -13,6 +13,7 @@ namespace Symfony\Component\EventDispatcher\Debug;
|
||||
|
||||
use Psr\EventDispatcher\StoppableEventInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -36,13 +37,13 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
* @var \SplObjectStorage<WrappedListener, array{string, string}>|null
|
||||
*/
|
||||
private ?\SplObjectStorage $callStack = null;
|
||||
private $dispatcher;
|
||||
private EventDispatcherInterface $dispatcher;
|
||||
private array $wrappedListeners = [];
|
||||
private array $orphanedEvents = [];
|
||||
private $requestStack;
|
||||
private ?RequestStack $requestStack;
|
||||
private string $currentRequestHash = '';
|
||||
|
||||
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null, RequestStack $requestStack = null)
|
||||
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, ?LoggerInterface $logger = null, ?RequestStack $requestStack = null)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->stopwatch = $stopwatch;
|
||||
@@ -51,7 +52,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0)
|
||||
{
|
||||
@@ -59,7 +60,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
@@ -67,7 +68,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function removeListener(string $eventName, callable|array $listener)
|
||||
{
|
||||
@@ -81,28 +82,22 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
}
|
||||
|
||||
return $this->dispatcher->removeListener($eventName, $listener);
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
return $this->dispatcher->removeSubscriber($subscriber);
|
||||
$this->dispatcher->removeSubscriber($subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListeners(string $eventName = null): array
|
||||
public function getListeners(?string $eventName = null): array
|
||||
{
|
||||
return $this->dispatcher->getListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListenerPriority(string $eventName, callable|array $listener): ?int
|
||||
{
|
||||
// we might have wrapped listeners for the event (if called while dispatching)
|
||||
@@ -118,24 +113,16 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
return $this->dispatcher->getListenerPriority($eventName, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasListeners(string $eventName = null): bool
|
||||
public function hasListeners(?string $eventName = null): bool
|
||||
{
|
||||
return $this->dispatcher->hasListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatch(object $event, string $eventName = null): object
|
||||
public function dispatch(object $event, ?string $eventName = null): object
|
||||
{
|
||||
$eventName = $eventName ?? \get_class($event);
|
||||
$eventName ??= $event::class;
|
||||
|
||||
if (null === $this->callStack) {
|
||||
$this->callStack = new \SplObjectStorage();
|
||||
}
|
||||
$this->callStack ??= new \SplObjectStorage();
|
||||
|
||||
$currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
|
||||
|
||||
@@ -166,7 +153,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
return $event;
|
||||
}
|
||||
|
||||
public function getCalledListeners(Request $request = null): array
|
||||
public function getCalledListeners(?Request $request = null): array
|
||||
{
|
||||
if (null === $this->callStack) {
|
||||
return [];
|
||||
@@ -184,14 +171,12 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
return $called;
|
||||
}
|
||||
|
||||
public function getNotCalledListeners(Request $request = null): array
|
||||
public function getNotCalledListeners(?Request $request = null): array
|
||||
{
|
||||
try {
|
||||
$allListeners = $this->getListeners();
|
||||
$allListeners = $this->dispatcher instanceof EventDispatcher ? $this->getListenersWithPriority() : $this->getListenersWithoutPriority();
|
||||
} catch (\Exception $e) {
|
||||
if (null !== $this->logger) {
|
||||
$this->logger->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
|
||||
}
|
||||
$this->logger?->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
|
||||
|
||||
// unable to retrieve the uncalled listeners
|
||||
return [];
|
||||
@@ -211,23 +196,24 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
$notCalled = [];
|
||||
|
||||
foreach ($allListeners as $eventName => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
foreach ($listeners as [$listener, $priority]) {
|
||||
if (!\in_array($listener, $calledListeners, true)) {
|
||||
if (!$listener instanceof WrappedListener) {
|
||||
$listener = new WrappedListener($listener, null, $this->stopwatch, $this);
|
||||
$listener = new WrappedListener($listener, null, $this->stopwatch, $this, $priority);
|
||||
}
|
||||
$notCalled[] = $listener->getInfo($eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uasort($notCalled, [$this, 'sortNotCalledListeners']);
|
||||
uasort($notCalled, $this->sortNotCalledListeners(...));
|
||||
|
||||
return $notCalled;
|
||||
}
|
||||
|
||||
public function getOrphanedEvents(Request $request = null): array
|
||||
public function getOrphanedEvents(?Request $request = null): array
|
||||
{
|
||||
if ($request) {
|
||||
return $this->orphanedEvents[spl_object_hash($request)] ?? [];
|
||||
@@ -240,6 +226,9 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
return array_merge(...array_values($this->orphanedEvents));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->callStack = null;
|
||||
@@ -260,6 +249,8 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
|
||||
/**
|
||||
* Called before dispatching the event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function beforeDispatch(string $eventName, object $event)
|
||||
{
|
||||
@@ -267,6 +258,8 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
|
||||
/**
|
||||
* Called after dispatching the event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function afterDispatch(string $eventName, object $event)
|
||||
{
|
||||
@@ -308,9 +301,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
if ($listener->wasCalled()) {
|
||||
if (null !== $this->logger) {
|
||||
$this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
|
||||
}
|
||||
$this->logger?->debug('Notified event "{event}" to listener "{listener}".', $context);
|
||||
} else {
|
||||
$this->callStack->detach($listener);
|
||||
}
|
||||
@@ -320,16 +311,14 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
}
|
||||
|
||||
if ($listener->stoppedPropagation()) {
|
||||
if (null !== $this->logger) {
|
||||
$this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
|
||||
}
|
||||
$this->logger?->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
|
||||
|
||||
$skipped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function sortNotCalledListeners(array $a, array $b)
|
||||
private function sortNotCalledListeners(array $a, array $b): int
|
||||
{
|
||||
if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
|
||||
return $cmp;
|
||||
@@ -353,4 +342,34 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
private function getListenersWithPriority(): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
$allListeners = new \ReflectionProperty(EventDispatcher::class, 'listeners');
|
||||
|
||||
foreach ($allListeners->getValue($this->dispatcher) as $eventName => $listenersByPriority) {
|
||||
foreach ($listenersByPriority as $priority => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$result[$eventName][] = [$listener, $priority];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getListenersWithoutPriority(): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->getListeners() as $eventName => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$result[$eventName][] = [$listener, null];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,26 +26,29 @@ final class WrappedListener
|
||||
private string $name;
|
||||
private bool $called = false;
|
||||
private bool $stoppedPropagation = false;
|
||||
private $stopwatch;
|
||||
private $dispatcher;
|
||||
private Stopwatch $stopwatch;
|
||||
private ?EventDispatcherInterface $dispatcher;
|
||||
private string $pretty;
|
||||
private $stub;
|
||||
private string $callableRef;
|
||||
private ClassStub|string $stub;
|
||||
private ?int $priority = null;
|
||||
private static bool $hasClassStub;
|
||||
|
||||
public function __construct(callable|array $listener, ?string $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
|
||||
public function __construct(callable|array $listener, ?string $name, Stopwatch $stopwatch, ?EventDispatcherInterface $dispatcher = null, ?int $priority = null)
|
||||
{
|
||||
$this->listener = $listener;
|
||||
$this->optimizedListener = $listener instanceof \Closure ? $listener : (\is_callable($listener) ? \Closure::fromCallable($listener) : null);
|
||||
$this->optimizedListener = $listener instanceof \Closure ? $listener : (\is_callable($listener) ? $listener(...) : null);
|
||||
$this->stopwatch = $stopwatch;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->priority = $priority;
|
||||
|
||||
if (\is_array($listener)) {
|
||||
$this->name = \is_object($listener[0]) ? get_debug_type($listener[0]) : $listener[0];
|
||||
[$this->name, $this->callableRef] = $this->parseListener($listener);
|
||||
$this->pretty = $this->name.'::'.$listener[1];
|
||||
$this->callableRef .= '::'.$listener[1];
|
||||
} elseif ($listener instanceof \Closure) {
|
||||
$r = new \ReflectionFunction($listener);
|
||||
if (str_contains($r->name, '{closure}')) {
|
||||
if (str_contains($r->name, '{closure')) {
|
||||
$this->pretty = $this->name = 'closure';
|
||||
} elseif ($class = \PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass()) {
|
||||
$this->name = $class->name;
|
||||
@@ -58,6 +61,7 @@ final class WrappedListener
|
||||
} else {
|
||||
$this->name = get_debug_type($listener);
|
||||
$this->pretty = $this->name.'::__invoke';
|
||||
$this->callableRef = $listener::class.'::__invoke';
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
@@ -89,11 +93,11 @@ final class WrappedListener
|
||||
|
||||
public function getInfo(string $eventName): array
|
||||
{
|
||||
$this->stub ??= self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
|
||||
$this->stub ??= self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->callableRef ?? $this->listener) : $this->pretty.'()';
|
||||
|
||||
return [
|
||||
'event' => $eventName,
|
||||
'priority' => null !== $this->priority ? $this->priority : (null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null),
|
||||
'priority' => $this->priority ??= $this->dispatcher?->getListenerPriority($eventName, $this->listener),
|
||||
'pretty' => $this->pretty,
|
||||
'stub' => $this->stub,
|
||||
];
|
||||
@@ -104,18 +108,37 @@ final class WrappedListener
|
||||
$dispatcher = $this->dispatcher ?: $dispatcher;
|
||||
|
||||
$this->called = true;
|
||||
$this->priority = $dispatcher->getListenerPriority($eventName, $this->listener);
|
||||
$this->priority ??= $dispatcher->getListenerPriority($eventName, $this->listener);
|
||||
|
||||
$e = $this->stopwatch->start($this->name, 'event_listener');
|
||||
|
||||
($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher);
|
||||
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
try {
|
||||
($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher);
|
||||
} finally {
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
}
|
||||
}
|
||||
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
$this->stoppedPropagation = true;
|
||||
}
|
||||
}
|
||||
|
||||
private function parseListener(array $listener): array
|
||||
{
|
||||
if ($listener[0] instanceof \Closure) {
|
||||
foreach ((new \ReflectionFunction($listener[0]))->getAttributes(\Closure::class) as $attribute) {
|
||||
if ($name = $attribute->getArguments()['name'] ?? false) {
|
||||
return [$name, $attribute->getArguments()['class'] ?? $name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (\is_object($listener[0])) {
|
||||
return [get_debug_type($listener[0]), $listener[0]::class];
|
||||
}
|
||||
|
||||
return [$listener[0], $listener[0]];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('event_dispatcher') && !$container->hasAlias('event_dispatcher')) {
|
||||
@@ -73,7 +76,7 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$event['method'] = $event['method'] ?? '__invoke';
|
||||
$event['method'] ??= '__invoke';
|
||||
$event['event'] = $this->getEventFromTypeDeclaration($container, $id, $event['method']);
|
||||
}
|
||||
|
||||
@@ -83,17 +86,21 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
$event['method'] = 'on'.preg_replace_callback([
|
||||
'/(?<=\b|_)[a-z]/i',
|
||||
'/[^a-z0-9]/i',
|
||||
], function ($matches) { return strtoupper($matches[0]); }, $event['event']);
|
||||
], fn ($matches) => strtoupper($matches[0]), $event['event']);
|
||||
$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
|
||||
|
||||
if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
|
||||
if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method'])) {
|
||||
if (!$r->hasMethod('__invoke')) {
|
||||
throw new InvalidArgumentException(sprintf('None of the "%s" or "__invoke" methods exist for the service "%s". Please define the "method" attribute on "kernel.event_listener" tags.', $event['method'], $id));
|
||||
}
|
||||
|
||||
$event['method'] = '__invoke';
|
||||
}
|
||||
}
|
||||
|
||||
$dispatcherDefinition = $globalDispatcherDefinition;
|
||||
if (isset($event['dispatcher'])) {
|
||||
$dispatcherDefinition = $container->getDefinition($event['dispatcher']);
|
||||
$dispatcherDefinition = $container->findDefinition($event['dispatcher']);
|
||||
}
|
||||
|
||||
$dispatcherDefinition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);
|
||||
@@ -132,7 +139,7 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$dispatcherDefinitions[$attributes['dispatcher']] = $container->getDefinition($attributes['dispatcher']);
|
||||
$dispatcherDefinitions[$attributes['dispatcher']] = $container->findDefinition($attributes['dispatcher']);
|
||||
}
|
||||
|
||||
if (!$dispatcherDefinitions) {
|
||||
@@ -191,7 +198,7 @@ class ExtractingEventDispatcher extends EventDispatcher implements EventSubscrib
|
||||
public static array $aliases = [];
|
||||
public static string $subscriber;
|
||||
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0)
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
|
||||
{
|
||||
$this->listeners[] = [$eventName, $listener[1], $priority];
|
||||
}
|
||||
|
||||
@@ -42,12 +42,9 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatch(object $event, string $eventName = null): object
|
||||
public function dispatch(object $event, ?string $eventName = null): object
|
||||
{
|
||||
$eventName = $eventName ?? \get_class($event);
|
||||
$eventName ??= $event::class;
|
||||
|
||||
if (isset($this->optimized)) {
|
||||
$listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName));
|
||||
@@ -62,10 +59,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListeners(string $eventName = null): array
|
||||
public function getListeners(?string $eventName = null): array
|
||||
{
|
||||
if (null !== $eventName) {
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
@@ -88,9 +82,6 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
return array_filter($this->sorted);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListenerPriority(string $eventName, callable|array $listener): ?int
|
||||
{
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
@@ -99,14 +90,14 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
|
||||
if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
|
||||
$listener[0] = $listener[0]();
|
||||
$listener[1] = $listener[1] ?? '__invoke';
|
||||
$listener[1] ??= '__invoke';
|
||||
}
|
||||
|
||||
foreach ($this->listeners[$eventName] as $priority => &$listeners) {
|
||||
foreach ($listeners as &$v) {
|
||||
if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure && 2 >= \count($v)) {
|
||||
$v[0] = $v[0]();
|
||||
$v[1] = $v[1] ?? '__invoke';
|
||||
$v[1] ??= '__invoke';
|
||||
}
|
||||
if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
|
||||
return $priority;
|
||||
@@ -117,10 +108,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasListeners(string $eventName = null): bool
|
||||
public function hasListeners(?string $eventName = null): bool
|
||||
{
|
||||
if (null !== $eventName) {
|
||||
return !empty($this->listeners[$eventName]);
|
||||
@@ -136,7 +124,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0)
|
||||
{
|
||||
@@ -145,7 +133,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function removeListener(string $eventName, callable|array $listener)
|
||||
{
|
||||
@@ -155,14 +143,14 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
|
||||
if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
|
||||
$listener[0] = $listener[0]();
|
||||
$listener[1] = $listener[1] ?? '__invoke';
|
||||
$listener[1] ??= '__invoke';
|
||||
}
|
||||
|
||||
foreach ($this->listeners[$eventName] as $priority => &$listeners) {
|
||||
foreach ($listeners as $k => &$v) {
|
||||
if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure && 2 >= \count($v)) {
|
||||
$v[0] = $v[0]();
|
||||
$v[1] = $v[1] ?? '__invoke';
|
||||
$v[1] ??= '__invoke';
|
||||
}
|
||||
if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
|
||||
unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]);
|
||||
@@ -176,7 +164,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
@@ -194,7 +182,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
@@ -218,6 +206,8 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
* @param callable[] $listeners The event listeners
|
||||
* @param string $eventName The name of the event to dispatch
|
||||
* @param object $event The event object to pass to the event handlers/listeners
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function callListeners(iterable $listeners, string $eventName, object $event)
|
||||
{
|
||||
@@ -234,16 +224,16 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
/**
|
||||
* Sorts the internal list of listeners for the given event by priority.
|
||||
*/
|
||||
private function sortListeners(string $eventName)
|
||||
private function sortListeners(string $eventName): void
|
||||
{
|
||||
krsort($this->listeners[$eventName]);
|
||||
$this->sorted[$eventName] = [];
|
||||
|
||||
foreach ($this->listeners[$eventName] as &$listeners) {
|
||||
foreach ($listeners as $k => &$listener) {
|
||||
foreach ($listeners as &$listener) {
|
||||
if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
|
||||
$listener[0] = $listener[0]();
|
||||
$listener[1] = $listener[1] ?? '__invoke';
|
||||
$listener[1] ??= '__invoke';
|
||||
}
|
||||
$this->sorted[$eventName][] = $listener;
|
||||
}
|
||||
@@ -265,12 +255,12 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
$closure = static function (...$args) use (&$listener, &$closure) {
|
||||
if ($listener[0] instanceof \Closure) {
|
||||
$listener[0] = $listener[0]();
|
||||
$listener[1] = $listener[1] ?? '__invoke';
|
||||
$listener[1] ??= '__invoke';
|
||||
}
|
||||
($closure = \Closure::fromCallable($listener))(...$args);
|
||||
($closure = $listener(...))(...$args);
|
||||
};
|
||||
} else {
|
||||
$closure = $listener instanceof \Closure || $listener instanceof WrappedListener ? $listener : \Closure::fromCallable($listener);
|
||||
$closure = $listener instanceof WrappedListener ? $listener : $listener(...);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
|
||||
*
|
||||
* @param int $priority The higher this value, the earlier an event
|
||||
* listener will be triggered in the chain (defaults to 0)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addListener(string $eventName, callable $listener, int $priority = 0);
|
||||
|
||||
@@ -35,14 +37,21 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
|
||||
*
|
||||
* The subscriber is asked for all the events it is
|
||||
* interested in and added as a listener for these events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber);
|
||||
|
||||
/**
|
||||
* Removes an event listener from the specified events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function removeListener(string $eventName, callable $listener);
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber);
|
||||
|
||||
/**
|
||||
@@ -50,7 +59,7 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
|
||||
*
|
||||
* @return array<callable[]|callable>
|
||||
*/
|
||||
public function getListeners(string $eventName = null): array;
|
||||
public function getListeners(?string $eventName = null): array;
|
||||
|
||||
/**
|
||||
* Gets the listener priority for a specific event.
|
||||
@@ -62,5 +71,5 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
|
||||
/**
|
||||
* Checks whether an event has any registered listeners.
|
||||
*/
|
||||
public function hasListeners(string $eventName = null): bool;
|
||||
public function hasListeners(?string $eventName = null): bool;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
|
||||
protected $arguments;
|
||||
|
||||
/**
|
||||
* Encapsulate an event with $subject and $args.
|
||||
* Encapsulate an event with $subject and $arguments.
|
||||
*
|
||||
* @param mixed $subject The subject of the event, usually an object or a callable
|
||||
* @param array $arguments Arguments to store in the event
|
||||
|
||||
@@ -18,23 +18,20 @@ namespace Symfony\Component\EventDispatcher;
|
||||
*/
|
||||
class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
{
|
||||
private $dispatcher;
|
||||
private EventDispatcherInterface $dispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatch(object $event, string $eventName = null): object
|
||||
public function dispatch(object $event, ?string $eventName = null): object
|
||||
{
|
||||
return $this->dispatcher->dispatch($event, $eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return never
|
||||
*/
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0)
|
||||
{
|
||||
@@ -42,7 +39,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return never
|
||||
*/
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
@@ -50,7 +47,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return never
|
||||
*/
|
||||
public function removeListener(string $eventName, callable|array $listener)
|
||||
{
|
||||
@@ -58,33 +55,24 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return never
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListeners(string $eventName = null): array
|
||||
public function getListeners(?string $eventName = null): array
|
||||
{
|
||||
return $this->dispatcher->getListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListenerPriority(string $eventName, callable|array $listener): ?int
|
||||
{
|
||||
return $this->dispatcher->getListenerPriority($eventName, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasListeners(string $eventName = null): bool
|
||||
public function hasListeners(?string $eventName = null): bool
|
||||
{
|
||||
return $this->dispatcher->hasListeners($eventName);
|
||||
}
|
||||
|
||||
2
vendor/symfony/event-dispatcher/LICENSE
vendored
2
vendor/symfony/event-dispatcher/LICENSE
vendored
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2004-2023 Fabien Potencier
|
||||
Copyright (c) 2004-present Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
25
vendor/symfony/event-dispatcher/composer.json
vendored
25
vendor/symfony/event-dispatcher/composer.json
vendored
@@ -16,30 +16,27 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=8.0.2",
|
||||
"symfony/event-dispatcher-contracts": "^2|^3"
|
||||
"php": ">=8.1",
|
||||
"symfony/event-dispatcher-contracts": "^2.5|^3"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/dependency-injection": "^5.4|^6.0",
|
||||
"symfony/expression-language": "^5.4|^6.0",
|
||||
"symfony/config": "^5.4|^6.0",
|
||||
"symfony/error-handler": "^5.4|^6.0",
|
||||
"symfony/http-foundation": "^5.4|^6.0",
|
||||
"symfony/service-contracts": "^1.1|^2|^3",
|
||||
"symfony/stopwatch": "^5.4|^6.0",
|
||||
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
|
||||
"symfony/expression-language": "^5.4|^6.0|^7.0",
|
||||
"symfony/config": "^5.4|^6.0|^7.0",
|
||||
"symfony/error-handler": "^5.4|^6.0|^7.0",
|
||||
"symfony/http-foundation": "^5.4|^6.0|^7.0",
|
||||
"symfony/service-contracts": "^2.5|^3",
|
||||
"symfony/stopwatch": "^5.4|^6.0|^7.0",
|
||||
"psr/log": "^1|^2|^3"
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/dependency-injection": "<5.4"
|
||||
"symfony/dependency-injection": "<5.4",
|
||||
"symfony/service-contracts": "<2.5"
|
||||
},
|
||||
"provide": {
|
||||
"psr/event-dispatcher-implementation": "1.0",
|
||||
"symfony/event-dispatcher-implementation": "2.0|3.0"
|
||||
},
|
||||
"suggest": {
|
||||
"symfony/dependency-injection": "",
|
||||
"symfony/http-kernel": ""
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Component\\EventDispatcher\\": "" },
|
||||
"exclude-from-classmap": [
|
||||
|
||||
Reference in New Issue
Block a user