This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -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(...);
}
}
}