getListeners example

->expects($this->once())
            ->method('getListeners')
            ->with($request)
            ->willReturn([[$listener], null, null]);

        $firewall = new TraceableFirewallListener($firewallMapnew EventDispatcher()new LogoutUrlGenerator());
        $firewall->onKernelRequest($event);

        $collector = new SecurityDataCollector(null, null, null, null, $firewallMap$firewall, true);
        $collector->collect($request$response);

        $this->assertNotEmpty($collected = $collector->getListeners()[0]);
        $collector->lateCollect();
        $this->assertSame(1, $listenerCalled);
    }

    public function testCollectCollectsDecisionLogWhenStrategyIsAffirmative()
    {
        $voter1 = new DummyVoter();
        $voter2 = new DummyVoter();

        $decoratedVoter1 = new TraceableVoter($voter1new class() implements EventDispatcherInterface {
            public function dispatch(object $event, string $eventName = null): object
            {
return $dom;
    }

    private function getEventDispatcherListenersDocument(EventDispatcherInterface $eventDispatcher, array $options): \DOMDocument
    {
        $event = \array_key_exists('event', $options) ? $options['event'] : null;
        $dom = new \DOMDocument('1.0', 'UTF-8');
        $dom->appendChild($eventDispatcherXML = $dom->createElement('event-dispatcher'));

        if (null !== $event) {
            $registeredListeners = $eventDispatcher->getListeners($event);
            $this->appendEventListenerDocument($eventDispatcher$event$eventDispatcherXML$registeredListeners);
        } else {
            // Try to see if "events" exists             $registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events']array_map(fn ($event) => $eventDispatcher->getListeners($event)$options['events'])) : $eventDispatcher->getListeners();
            ksort($registeredListeners);

            foreach ($registeredListeners as $eventListened => $eventListeners) {
                $eventDispatcherXML->appendChild($eventXML = $dom->createElement('event'));
                $eventXML->setAttribute('name', $eventListened);

                $this->appendEventListenerDocument($eventDispatcher$eventListened$eventXML$eventListeners);
            }


    /** * {@inheritdoc} */
    public function getListeners($eventName)
    {
        $eventName = strtolower($eventName);

        $this->lazyLoad($eventName);

        return parent::getListeners($eventName);
    }

    /** * Adds a service as event subscriber. * * @param string $serviceId The service ID of the subscriber service * @param class-string<SubscriberInterface> $class The service's class name (which must implement EventSubscriberInterface) */
    public function addSubscriberService($serviceId$class)
    {
        foreach ($class::getSubscribedEvents() as $eventName => $params) {
            
public function testListenerCanRemoveItselfWhenExecuted()
    {
        $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher()new Stopwatch());
        $listener1 = function D) use ($eventDispatcher, &$listener1) {
            $eventDispatcher->removeListener('foo', $listener1);
        };
        $eventDispatcher->addListener('foo', $listener1);
        $eventDispatcher->addListener('foo', function D) {});
        $eventDispatcher->dispatch(new Event(), 'foo');

        $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
    }

    protected function getHttpKernel($dispatcher)
    {
        $controllerResolver = $this->createMock(ControllerResolverInterface::class);
        $controllerResolver->expects($this->once())->method('getController')->willReturn(fn () => new Response());
        $argumentResolver = $this->createMock(ArgumentResolverInterface::class);
        $argumentResolver->expects($this->once())->method('getArguments')->willReturn([]);

        return new HttpKernel($dispatcher$controllerResolvernew RequestStack()$argumentResolver);
    }
}
$event = $options['event'] ?? null;
        $dispatcherServiceName = $options['dispatcher_service_name'] ?? null;

        $title = 'Registered listeners';

        if (null !== $dispatcherServiceName) {
            $title .= sprintf(' of event dispatcher "%s"', $dispatcherServiceName);
        }

        if (null !== $event) {
            $title .= sprintf(' for event `%s` ordered by descending priority', $event);
            $registeredListeners = $eventDispatcher->getListeners($event);
        } else {
            // Try to see if "events" exists             $registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events']array_map(fn ($event) => $eventDispatcher->getListeners($event)$options['events'])) : $eventDispatcher->getListeners();
        }

        $this->write(sprintf('# %s', $title)."\n");

        if (null !== $event) {
            foreach ($registeredListeners as $order => $listener) {
                $this->write("\n".sprintf('## Listener %d', $order + 1)."\n");
                $this->describeCallable($listener);
                
public function notify($event$eventArgs = null)
    {
        if (!$this->hasListeners($event)) {
            return null;
        }

        $eventArgs = $this->buildEventArgs($eventArgs);
        $eventArgs->setReturn(null);
        $eventArgs->setName($event);
        $eventArgs->setProcessed(false);

        foreach ($this->getListeners($event) as $listener) {
            $listener->execute($eventArgs);
        }
        $eventArgs->setProcessed(true);

        return $eventArgs;
    }

    /** * Checks if the event has registered listeners. * If the event has listeners this listeners will be executed with the given event arguments. * The event arguments have to been an array or an instance of the Enlight_Event_EventArgs class. * If the given arguments not an array or an instance of the Enlight_Event_EventArgs class enlight * throw an Enlight_Event_Exception. * Before the listener will be executed the the flag "processed" will be set to false in the event arguments. * After all event listeners has been executed the "processed" flag will be set to true. * * The event listeners will be executed until one of the listeners return not null. * * @param string $event * @param Enlight_Event_EventArgs|array|null $eventArgs * * @throws Enlight_Exception * * @return Enlight_Event_EventArgs|null */
public function removeSubscriber(EventSubscriberInterface $subscriber): void
    {
        $this->dispatcher->removeSubscriber($subscriber);
    }

    /** * @return array<array-key, array<array-key, callable(object): void>|callable(object): void> */
    public function getListeners(?string $eventName = null): array
    {
        return $this->dispatcher->getListeners($eventName);
    }

    public function getListenerPriority(string $eventName, callable $listener): ?int
    {
        /** @var callable(object): void $listener - Specify generic callback interface callers can provide more specific implementations */
        return $this->dispatcher->getListenerPriority($eventName$listener);
    }

    public function hasListeners(?string $eventName = null): bool
    {
        return $this->dispatcher->hasListeners($eventName);
    }
$this->dispatcher = $this->createEventDispatcher();
        $this->listener = new TestEventListener();
    }

    protected function createEventDispatcher()
    {
        return new EventDispatcher();
    }

    public function testInitialState()
    {
        $this->assertEquals([]$this->dispatcher->getListeners());
        $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
        $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
    }

    public function testAddListener()
    {
        $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
        $this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
        $this->assertTrue($this->dispatcher->hasListeners());
        $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
        $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
        
$event = $options['event'] ?? null;
        $dispatcherServiceName = $options['dispatcher_service_name'] ?? null;

        $title = 'Registered Listeners';

        if (null !== $dispatcherServiceName) {
            $title .= sprintf(' of Event Dispatcher "%s"', $dispatcherServiceName);
        }

        if (null !== $event) {
            $title .= sprintf(' for "%s" Event', $event);
            $registeredListeners = $eventDispatcher->getListeners($event);
        } else {
            $title .= ' Grouped by Event';
            // Try to see if "events" exists             $registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events']array_map(fn ($event) => $eventDispatcher->getListeners($event)$options['events'])) : $eventDispatcher->getListeners();
        }

        $options['output']->title($title);
        if (null !== $event) {
            $this->renderEventListenerTable($eventDispatcher$event$registeredListeners$options['output']);
        } else {
            ksort($registeredListeners);
            
/** * @return never */
    public function removeSubscriber(EventSubscriberInterface $subscriber)
    {
        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);
    }
}
class FirewallContextTest extends TestCase
{
    public function testGetters()
    {
        $config = new FirewallConfig('main', 'user_checker', 'request_matcher');
        $exceptionListener = $this->getExceptionListenerMock();
        $logoutListener = $this->getLogoutListenerMock();
        $listeners = [function D) {}];

        $context = new FirewallContext($listeners$exceptionListener$logoutListener$config);

        $this->assertEquals($listeners$context->getListeners());
        $this->assertEquals($exceptionListener$context->getExceptionListener());
        $this->assertEquals($logoutListener$context->getLogoutListener());
        $this->assertEquals($config$context->getConfig());
    }

    private function getExceptionListenerMock()
    {
        return $this
            ->getMockBuilder(ExceptionListener::class)
            ->disableOriginalConstructor()
            ->getMock();
    }
$map->add($matchingMatcher[$theListener]$theException);

        $tooLateMatcher = $this->createMock(RequestMatcherInterface::class);
        $tooLateMatcher
            ->expects($this->never())
            ->method('matches')
        ;

        $map->add($tooLateMatcher[function D) {}]);

        [$listeners$exception] = $map->getListeners($request);

        $this->assertEquals([$theListener]$listeners);
        $this->assertEquals($theException$exception);
    }

    public function testGetListenersWithAnEntryHavingNoRequestMatcher()
    {
        $map = new FirewallMap();

        $request = new Request();

        
$this->optimized = [];
        }
    }

    public function dispatch(object $event, string $eventName = null): object
    {
        $eventName ??= $event::class;

        if (isset($this->optimized)) {
            $listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName));
        } else {
            $listeners = $this->getListeners($eventName);
        }

        if ($listeners) {
            $this->callListeners($listeners$eventName$event);
        }

        return $event;
    }

    public function getListeners(string $eventName = null): array
    {
        


  /** * {@inheritdoc} */
  public function getListeners($event_name = NULL): array {
    $result = [];

    if ($event_name === NULL) {
      // If event name was omitted, collect all listeners of all events.       foreach (array_keys($this->listeners) as $event_name) {
        $listeners = $this->getListeners($event_name);
        if (!empty($listeners)) {
          $result[$event_name] = $listeners;
        }
      }
    }
    elseif (isset($this->listeners[$event_name])) {
      // Sort listeners if necessary.       if (isset($this->unsorted[$event_name])) {
        krsort($this->listeners[$event_name]);
        unset($this->unsorted[$event_name]);
      }

      
/** * @return never */
    public function removeSubscriber(EventSubscriberInterface $subscriber)
    {
        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);
    }
}
Home | Imprint | This part of the site doesn't use cookies.