[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/zendframework/zend-eventmanager/src/ -> LazyListener.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   * Lazy listener instance.
  16   *
  17   * Used to allow lazy creation of listeners via a dependency injection
  18   * container.
  19   *
  20   * Lazy listener definitions have the following members:
  21   *
  22   * - listener: the service name of the listener to use.
  23   * - method: the method name of the listener to invoke for the specified event.
  24   *
  25   * If desired, you can pass $env at instantiation; this will be passed to the
  26   * container's `build()` method, if it has one, when creating the listener
  27   * instance.
  28   *
  29   * Pass instances directly to the event manager's `attach()` method as the
  30   * listener argument.
  31   */
  32  class LazyListener
  33  {
  34      /**
  35       * @var ContainerInterface Container from which to pull listener.
  36       */
  37      private $container;
  38  
  39      /**
  40       * @var array Variables/options to use during service creation, if any.
  41       */
  42      private $env;
  43  
  44      /**
  45       * @var callable Marshaled listener callback.
  46       */
  47      private $listener;
  48  
  49      /**
  50       * @var string Method name to invoke on listener.
  51       */
  52      private $method;
  53  
  54      /**
  55       * @var string Service name of listener.
  56       */
  57      private $service;
  58  
  59      /**
  60       * @param array $definition
  61       * @param ContainerInterface $container
  62       * @param array $env
  63       */
  64      public function __construct(array $definition, ContainerInterface $container, array $env = [])
  65      {
  66          if ((! isset($definition['listener'])
  67              || ! is_string($definition['listener'])
  68              || empty($definition['listener']))
  69          ) {
  70              throw new Exception\InvalidArgumentException(
  71                  'Lazy listener definition is missing a valid "listener" member; cannot create LazyListener'
  72              );
  73          }
  74  
  75          if ((! isset($definition['method'])
  76              || ! is_string($definition['method'])
  77              || empty($definition['method']))
  78          ) {
  79              throw new Exception\InvalidArgumentException(
  80                  'Lazy listener definition is missing a valid "method" member; cannot create LazyListener'
  81              );
  82          }
  83  
  84          $this->service   = $definition['listener'];
  85          $this->method    = $definition['method'];
  86          $this->container = $container;
  87          $this->env       = $env;
  88      }
  89  
  90      /**
  91       * Use the listener as an invokable, allowing direct attachment to an event manager.
  92       *
  93       * @param EventInterface $event
  94       * @return callable
  95       */
  96      public function __invoke(EventInterface $event)
  97      {
  98          $listener = $this->fetchListener();
  99          $method   = $this->method;
 100          return $listener->{$method}($event);
 101      }
 102  
 103      /**
 104       * @return callable
 105       */
 106      private function fetchListener()
 107      {
 108          if ($this->listener) {
 109              return $this->listener;
 110          }
 111  
 112          // In the future, typehint against Zend\ServiceManager\ServiceLocatorInterface,
 113          // which defines this message starting in v3.
 114          if (method_exists($this->container, 'build') && ! empty($this->env)) {
 115              $this->listener = $this->container->build($this->service, $this->env);
 116              return $this->listener;
 117          }
 118  
 119          $this->listener = $this->container->get($this->service);
 120          return $this->listener;
 121      }
 122  }


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