package and depencies

This commit is contained in:
RafficMohammed
2023-01-08 02:57:24 +05:30
parent d5332eb421
commit 1d54b8bc7f
4309 changed files with 193331 additions and 172289 deletions

View File

@@ -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;
@@ -33,45 +34,34 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
protected $stopwatch;
/**
* @var \SplObjectStorage<WrappedListener, array{string, string}>
* @var \SplObjectStorage<WrappedListener, array{string, string}>|null
*/
private $callStack;
private $dispatcher;
private $wrappedListeners;
private $orphanedEvents;
private $requestStack;
private $currentRequestHash = '';
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->wrappedListeners = [];
$this->orphanedEvents = [];
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function addListener(string $eventName, $listener, int $priority = 0)
public function addListener(string $eventName, callable|array $listener, int $priority = 0)
{
$this->dispatcher->addListener($eventName, $listener, $priority);
}
/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
$this->dispatcher->addSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function removeListener(string $eventName, $listener)
public function removeListener(string $eventName, callable|array $listener)
{
if (isset($this->wrappedListeners[$eventName])) {
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
@@ -86,26 +76,17 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $this->dispatcher->removeListener($eventName, $listener);
}
/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
return $this->dispatcher->removeSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function getListeners(string $eventName = null)
public function getListeners(string $eventName = null): array
{
return $this->dispatcher->getListeners($eventName);
}
/**
* {@inheritdoc}
*/
public function getListenerPriority(string $eventName, $listener)
public function getListenerPriority(string $eventName, callable|array $listener): ?int
{
// we might have wrapped listeners for the event (if called while dispatching)
// in that case get the priority by wrapper
@@ -120,24 +101,16 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $this->dispatcher->getListenerPriority($eventName, $listener);
}
/**
* {@inheritdoc}
*/
public function hasListeners(string $eventName = null)
public function hasListeners(string $eventName = null): bool
{
return $this->dispatcher->hasListeners($eventName);
}
/**
* {@inheritdoc}
*/
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) : '';
@@ -168,10 +141,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $event;
}
/**
* @return array
*/
public function getCalledListeners(Request $request = null)
public function getCalledListeners(Request $request = null): array
{
if (null === $this->callStack) {
return [];
@@ -189,17 +159,12 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return $called;
}
/**
* @return array
*/
public function getNotCalledListeners(Request $request = null)
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 [];
@@ -219,18 +184,19 @@ 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;
}
@@ -260,10 +226,8 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
*
* @param string $method The method name
* @param array $arguments The method arguments
*
* @return mixed
*/
public function __call(string $method, array $arguments)
public function __call(string $method, array $arguments): mixed
{
return $this->dispatcher->{$method}(...$arguments);
}
@@ -318,9 +282,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);
}
@@ -330,16 +292,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;
@@ -363,4 +323,35 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
return 1;
}
private function getListenersWithPriority(): array
{
$result = [];
$allListeners = new \ReflectionProperty(EventDispatcher::class, 'listeners');
$allListeners->setAccessible(true);
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;
}
}