Verzeichnisstruktur phpBB-3.3.16


Veröffentlicht
27.04.2026

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

EventListenerIntrospectionTrait.php

Zuletzt modifiziert: 01.05.2026, 11:27 - Dateigröße: 4.99 KiB


 001  <?php
 002  /**
 003   * Zend Framework (http://framework.zend.com/)
 004   *
 005   * @link      http://github.com/zendframework/zend-eventmanager for the canonical source repository
 006   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 007   * @license   https://github.com/zendframework/zend-eventmanager/blob/master/LICENSE.md
 008   */
 009   
 010  namespace Zend\EventManager\Test;
 011   
 012  use PHPUnit\Framework\Assert;
 013  use ReflectionProperty;
 014  use Zend\EventManager\EventManager;
 015   
 016  /**
 017   * Trait providing utility methods and assertions for use in PHPUnit test cases.
 018   *
 019   * This trait may be composed into a test case, and provides:
 020   *
 021   * - methods for introspecting events and listeners
 022   * - methods for asserting listeners are attached at a specific priority
 023   *
 024   * Some functionality in this trait duplicates functionality present in the
 025   * version 2 EventManagerInterface and/or EventManager implementation, but
 026   * abstracts that functionality for use in v3. As such, components or code
 027   * that is testing for listener registration should use the methods in this
 028   * trait to ensure tests are forwards-compatible between zend-eventmanager
 029   * versions.
 030   */
 031  trait EventListenerIntrospectionTrait
 032  {
 033      /**
 034       * Retrieve a list of event names from an event manager.
 035       *
 036       * @param EventManager $events
 037       * @return string[]
 038       */
 039      private function getEventsFromEventManager(EventManager $events)
 040      {
 041          $r = new ReflectionProperty($events, 'events');
 042          $r->setAccessible(true);
 043          $listeners = $r->getValue($events);
 044          return array_keys($listeners);
 045      }
 046   
 047      /**
 048       * Retrieve an interable list of listeners for an event.
 049       *
 050       * Given an event and an event manager, returns an iterator with the
 051       * listeners for that event, in priority order.
 052       *
 053       * If $withPriority is true, the key values will be the priority at which
 054       * the given listener is attached.
 055       *
 056       * Do not pass $withPriority if you want to cast the iterator to an array,
 057       * as many listeners will likely have the same priority, and thus casting
 058       * will collapse to the last added.
 059       *
 060       * @param string $event
 061       * @param EventManager $events
 062       * @param bool $withPriority
 063       * @return \Traversable
 064       */
 065      private function getListenersForEvent($event, EventManager $events, $withPriority = false)
 066      {
 067          $r = new ReflectionProperty($events, 'events');
 068          $r->setAccessible(true);
 069          $internal = $r->getValue($events);
 070   
 071          $listeners = [];
 072          foreach (isset($internal[$event]) ? $internal[$event] : [] as $p => $listOfListeners) {
 073              foreach ($listOfListeners as $l) {
 074                  $listeners[$p] = isset($listeners[$p]) ? array_merge($listeners[$p], $l) : $l;
 075              }
 076          }
 077   
 078          return $this->traverseListeners($listeners, $withPriority);
 079      }
 080   
 081      /**
 082       * Assert that a given listener exists at the specified priority.
 083       *
 084       * @param callable $expectedListener
 085       * @param int $expectedPriority
 086       * @param string $event
 087       * @param EventManager $events
 088       * @param string $message Failure message to use, if any.
 089       */
 090      private function assertListenerAtPriority(
 091          callable $expectedListener,
 092          $expectedPriority,
 093          $event,
 094          EventManager $events,
 095          $message = ''
 096      ) {
 097          $message = $message ?: sprintf(
 098              'Listener not found for event "%s" and priority %d',
 099              $event,
 100              $expectedPriority
 101          );
 102          $listeners = $this->getListenersForEvent($event, $events, true);
 103          $found     = false;
 104          foreach ($listeners as $priority => $listener) {
 105              if ($listener === $expectedListener
 106                  && $priority === $expectedPriority
 107              ) {
 108                  $found = true;
 109                  break;
 110              }
 111          }
 112          Assert::assertTrue($found, $message);
 113      }
 114   
 115      /**
 116       * Returns an indexed array of listeners for an event.
 117       *
 118       * Returns an indexed array of listeners for an event, in priority order.
 119       * Priority values will not be included; use this only for testing if
 120       * specific listeners are present, or for a count of listeners.
 121       *
 122       * @param string $event
 123       * @param EventManager $events
 124       * @return callable[]
 125       */
 126      private function getArrayOfListenersForEvent($event, EventManager $events)
 127      {
 128          return iterator_to_array($this->getListenersForEvent($event, $events));
 129      }
 130   
 131      /**
 132       * Generator for traversing listeners in priority order.
 133       *
 134       * @param array $listeners
 135       * @param bool $withPriority When true, yields priority as key.
 136       */
 137      public function traverseListeners(array $queue, $withPriority = false)
 138      {
 139          krsort($queue, SORT_NUMERIC);
 140   
 141          foreach ($queue as $priority => $listeners) {
 142              $priority = (int) $priority;
 143              foreach ($listeners as $listener) {
 144                  if ($withPriority) {
 145                      yield $priority => $listener;
 146                  } else {
 147                      yield $listener;
 148                  }
 149              }
 150          }
 151      }
 152  }
 153