~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/src/Event/Emitter.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace GuzzleHttp\Event;
 
4
 
 
5
/**
 
6
 * Guzzle event emitter.
 
7
 *
 
8
 * Some of this class is based on the Symfony EventDispatcher component, which
 
9
 * ships with the following license:
 
10
 *
 
11
 *     This file is part of the Symfony package.
 
12
 *
 
13
 *     (c) Fabien Potencier <fabien@symfony.com>
 
14
 *
 
15
 *     For the full copyright and license information, please view the LICENSE
 
16
 *     file that was distributed with this source code.
 
17
 *
 
18
 * @link https://github.com/symfony/symfony/tree/master/src/Symfony/Component/EventDispatcher
 
19
 */
 
20
class Emitter implements EmitterInterface
 
21
{
 
22
    /** @var array */
 
23
    private $listeners = [];
 
24
 
 
25
    /** @var array */
 
26
    private $sorted = [];
 
27
 
 
28
    public function on($eventName, callable $listener, $priority = 0)
 
29
    {
 
30
        if ($priority === 'first') {
 
31
            $priority = isset($this->listeners[$eventName])
 
32
                ? max(array_keys($this->listeners[$eventName])) + 1
 
33
                : 1;
 
34
        } elseif ($priority === 'last') {
 
35
            $priority = isset($this->listeners[$eventName])
 
36
                ? min(array_keys($this->listeners[$eventName])) - 1
 
37
                : -1;
 
38
        }
 
39
 
 
40
        $this->listeners[$eventName][$priority][] = $listener;
 
41
        unset($this->sorted[$eventName]);
 
42
    }
 
43
 
 
44
    public function once($eventName, callable $listener, $priority = 0)
 
45
    {
 
46
        $onceListener = function (
 
47
            EventInterface $event,
 
48
            $eventName
 
49
        ) use (&$onceListener, $eventName, $listener, $priority) {
 
50
            $this->removeListener($eventName, $onceListener);
 
51
            $listener($event, $eventName, $this);
 
52
        };
 
53
 
 
54
        $this->on($eventName, $onceListener, $priority);
 
55
    }
 
56
 
 
57
    public function removeListener($eventName, callable $listener)
 
58
    {
 
59
        if (!isset($this->listeners[$eventName])) {
 
60
            return;
 
61
        }
 
62
 
 
63
        foreach ($this->listeners[$eventName] as $priority => $listeners) {
 
64
            if (false !== ($key = array_search($listener, $listeners, true))) {
 
65
                unset(
 
66
                    $this->listeners[$eventName][$priority][$key],
 
67
                    $this->sorted[$eventName]
 
68
                );
 
69
            }
 
70
        }
 
71
    }
 
72
 
 
73
    public function listeners($eventName = null)
 
74
    {
 
75
        // Return all events in a sorted priority order
 
76
        if ($eventName === null) {
 
77
            foreach (array_keys($this->listeners) as $eventName) {
 
78
                if (!isset($this->sorted[$eventName])) {
 
79
                    $this->listeners($eventName);
 
80
                }
 
81
            }
 
82
            return $this->sorted;
 
83
        }
 
84
 
 
85
        // Return the listeners for a specific event, sorted in priority order
 
86
        if (!isset($this->sorted[$eventName])) {
 
87
            if (!isset($this->listeners[$eventName])) {
 
88
                return [];
 
89
            } else {
 
90
                krsort($this->listeners[$eventName]);
 
91
                $this->sorted[$eventName] = call_user_func_array(
 
92
                    'array_merge',
 
93
                    $this->listeners[$eventName]
 
94
                );
 
95
            }
 
96
        }
 
97
 
 
98
        return $this->sorted[$eventName];
 
99
    }
 
100
 
 
101
    public function emit($eventName, EventInterface $event)
 
102
    {
 
103
        if (isset($this->listeners[$eventName])) {
 
104
            foreach ($this->listeners($eventName) as $listener) {
 
105
                $listener($event, $eventName);
 
106
                if ($event->isPropagationStopped()) {
 
107
                    break;
 
108
                }
 
109
            }
 
110
        }
 
111
 
 
112
        return $event;
 
113
    }
 
114
 
 
115
    public function attach(SubscriberInterface $subscriber)
 
116
    {
 
117
        foreach ($subscriber->getEvents() as $eventName => $listeners) {
 
118
            if (is_array($listeners[0])) {
 
119
                foreach ($listeners as $listener) {
 
120
                    $this->on(
 
121
                        $eventName,
 
122
                        [$subscriber, $listener[0]],
 
123
                        isset($listener[1]) ? $listener[1] : 0
 
124
                    );
 
125
                }
 
126
            } else {
 
127
                $this->on(
 
128
                    $eventName,
 
129
                    [$subscriber, $listeners[0]],
 
130
                    isset($listeners[1]) ? $listeners[1] : 0
 
131
                );
 
132
            }
 
133
        }
 
134
    }
 
135
 
 
136
    public function detach(SubscriberInterface $subscriber)
 
137
    {
 
138
        foreach ($subscriber->getEvents() as $eventName => $listener) {
 
139
            $this->removeListener($eventName, array($subscriber, $listener[0]));
 
140
        }
 
141
    }
 
142
 
 
143
    public function __call($name, $arguments)
 
144
    {
 
145
        return \GuzzleHttp\deprecation_proxy(
 
146
            $this,
 
147
            $name,
 
148
            $arguments,
 
149
            [
 
150
                'addSubscriber'    => 'attach',
 
151
                'removeSubscriber' => 'detach',
 
152
                'addListener'      => 'on',
 
153
                'dispatch'         => 'emit'
 
154
            ]
 
155
        );
 
156
    }
 
157
}