mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-13 19:51:30 +00:00
resolved conflicts
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Attribute;
|
||||
|
||||
/**
|
||||
* Service tag to autoconfigure event listeners.
|
||||
*
|
||||
* @author Alexander M. Turek <me@derrabus.de>
|
||||
*/
|
||||
#[\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,
|
||||
public int $priority = 0,
|
||||
public ?string $dispatcher = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
+351
@@ -0,0 +1,351 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
use Symfony\Contracts\Service\ResetInterface;
|
||||
|
||||
/**
|
||||
* Collects some data about event listeners.
|
||||
*
|
||||
* This event dispatcher delegates the dispatching to another one.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterface
|
||||
{
|
||||
/**
|
||||
* @var \SplObjectStorage<WrappedListener, array{string, string}>|null
|
||||
*/
|
||||
private ?\SplObjectStorage $callStack = null;
|
||||
private array $wrappedListeners = [];
|
||||
private array $orphanedEvents = [];
|
||||
private string $currentRequestHash = '';
|
||||
|
||||
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
|
||||
{
|
||||
$this->dispatcher->addListener($eventName, $listener, $priority);
|
||||
}
|
||||
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber): void
|
||||
{
|
||||
$this->dispatcher->addSubscriber($subscriber);
|
||||
}
|
||||
|
||||
public function removeListener(string $eventName, callable|array $listener): void
|
||||
{
|
||||
if (isset($this->wrappedListeners[$eventName])) {
|
||||
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
|
||||
if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) {
|
||||
$listener = $wrappedListener;
|
||||
unset($this->wrappedListeners[$eventName][$index]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
}
|
||||
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber): void
|
||||
{
|
||||
$this->dispatcher->removeSubscriber($subscriber);
|
||||
}
|
||||
|
||||
public function getListeners(?string $eventName = null): array
|
||||
{
|
||||
return $this->dispatcher->getListeners($eventName);
|
||||
}
|
||||
|
||||
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
|
||||
if (isset($this->wrappedListeners[$eventName])) {
|
||||
foreach ($this->wrappedListeners[$eventName] as $wrappedListener) {
|
||||
if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) {
|
||||
return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->dispatcher->getListenerPriority($eventName, $listener);
|
||||
}
|
||||
|
||||
public function hasListeners(?string $eventName = null): bool
|
||||
{
|
||||
return $this->dispatcher->hasListeners($eventName);
|
||||
}
|
||||
|
||||
public function dispatch(object $event, ?string $eventName = null): object
|
||||
{
|
||||
$eventName ??= $event::class;
|
||||
|
||||
$this->callStack ??= new \SplObjectStorage();
|
||||
|
||||
$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->preProcess($eventName);
|
||||
try {
|
||||
$this->beforeDispatch($eventName, $event);
|
||||
try {
|
||||
$e = $this->stopwatch->start($eventName, 'section');
|
||||
try {
|
||||
$this->dispatcher->dispatch($event, $eventName);
|
||||
} finally {
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
$this->afterDispatch($eventName, $event);
|
||||
}
|
||||
} finally {
|
||||
$this->currentRequestHash = $currentRequestHash;
|
||||
$this->postProcess($eventName);
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
public function getCalledListeners(?Request $request = null): array
|
||||
{
|
||||
if (null === $this->callStack) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$hash = $request ? spl_object_hash($request) : null;
|
||||
$called = [];
|
||||
foreach ($this->callStack as $listener) {
|
||||
[$eventName, $requestHash] = $this->callStack->getInfo();
|
||||
if (null === $hash || $hash === $requestHash) {
|
||||
$called[] = $listener->getInfo($eventName);
|
||||
}
|
||||
}
|
||||
|
||||
return $called;
|
||||
}
|
||||
|
||||
public function getNotCalledListeners(?Request $request = null): array
|
||||
{
|
||||
try {
|
||||
$allListeners = $this->dispatcher instanceof EventDispatcher ? $this->getListenersWithPriority() : $this->getListenersWithoutPriority();
|
||||
} catch (\Exception $e) {
|
||||
$this->logger?->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
|
||||
|
||||
// unable to retrieve the uncalled listeners
|
||||
return [];
|
||||
}
|
||||
|
||||
$hash = $request ? spl_object_hash($request) : null;
|
||||
$calledListeners = [];
|
||||
|
||||
if (null !== $this->callStack) {
|
||||
foreach ($this->callStack as $calledListener) {
|
||||
[, $requestHash] = $this->callStack->getInfo();
|
||||
|
||||
if (null === $hash || $hash === $requestHash) {
|
||||
$calledListeners[] = $calledListener->getWrappedListener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$notCalled = [];
|
||||
|
||||
foreach ($allListeners as $eventName => $listeners) {
|
||||
foreach ($listeners as [$listener, $priority]) {
|
||||
if (!\in_array($listener, $calledListeners, true)) {
|
||||
if (!$listener instanceof WrappedListener) {
|
||||
$listener = new WrappedListener($listener, null, $this->stopwatch, $this, $priority);
|
||||
}
|
||||
$notCalled[] = $listener->getInfo($eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uasort($notCalled, $this->sortNotCalledListeners(...));
|
||||
|
||||
return $notCalled;
|
||||
}
|
||||
|
||||
public function getOrphanedEvents(?Request $request = null): array
|
||||
{
|
||||
if ($request) {
|
||||
return $this->orphanedEvents[spl_object_hash($request)] ?? [];
|
||||
}
|
||||
|
||||
if (!$this->orphanedEvents) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_merge(...array_values($this->orphanedEvents));
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->callStack = null;
|
||||
$this->orphanedEvents = [];
|
||||
$this->currentRequestHash = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxies all method calls to the original event dispatcher.
|
||||
*
|
||||
* @param string $method The method name
|
||||
* @param array $arguments The method arguments
|
||||
*/
|
||||
public function __call(string $method, array $arguments): mixed
|
||||
{
|
||||
return $this->dispatcher->{$method}(...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called before dispatching the event.
|
||||
*/
|
||||
protected function beforeDispatch(string $eventName, object $event): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after dispatching the event.
|
||||
*/
|
||||
protected function afterDispatch(string $eventName, object $event): void
|
||||
{
|
||||
}
|
||||
|
||||
private function preProcess(string $eventName): void
|
||||
{
|
||||
if (!$this->dispatcher->hasListeners($eventName)) {
|
||||
$this->orphanedEvents[$this->currentRequestHash][] = $eventName;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
|
||||
$priority = $this->getListenerPriority($eventName, $listener);
|
||||
$wrappedListener = new WrappedListener($listener instanceof WrappedListener ? $listener->getWrappedListener() : $listener, null, $this->stopwatch, $this);
|
||||
$this->wrappedListeners[$eventName][] = $wrappedListener;
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
$this->dispatcher->addListener($eventName, $wrappedListener, $priority);
|
||||
$this->callStack->attach($wrappedListener, [$eventName, $this->currentRequestHash]);
|
||||
}
|
||||
}
|
||||
|
||||
private function postProcess(string $eventName): void
|
||||
{
|
||||
unset($this->wrappedListeners[$eventName]);
|
||||
$skipped = false;
|
||||
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
|
||||
if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
|
||||
continue;
|
||||
}
|
||||
// Unwrap listener
|
||||
$priority = $this->getListenerPriority($eventName, $listener);
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
$this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
|
||||
|
||||
if (null !== $this->logger) {
|
||||
$context = ['event' => $eventName, 'listener' => $listener->getPretty()];
|
||||
}
|
||||
|
||||
if ($listener->wasCalled()) {
|
||||
$this->logger?->debug('Notified event "{event}" to listener "{listener}".', $context);
|
||||
} else {
|
||||
$this->callStack->detach($listener);
|
||||
}
|
||||
|
||||
if (null !== $this->logger && $skipped) {
|
||||
$this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
|
||||
}
|
||||
|
||||
if ($listener->stoppedPropagation()) {
|
||||
$this->logger?->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
|
||||
|
||||
$skipped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function sortNotCalledListeners(array $a, array $b): int
|
||||
{
|
||||
if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
|
||||
return $cmp;
|
||||
}
|
||||
|
||||
if (\is_int($a['priority']) && !\is_int($b['priority'])) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!\is_int($a['priority']) && \is_int($b['priority'])) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ($a['priority'] === $b['priority']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($a['priority'] > $b['priority']) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Debug;
|
||||
|
||||
use Psr\EventDispatcher\StoppableEventInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
use Symfony\Component\VarDumper\Caster\ClassStub;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
final class WrappedListener
|
||||
{
|
||||
private string|array|object $listener;
|
||||
private ?\Closure $optimizedListener;
|
||||
private string $name;
|
||||
private bool $called = false;
|
||||
private bool $stoppedPropagation = false;
|
||||
private string $pretty;
|
||||
private string $callableRef;
|
||||
private ClassStub|string $stub;
|
||||
private static bool $hasClassStub;
|
||||
|
||||
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);
|
||||
|
||||
if (\is_array($listener)) {
|
||||
[$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 ($r->isAnonymous()) {
|
||||
$this->pretty = $this->name = 'closure';
|
||||
} elseif ($class = $r->getClosureCalledClass()) {
|
||||
$this->name = $class->name;
|
||||
$this->pretty = $this->name.'::'.$r->name;
|
||||
} else {
|
||||
$this->pretty = $this->name = $r->name;
|
||||
}
|
||||
} elseif (\is_string($listener)) {
|
||||
$this->pretty = $this->name = $listener;
|
||||
} else {
|
||||
$this->name = get_debug_type($listener);
|
||||
$this->pretty = $this->name.'::__invoke';
|
||||
$this->callableRef = $listener::class.'::__invoke';
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
self::$hasClassStub ??= class_exists(ClassStub::class);
|
||||
}
|
||||
|
||||
public function getWrappedListener(): callable|array
|
||||
{
|
||||
return $this->listener;
|
||||
}
|
||||
|
||||
public function wasCalled(): bool
|
||||
{
|
||||
return $this->called;
|
||||
}
|
||||
|
||||
public function stoppedPropagation(): bool
|
||||
{
|
||||
return $this->stoppedPropagation;
|
||||
}
|
||||
|
||||
public function getPretty(): string
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
|
||||
public function getInfo(string $eventName): array
|
||||
{
|
||||
$this->stub ??= self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->callableRef ?? $this->listener) : $this->pretty.'()';
|
||||
|
||||
return [
|
||||
'event' => $eventName,
|
||||
'priority' => $this->priority ??= $this->dispatcher?->getListenerPriority($eventName, $this->listener),
|
||||
'pretty' => $this->pretty,
|
||||
'stub' => $this->stub,
|
||||
];
|
||||
}
|
||||
|
||||
public function __invoke(object $event, string $eventName, EventDispatcherInterface $dispatcher): void
|
||||
{
|
||||
$dispatcher = $this->dispatcher ?: $dispatcher;
|
||||
|
||||
$this->called = true;
|
||||
$this->priority ??= $dispatcher->getListenerPriority($eventName, $this->listener);
|
||||
|
||||
$e = $this->stopwatch->start($this->name, 'event_listener');
|
||||
|
||||
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]];
|
||||
}
|
||||
}
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
/**
|
||||
* This pass allows bundles to extend the list of event aliases.
|
||||
*
|
||||
* @author Alexander M. Turek <me@derrabus.de>
|
||||
*/
|
||||
class AddEventAliasesPass implements CompilerPassInterface
|
||||
{
|
||||
public function __construct(
|
||||
private array $eventAliases,
|
||||
) {
|
||||
}
|
||||
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
$eventAliases = $container->hasParameter('event_dispatcher.event_aliases') ? $container->getParameter('event_dispatcher.event_aliases') : [];
|
||||
|
||||
$container->setParameter(
|
||||
'event_dispatcher.event_aliases',
|
||||
array_merge($eventAliases, $this->eventAliases)
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+213
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Compiler pass to register tagged services for an event dispatcher.
|
||||
*/
|
||||
class RegisterListenersPass implements CompilerPassInterface
|
||||
{
|
||||
private array $hotPathEvents = [];
|
||||
private array $noPreloadEvents = [];
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setHotPathEvents(array $hotPathEvents): static
|
||||
{
|
||||
$this->hotPathEvents = array_flip($hotPathEvents);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setNoPreloadEvents(array $noPreloadEvents): static
|
||||
{
|
||||
$this->noPreloadEvents = array_flip($noPreloadEvents);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
if (!$container->hasDefinition('event_dispatcher') && !$container->hasAlias('event_dispatcher')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$aliases = [];
|
||||
|
||||
if ($container->hasParameter('event_dispatcher.event_aliases')) {
|
||||
$aliases = $container->getParameter('event_dispatcher.event_aliases');
|
||||
}
|
||||
|
||||
$globalDispatcherDefinition = $container->findDefinition('event_dispatcher');
|
||||
|
||||
foreach ($container->findTaggedServiceIds('kernel.event_listener', true) as $id => $events) {
|
||||
$noPreload = 0;
|
||||
|
||||
foreach ($events as $event) {
|
||||
$priority = $event['priority'] ?? 0;
|
||||
|
||||
if (!isset($event['event'])) {
|
||||
if ($container->getDefinition($id)->hasTag('kernel.event_subscriber')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event['method'] ??= '__invoke';
|
||||
$event['event'] = $this->getEventFromTypeDeclaration($container, $id, $event['method']);
|
||||
}
|
||||
|
||||
$event['event'] = $aliases[$event['event']] ?? $event['event'];
|
||||
|
||||
if (!isset($event['method'])) {
|
||||
$event['method'] = 'on'.preg_replace_callback([
|
||||
'/(?<=\b|_)[a-z]/i',
|
||||
'/[^a-z0-9]/i',
|
||||
], 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'])) {
|
||||
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->findDefinition($event['dispatcher']);
|
||||
}
|
||||
|
||||
$dispatcherDefinition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);
|
||||
|
||||
if (isset($this->hotPathEvents[$event['event']])) {
|
||||
$container->getDefinition($id)->addTag('container.hot_path');
|
||||
} elseif (isset($this->noPreloadEvents[$event['event']])) {
|
||||
++$noPreload;
|
||||
}
|
||||
}
|
||||
|
||||
if ($noPreload && \count($events) === $noPreload) {
|
||||
$container->getDefinition($id)->addTag('container.no_preload');
|
||||
}
|
||||
}
|
||||
|
||||
$extractingDispatcher = new ExtractingEventDispatcher();
|
||||
|
||||
foreach ($container->findTaggedServiceIds('kernel.event_subscriber', true) as $id => $tags) {
|
||||
$def = $container->getDefinition($id);
|
||||
|
||||
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
||||
$class = $def->getClass();
|
||||
|
||||
if (!$r = $container->getReflectionClass($class)) {
|
||||
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));
|
||||
}
|
||||
$class = $r->name;
|
||||
|
||||
$dispatcherDefinitions = [];
|
||||
foreach ($tags as $attributes) {
|
||||
if (!isset($attributes['dispatcher']) || isset($dispatcherDefinitions[$attributes['dispatcher']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dispatcherDefinitions[$attributes['dispatcher']] = $container->findDefinition($attributes['dispatcher']);
|
||||
}
|
||||
|
||||
if (!$dispatcherDefinitions) {
|
||||
$dispatcherDefinitions = [$globalDispatcherDefinition];
|
||||
}
|
||||
|
||||
$noPreload = 0;
|
||||
ExtractingEventDispatcher::$aliases = $aliases;
|
||||
ExtractingEventDispatcher::$subscriber = $class;
|
||||
$extractingDispatcher->addSubscriber($extractingDispatcher);
|
||||
foreach ($extractingDispatcher->listeners as $args) {
|
||||
$args[1] = [new ServiceClosureArgument(new Reference($id)), $args[1]];
|
||||
foreach ($dispatcherDefinitions as $dispatcherDefinition) {
|
||||
$dispatcherDefinition->addMethodCall('addListener', $args);
|
||||
}
|
||||
|
||||
if (isset($this->hotPathEvents[$args[0]])) {
|
||||
$container->getDefinition($id)->addTag('container.hot_path');
|
||||
} elseif (isset($this->noPreloadEvents[$args[0]])) {
|
||||
++$noPreload;
|
||||
}
|
||||
}
|
||||
if ($noPreload && \count($extractingDispatcher->listeners) === $noPreload) {
|
||||
$container->getDefinition($id)->addTag('container.no_preload');
|
||||
}
|
||||
$extractingDispatcher->listeners = [];
|
||||
ExtractingEventDispatcher::$aliases = [];
|
||||
}
|
||||
}
|
||||
|
||||
private function getEventFromTypeDeclaration(ContainerBuilder $container, string $id, string $method): string
|
||||
{
|
||||
if (
|
||||
null === ($class = $container->getDefinition($id)->getClass())
|
||||
|| !($r = $container->getReflectionClass($class, false))
|
||||
|| !$r->hasMethod($method)
|
||||
|| 1 > ($m = $r->getMethod($method))->getNumberOfParameters()
|
||||
|| !($type = $m->getParameters()[0]->getType()) instanceof \ReflectionNamedType
|
||||
|| $type->isBuiltin()
|
||||
|| Event::class === ($name = $type->getName())
|
||||
) {
|
||||
throw new InvalidArgumentException(\sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id));
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ExtractingEventDispatcher extends EventDispatcher implements EventSubscriberInterface
|
||||
{
|
||||
public array $listeners = [];
|
||||
|
||||
public static array $aliases = [];
|
||||
public static string $subscriber;
|
||||
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
|
||||
{
|
||||
$this->listeners[] = [$eventName, $listener[1], $priority];
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
$events = [];
|
||||
|
||||
foreach ([self::$subscriber, 'getSubscribedEvents']() as $eventName => $params) {
|
||||
$events[self::$aliases[$eventName] ?? $eventName] = $params;
|
||||
}
|
||||
|
||||
return $events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Event encapsulation class.
|
||||
*
|
||||
* Encapsulates events thus decoupling the observer from the subject they encapsulate.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @implements \ArrayAccess<string, mixed>
|
||||
* @implements \IteratorAggregate<string, mixed>
|
||||
*/
|
||||
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* 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(
|
||||
protected mixed $subject = null,
|
||||
protected array $arguments = [],
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for subject property.
|
||||
*/
|
||||
public function getSubject(): mixed
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get argument by key.
|
||||
*
|
||||
* @throws \InvalidArgumentException if key is not found
|
||||
*/
|
||||
public function getArgument(string $key): mixed
|
||||
{
|
||||
if ($this->hasArgument($key)) {
|
||||
return $this->arguments[$key];
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(\sprintf('Argument "%s" not found.', $key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add argument to event.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setArgument(string $key, mixed $value): static
|
||||
{
|
||||
$this->arguments[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for all arguments.
|
||||
*/
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set args property.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setArguments(array $args = []): static
|
||||
{
|
||||
$this->arguments = $args;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has argument.
|
||||
*/
|
||||
public function hasArgument(string $key): bool
|
||||
{
|
||||
return \array_key_exists($key, $this->arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess for argument getter.
|
||||
*
|
||||
* @param string $key Array key
|
||||
*
|
||||
* @throws \InvalidArgumentException if key does not exist in $this->args
|
||||
*/
|
||||
public function offsetGet(mixed $key): mixed
|
||||
{
|
||||
return $this->getArgument($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess for argument setter.
|
||||
*
|
||||
* @param string $key Array key to set
|
||||
*/
|
||||
public function offsetSet(mixed $key, mixed $value): void
|
||||
{
|
||||
$this->setArgument($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess for unset argument.
|
||||
*
|
||||
* @param string $key Array key
|
||||
*/
|
||||
public function offsetUnset(mixed $key): void
|
||||
{
|
||||
if ($this->hasArgument($key)) {
|
||||
unset($this->arguments[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess has argument.
|
||||
*
|
||||
* @param string $key Array key
|
||||
*/
|
||||
public function offsetExists(mixed $key): bool
|
||||
{
|
||||
return $this->hasArgument($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* IteratorAggregate for iterating over the object like an array.
|
||||
*
|
||||
* @return \ArrayIterator<string, mixed>
|
||||
*/
|
||||
public function getIterator(): \ArrayIterator
|
||||
{
|
||||
return new \ArrayIterator($this->arguments);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher;
|
||||
|
||||
/**
|
||||
* A read-only proxy for an event dispatcher.
|
||||
*
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
public function dispatch(object $event, ?string $eventName = null): object
|
||||
{
|
||||
return $this->dispatcher->dispatch($event, $eventName);
|
||||
}
|
||||
|
||||
public function addListener(string $eventName, callable|array $listener, int $priority = 0): never
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber): never
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
public function removeListener(string $eventName, callable|array $listener): never
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber): never
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
public function getListeners(?string $eventName = null): array
|
||||
{
|
||||
return $this->dispatcher->getListeners($eventName);
|
||||
}
|
||||
|
||||
public function getListenerPriority(string $eventName, callable|array $listener): ?int
|
||||
{
|
||||
return $this->dispatcher->getListenerPriority($eventName, $listener);
|
||||
}
|
||||
|
||||
public function hasListeners(?string $eventName = null): bool
|
||||
{
|
||||
return $this->dispatcher->hasListeners($eventName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user