This commit is contained in:
2025-05-12 14:25:25 +02:00
parent ab2db755ef
commit 9e378ca2b7
2719 changed files with 46505 additions and 60181 deletions

View File

@@ -30,25 +30,20 @@ use Symfony\Contracts\Service\ResetInterface;
*/
class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterface
{
protected ?LoggerInterface $logger;
protected Stopwatch $stopwatch;
/**
* @var \SplObjectStorage<WrappedListener, array{string, string}>|null
*/
private ?\SplObjectStorage $callStack = null;
private EventDispatcherInterface $dispatcher;
private array $wrappedListeners = [];
private array $orphanedEvents = [];
private ?RequestStack $requestStack;
private string $currentRequestHash = '';
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null, RequestStack $requestStack = null)
{
$this->dispatcher = $dispatcher;
$this->stopwatch = $stopwatch;
$this->logger = $logger;
$this->requestStack = $requestStack;
public function __construct(
private EventDispatcherInterface $dispatcher,
protected Stopwatch $stopwatch,
protected ?LoggerInterface $logger = null,
private ?RequestStack $requestStack = null,
) {
}
public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
@@ -81,7 +76,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
$this->dispatcher->removeSubscriber($subscriber);
}
public function getListeners(string $eventName = null): array
public function getListeners(?string $eventName = null): array
{
return $this->dispatcher->getListeners($eventName);
}
@@ -101,12 +96,12 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $this->dispatcher->getListenerPriority($eventName, $listener);
}
public function hasListeners(string $eventName = null): bool
public function hasListeners(?string $eventName = null): bool
{
return $this->dispatcher->hasListeners($eventName);
}
public function dispatch(object $event, string $eventName = null): object
public function dispatch(object $event, ?string $eventName = null): object
{
$eventName ??= $event::class;
@@ -115,7 +110,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
$currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
if (null !== $this->logger && $event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
$this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
$this->logger->debug(\sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
}
$this->preProcess($eventName);
@@ -141,7 +136,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 [];
@@ -159,7 +154,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $called;
}
public function getNotCalledListeners(Request $request = null): array
public function getNotCalledListeners(?Request $request = null): array
{
try {
$allListeners = $this->dispatcher instanceof EventDispatcher ? $this->getListenersWithPriority() : $this->getListenersWithoutPriority();
@@ -201,7 +196,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
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)] ?? [];

View File

@@ -26,21 +26,20 @@ final class WrappedListener
private string $name;
private bool $called = false;
private bool $stoppedPropagation = false;
private Stopwatch $stopwatch;
private ?EventDispatcherInterface $dispatcher;
private string $pretty;
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, int $priority = null)
{
public function __construct(
callable|array $listener,
?string $name,
private Stopwatch $stopwatch,
private ?EventDispatcherInterface $dispatcher = null,
private ?int $priority = null,
) {
$this->listener = $listener;
$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, $this->callableRef] = $this->parseListener($listener);
@@ -48,7 +47,7 @@ final class WrappedListener
$this->callableRef .= '::'.$listener[1];
} elseif ($listener instanceof \Closure) {
$r = new \ReflectionFunction($listener);
if (str_contains($r->name, '{closure}')) {
if ($r->isAnonymous()) {
$this->pretty = $this->name = 'closure';
} elseif ($class = $r->getClosureCalledClass()) {
$this->name = $class->name;