Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
LazyListener.php
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-2015 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;
011
012 use Interop\Container\ContainerInterface;
013
014 /**
015 * Lazy listener instance.
016 *
017 * Used to allow lazy creation of listeners via a dependency injection
018 * container.
019 *
020 * Lazy listener definitions have the following members:
021 *
022 * - listener: the service name of the listener to use.
023 * - method: the method name of the listener to invoke for the specified event.
024 *
025 * If desired, you can pass $env at instantiation; this will be passed to the
026 * container's `build()` method, if it has one, when creating the listener
027 * instance.
028 *
029 * Pass instances directly to the event manager's `attach()` method as the
030 * listener argument.
031 */
032 class LazyListener
033 {
034 /**
035 * @var ContainerInterface Container from which to pull listener.
036 */
037 private $container;
038
039 /**
040 * @var array Variables/options to use during service creation, if any.
041 */
042 private $env;
043
044 /**
045 * @var callable Marshaled listener callback.
046 */
047 private $listener;
048
049 /**
050 * @var string Method name to invoke on listener.
051 */
052 private $method;
053
054 /**
055 * @var string Service name of listener.
056 */
057 private $service;
058
059 /**
060 * @param array $definition
061 * @param ContainerInterface $container
062 * @param array $env
063 */
064 public function __construct(array $definition, ContainerInterface $container, array $env = [])
065 {
066 if ((! isset($definition['listener'])
067 || ! is_string($definition['listener'])
068 || empty($definition['listener']))
069 ) {
070 throw new Exception\InvalidArgumentException(
071 'Lazy listener definition is missing a valid "listener" member; cannot create LazyListener'
072 );
073 }
074
075 if ((! isset($definition['method'])
076 || ! is_string($definition['method'])
077 || empty($definition['method']))
078 ) {
079 throw new Exception\InvalidArgumentException(
080 'Lazy listener definition is missing a valid "method" member; cannot create LazyListener'
081 );
082 }
083
084 $this->service = $definition['listener'];
085 $this->method = $definition['method'];
086 $this->container = $container;
087 $this->env = $env;
088 }
089
090 /**
091 * Use the listener as an invokable, allowing direct attachment to an event manager.
092 *
093 * @param EventInterface $event
094 * @return callable
095 */
096 public function __invoke(EventInterface $event)
097 {
098 $listener = $this->fetchListener();
099 $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 }
123