[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/zendframework/zend-eventmanager/src/ -> LazyListenerAggregate.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zend-eventmanager for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   https://github.com/zendframework/zend-eventmanager/blob/master/LICENSE.md
   8   */
   9  
  10  namespace Zend\EventManager;
  11  
  12  use Interop\Container\ContainerInterface;
  13  
  14  /**
  15   * Aggregate listener for attaching lazy listeners.
  16   *
  17   * Lazy listeners are listeners where creation is deferred until they are
  18   * triggered; this removes the most costly mechanism of pulling a listener
  19   * from a container unless the listener is actually invoked.
  20   *
  21   * Usage is:
  22   *
  23   * <code>
  24   * $events->attachAggregate(new LazyListenerAggregate(
  25   *     $lazyEventListenersOrDefinitions,
  26   *     $container
  27   * ));
  28   * </code>
  29   */
  30  class LazyListenerAggregate implements ListenerAggregateInterface
  31  {
  32      use ListenerAggregateTrait;
  33  
  34      /**
  35       * @var ContainerInterface Container from which to pull lazy listeners.
  36       */
  37      private $container;
  38  
  39      /**
  40       * @var array Additional environment/option variables to use when creating listener.
  41       */
  42      private $env;
  43  
  44      /**
  45       * Generated LazyEventListener instances.
  46       *
  47       * @var LazyEventListener[]
  48       */
  49      private $lazyListeners = [];
  50  
  51      /**
  52       * Constructor
  53       *
  54       * Accepts the composed $listeners, as well as the $container and $env in
  55       * order to create a listener aggregate that defers listener creation until
  56       * the listener is triggered.
  57       *
  58       * Listeners may be either LazyEventListener instances, or lazy event
  59       * listener definitions that can be provided to a LazyEventListener
  60       * constructor in order to create a new instance; in the latter case, the
  61       * $container and $env will be passed at instantiation as well.
  62       *
  63       * @var array $listeners LazyEventListener instances or array definitions
  64       *     to pass to the LazyEventListener constructor.
  65       * @var ContainerInterface $container
  66       * @var array $env
  67       * @throws Exception\InvalidArgumentException for invalid listener items.
  68       */
  69      public function __construct(array $listeners, ContainerInterface $container, array $env = [])
  70      {
  71          $this->container = $container;
  72          $this->env       = $env;
  73  
  74          // This would raise an exception for invalid structs
  75          foreach ($listeners as $listener) {
  76              if (is_array($listener)) {
  77                  $listener = new LazyEventListener($listener, $container, $env);
  78              }
  79  
  80              if (! $listener instanceof LazyEventListener) {
  81                  throw new Exception\InvalidArgumentException(sprintf(
  82                      'All listeners must be LazyEventListener instances or definitions; received %s',
  83                      (is_object($listener) ? get_class($listener) : gettype($listener))
  84                  ));
  85              }
  86  
  87              $this->lazyListeners[] = $listener;
  88          }
  89      }
  90  
  91      /**
  92       * Attach the aggregate to the event manager.
  93       *
  94       * Loops through all composed lazy listeners, and attaches them to the
  95       * event manager.
  96       *
  97       * @var EventManagerInterface $events
  98       * @var int $priority
  99       */
 100      public function attach(EventManagerInterface $events, $priority = 1)
 101      {
 102          foreach ($this->lazyListeners as $lazyListener) {
 103              $this->listeners[] = $events->attach(
 104                  $lazyListener->getEvent(),
 105                  $lazyListener,
 106                  $lazyListener->getPriority($priority)
 107              );
 108          }
 109      }
 110  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1