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

@@ -19,6 +19,12 @@ namespace Symfony\Component\EventDispatcher\Attribute;
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class AsEventListener
{
/**
* @param string|null $event The event name to listen to
* @param string|null $method The method to run when the listened event is triggered
* @param int $priority The priority of this listener if several are declared for the same event
* @param string|null $dispatcher The service id of the event dispatcher to listen to
*/
public function __construct(
public ?string $event = null,
public ?string $method = null,

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;

View File

@@ -21,11 +21,9 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
*/
class AddEventAliasesPass implements CompilerPassInterface
{
private array $eventAliases;
public function __construct(array $eventAliases)
{
$this->eventAliases = $eventAliases;
public function __construct(
private array $eventAliases,
) {
}
public function process(ContainerBuilder $container): void

View File

@@ -88,7 +88,7 @@ class RegisterListenersPass implements CompilerPassInterface
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));
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';
@@ -123,10 +123,10 @@ class RegisterListenersPass implements CompilerPassInterface
$class = $def->getClass();
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}
if (!$r->isSubclassOf(EventSubscriberInterface::class)) {
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
throw new InvalidArgumentException(\sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
}
$class = $r->name;
@@ -178,7 +178,7 @@ class RegisterListenersPass implements CompilerPassInterface
|| $type->isBuiltin()
|| Event::class === ($name = $type->getName())
) {
throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id));
throw new InvalidArgumentException(\sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id));
}
return $name;

View File

@@ -42,7 +42,7 @@ class EventDispatcher implements EventDispatcherInterface
}
}
public function dispatch(object $event, string $eventName = null): object
public function dispatch(object $event, ?string $eventName = null): object
{
$eventName ??= $event::class;
@@ -59,7 +59,7 @@ class EventDispatcher implements EventDispatcherInterface
return $event;
}
public function getListeners(string $eventName = null): array
public function getListeners(?string $eventName = null): array
{
if (null !== $eventName) {
if (empty($this->listeners[$eventName])) {
@@ -108,7 +108,7 @@ class EventDispatcher implements EventDispatcherInterface
return null;
}
public function hasListeners(string $eventName = null): bool
public function hasListeners(?string $eventName = null): bool
{
if (null !== $eventName) {
return !empty($this->listeners[$eventName]);

View File

@@ -50,7 +50,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 +62,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;
}

View File

@@ -25,19 +25,16 @@ use Symfony\Contracts\EventDispatcher\Event;
*/
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
{
protected mixed $subject;
protected array $arguments;
/**
* 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
*/
public function __construct(mixed $subject = null, array $arguments = [])
{
$this->subject = $subject;
$this->arguments = $arguments;
public function __construct(
protected mixed $subject = null,
protected array $arguments = [],
) {
}
/**
@@ -59,7 +56,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
return $this->arguments[$key];
}
throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
throw new \InvalidArgumentException(\sprintf('Argument "%s" not found.', $key));
}
/**

View File

@@ -18,14 +18,12 @@ namespace Symfony\Component\EventDispatcher;
*/
class ImmutableEventDispatcher implements EventDispatcherInterface
{
private EventDispatcherInterface $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
public function __construct(
private EventDispatcherInterface $dispatcher,
) {
}
public function dispatch(object $event, string $eventName = null): object
public function dispatch(object $event, ?string $eventName = null): object
{
return $this->dispatcher->dispatch($event, $eventName);
}
@@ -50,7 +48,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}
public function getListeners(string $eventName = null): array
public function getListeners(?string $eventName = null): array
{
return $this->dispatcher->getListeners($eventName);
}
@@ -60,7 +58,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
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);
}