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

SharedEventManager.php

Zuletzt modifiziert: 09.10.2024, 12:55 - Dateigröße: 5.60 KiB


 001  <?php
 002  /**
 003   * Zend Framework (http://framework.zend.com/)
 004   *
 005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
 006   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 007   * @license   http://framework.zend.com/license/new-bsd New BSD License
 008   */
 009   
 010  namespace Zend\EventManager;
 011   
 012  use Zend\Stdlib\CallbackHandler;
 013  use Zend\Stdlib\PriorityQueue;
 014   
 015  /**
 016   * Shared/contextual EventManager
 017   *
 018   * Allows attaching to EMs composed by other classes without having an instance first.
 019   * The assumption is that the SharedEventManager will be injected into EventManager
 020   * instances, and then queried for additional listeners when triggering an event.
 021   */
 022  class SharedEventManager implements
 023      SharedEventAggregateAwareInterface,
 024      SharedEventManagerInterface
 025  {
 026      /**
 027       * Identifiers with event connections
 028       * @var array
 029       */
 030      protected $identifiers = array();
 031   
 032      /**
 033       * Attach a listener to an event
 034       *
 035       * Allows attaching a callback to an event offered by one or more
 036       * identifying components. As an example, the following connects to the
 037       * "getAll" event of both an AbstractResource and EntityResource:
 038       *
 039       * <code>
 040       * $sharedEventManager = new SharedEventManager();
 041       * $sharedEventManager->attach(
 042       *     array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
 043       *     'getAll',
 044       *     function ($e) use ($cache) {
 045       *         if (!$id = $e->getParam('id', false)) {
 046       *             return;
 047       *         }
 048       *         if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
 049       *             return;
 050       *         }
 051       *         return $data;
 052       *     }
 053       * );
 054       * </code>
 055       *
 056       * @param  string|array $id Identifier(s) for event emitting component(s)
 057       * @param  string $event
 058       * @param  callable $callback PHP Callback
 059       * @param  int $priority Priority at which listener should execute
 060       * @return CallbackHandler|array Either CallbackHandler or array of CallbackHandlers
 061       */
 062      public function attach($id, $event, $callback, $priority = 1)
 063      {
 064          $ids = (array) $id;
 065          $listeners = array();
 066          foreach ($ids as $id) {
 067              if (!array_key_exists($id, $this->identifiers)) {
 068                  $this->identifiers[$id] = new EventManager($id);
 069              }
 070              $listeners[] = $this->identifiers[$id]->attach($event, $callback, $priority);
 071          }
 072          if (count($listeners) > 1) {
 073              return $listeners;
 074          }
 075          return $listeners[0];
 076      }
 077   
 078      /**
 079       * Attach a listener aggregate
 080       *
 081       * Listener aggregates accept an EventManagerInterface instance, and call attachShared()
 082       * one or more times, typically to attach to multiple events using local
 083       * methods.
 084       *
 085       * @param  SharedListenerAggregateInterface $aggregate
 086       * @param  int $priority If provided, a suggested priority for the aggregate to use
 087       * @return mixed return value of {@link ListenerAggregateInterface::attachShared()}
 088       */
 089      public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1)
 090      {
 091          return $aggregate->attachShared($this, $priority);
 092      }
 093   
 094      /**
 095       * Detach a listener from an event offered by a given resource
 096       *
 097       * @param  string|int $id
 098       * @param  CallbackHandler $listener
 099       * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
 100       */
 101      public function detach($id, CallbackHandler $listener)
 102      {
 103          if (!array_key_exists($id, $this->identifiers)) {
 104              return false;
 105          }
 106          return $this->identifiers[$id]->detach($listener);
 107      }
 108   
 109      /**
 110       * Detach a listener aggregate
 111       *
 112       * Listener aggregates accept a SharedEventManagerInterface instance, and call detachShared()
 113       * of all previously attached listeners.
 114       *
 115       * @param  SharedListenerAggregateInterface $aggregate
 116       * @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
 117       */
 118      public function detachAggregate(SharedListenerAggregateInterface $aggregate)
 119      {
 120          return $aggregate->detachShared($this);
 121      }
 122   
 123      /**
 124       * Retrieve all registered events for a given resource
 125       *
 126       * @param  string|int $id
 127       * @return array
 128       */
 129      public function getEvents($id)
 130      {
 131          if (!array_key_exists($id, $this->identifiers)) {
 132              //Check if there are any id wildcards listeners
 133              if ('*' != $id && array_key_exists('*', $this->identifiers)) {
 134                  return $this->identifiers['*']->getEvents();
 135              }
 136              return false;
 137          }
 138          return $this->identifiers[$id]->getEvents();
 139      }
 140   
 141      /**
 142       * Retrieve all listeners for a given identifier and event
 143       *
 144       * @param  string|int $id
 145       * @param  string|int $event
 146       * @return false|PriorityQueue
 147       */
 148      public function getListeners($id, $event)
 149      {
 150          if (!array_key_exists($id, $this->identifiers)) {
 151              return false;
 152          }
 153          return $this->identifiers[$id]->getListeners($event);
 154      }
 155   
 156      /**
 157       * Clear all listeners for a given identifier, optionally for a specific event
 158       *
 159       * @param  string|int $id
 160       * @param  null|string $event
 161       * @return bool
 162       */
 163      public function clearListeners($id, $event = null)
 164      {
 165          if (!array_key_exists($id, $this->identifiers)) {
 166              return false;
 167          }
 168   
 169          if (null === $event) {
 170              unset($this->identifiers[$id]);
 171              return true;
 172          }
 173   
 174          return $this->identifiers[$id]->clearListeners($event);
 175      }
 176  }
 177