Projekt

Obecné

Profil

Stáhnout (7.01 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
namespace Symfony\Component\EventDispatcher;
13

    
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15

    
16
/**
17
 * Lazily loads listeners and subscribers from the dependency injection
18
 * container.
19
 *
20
 * @author Fabien Potencier <fabien@symfony.com>
21
 * @author Bernhard Schussek <bschussek@gmail.com>
22
 * @author Jordan Alliot <jordan.alliot@gmail.com>
23
 *
24
 * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead.
25
 */
26
class ContainerAwareEventDispatcher extends EventDispatcher
27
{
28
    private $container;
29

    
30
    /**
31
     * The service IDs of the event listeners and subscribers.
32
     */
33
    private $listenerIds = array();
34

    
35
    /**
36
     * The services registered as listeners.
37
     */
38
    private $listeners = array();
39

    
40
    public function __construct(ContainerInterface $container)
41
    {
42
        $this->container = $container;
43

    
44
        $class = get_class($this);
45
        if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
46
            $class = get_parent_class($class);
47
        }
48
        if (__CLASS__ !== $class) {
49
            @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
50
        }
51
    }
52

    
53
    /**
54
     * Adds a service as event listener.
55
     *
56
     * @param string $eventName Event for which the listener is added
57
     * @param array  $callback  The service ID of the listener service & the method
58
     *                          name that has to be called
59
     * @param int    $priority  The higher this value, the earlier an event listener
60
     *                          will be triggered in the chain.
61
     *                          Defaults to 0.
62
     *
63
     * @throws \InvalidArgumentException
64
     */
65
    public function addListenerService($eventName, $callback, $priority = 0)
66
    {
67
        @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
68

    
69
        if (!is_array($callback) || 2 !== count($callback)) {
70
            throw new \InvalidArgumentException('Expected an array("service", "method") argument');
71
        }
72

    
73
        $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
74
    }
75

    
76
    public function removeListener($eventName, $listener)
77
    {
78
        $this->lazyLoad($eventName);
79

    
80
        if (isset($this->listenerIds[$eventName])) {
81
            foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) {
82
                $key = $serviceId.'.'.$method;
83
                if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
84
                    unset($this->listeners[$eventName][$key]);
85
                    if (empty($this->listeners[$eventName])) {
86
                        unset($this->listeners[$eventName]);
87
                    }
88
                    unset($this->listenerIds[$eventName][$i]);
89
                    if (empty($this->listenerIds[$eventName])) {
90
                        unset($this->listenerIds[$eventName]);
91
                    }
92
                }
93
            }
94
        }
95

    
96
        parent::removeListener($eventName, $listener);
97
    }
98

    
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function hasListeners($eventName = null)
103
    {
104
        if (null === $eventName) {
105
            return $this->listenerIds || $this->listeners || parent::hasListeners();
106
        }
107

    
108
        if (isset($this->listenerIds[$eventName])) {
109
            return true;
110
        }
111

    
112
        return parent::hasListeners($eventName);
113
    }
114

    
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getListeners($eventName = null)
119
    {
120
        if (null === $eventName) {
121
            foreach ($this->listenerIds as $serviceEventName => $args) {
122
                $this->lazyLoad($serviceEventName);
123
            }
124
        } else {
125
            $this->lazyLoad($eventName);
126
        }
127

    
128
        return parent::getListeners($eventName);
129
    }
130

    
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getListenerPriority($eventName, $listener)
135
    {
136
        $this->lazyLoad($eventName);
137

    
138
        return parent::getListenerPriority($eventName, $listener);
139
    }
140

    
141
    /**
142
     * Adds a service as event subscriber.
143
     *
144
     * @param string $serviceId The service ID of the subscriber service
145
     * @param string $class     The service's class name (which must implement EventSubscriberInterface)
146
     */
147
    public function addSubscriberService($serviceId, $class)
148
    {
149
        @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
150

    
151
        foreach ($class::getSubscribedEvents() as $eventName => $params) {
152
            if (is_string($params)) {
153
                $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
154
            } elseif (is_string($params[0])) {
155
                $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
156
            } else {
157
                foreach ($params as $listener) {
158
                    $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
159
                }
160
            }
161
        }
162
    }
163

    
164
    public function getContainer()
165
    {
166
        @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 as its class will be removed in 4.0. Inject the container or the services you need in your listeners/subscribers instead.', E_USER_DEPRECATED);
167

    
168
        return $this->container;
169
    }
170

    
171
    /**
172
     * Lazily loads listeners for this event from the dependency injection
173
     * container.
174
     *
175
     * @param string $eventName The name of the event to dispatch. The name of
176
     *                          the event is the name of the method that is
177
     *                          invoked on listeners.
178
     */
179
    protected function lazyLoad($eventName)
180
    {
181
        if (isset($this->listenerIds[$eventName])) {
182
            foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
183
                $listener = $this->container->get($serviceId);
184

    
185
                $key = $serviceId.'.'.$method;
186
                if (!isset($this->listeners[$eventName][$key])) {
187
                    $this->addListener($eventName, array($listener, $method), $priority);
188
                } elseif ($this->listeners[$eventName][$key] !== $listener) {
189
                    parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
190
                    $this->addListener($eventName, array($listener, $method), $priority);
191
                }
192

    
193
                $this->listeners[$eventName][$key] = $listener;
194
            }
195
        }
196
    }
197
}
(3-3/13)