Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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

ListenerAttacherTrait.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 3.27 KiB


01  <?php
02  namespace GuzzleHttp\Event;
03   
04  /**
05   * Trait that provides methods for extract event listeners specified in an array
06   * and attaching them to an emitter owned by the object or one of its direct
07   * dependencies.
08   */
09  trait ListenerAttacherTrait
10  {
11      /**
12       * Attaches event listeners and properly sets their priorities and whether
13       * or not they are are only executed once.
14       *
15       * @param HasEmitterInterface $object    Object that has the event emitter.
16       * @param array               $listeners Array of hashes representing event
17       *                                       event listeners. Each item contains
18       *                                       "name", "fn", "priority", & "once".
19       */
20      private function attachListeners(HasEmitterInterface $object, array $listeners)
21      {
22          $emitter = $object->getEmitter();
23          foreach ($listeners as $el) {
24              if ($el['once']) {
25                  $emitter->once($el['name'], $el['fn'], $el['priority']);
26              } else {
27                  $emitter->on($el['name'], $el['fn'], $el['priority']);
28              }
29          }
30      }
31   
32      /**
33       * Extracts the allowed events from the provided array, and ignores anything
34       * else in the array. The event listener must be specified as a callable or
35       * as an array of event listener data ("name", "fn", "priority", "once").
36       *
37       * @param array $source Array containing callables or hashes of data to be
38       *                      prepared as event listeners.
39       * @param array $events Names of events to look for in the provided $source
40       *                      array. Other keys are ignored.
41       * @return array
42       */
43      private function prepareListeners(array $source, array $events)
44      {
45          $listeners = [];
46          foreach ($events as $name) {
47              if (isset($source[$name])) {
48                  $this->buildListener($name, $source[$name], $listeners);
49              }
50          }
51   
52          return $listeners;
53      }
54   
55      /**
56       * Creates a complete event listener definition from the provided array of
57       * listener data. Also works recursively if more than one listeners are
58       * contained in the provided array.
59       *
60       * @param string         $name      Name of the event the listener is for.
61       * @param array|callable $data      Event listener data to prepare.
62       * @param array          $listeners Array of listeners, passed by reference.
63       *
64       * @throws \InvalidArgumentException if the event data is malformed.
65       */
66      private function buildListener($name, $data, &$listeners)
67      {
68          static $defaults = ['priority' => 0, 'once' => false];
69   
70          // If a callable is provided, normalize it to the array format.
71          if (is_callable($data)) {
72              $data = ['fn' => $data];
73          }
74   
75          // Prepare the listener and add it to the array, recursively.
76          if (isset($data['fn'])) {
77              $data['name'] = $name;
78              $listeners[] = $data + $defaults;
79          } elseif (is_array($data)) {
80              foreach ($data as $listenerData) {
81                  $this->buildListener($name, $listenerData, $listeners);
82              }
83          } else {
84              throw new \InvalidArgumentException('Each event listener must be a '
85                  . 'callable or an associative array containing a "fn" key.');
86          }
87      }
88  }
89