| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Hoa
|
|
|
5 |
*
|
|
|
6 |
*
|
|
|
7 |
* @license
|
|
|
8 |
*
|
|
|
9 |
* New BSD License
|
|
|
10 |
*
|
|
|
11 |
* Copyright © 2007-2017, Hoa community. All rights reserved.
|
|
|
12 |
*
|
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
|
14 |
* modification, are permitted provided that the following conditions are met:
|
|
|
15 |
* * Redistributions of source code must retain the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer.
|
|
|
17 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
18 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
19 |
* documentation and/or other materials provided with the distribution.
|
|
|
20 |
* * Neither the name of the Hoa nor the names of its contributors may be
|
|
|
21 |
* used to endorse or promote products derived from this software without
|
|
|
22 |
* specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
25 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
|
|
|
28 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
29 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
30 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
31 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
32 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
33 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
34 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
35 |
*/
|
|
|
36 |
|
|
|
37 |
namespace Psy\Readline\Hoa;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Events are asynchronous at registration, anonymous at use (until we
|
|
|
41 |
* receive a bucket) and useful to largely spread data through components
|
|
|
42 |
* without any known connection between them.
|
|
|
43 |
*/
|
|
|
44 |
class Event
|
|
|
45 |
{
|
|
|
46 |
/**
|
|
|
47 |
* Event ID key.
|
|
|
48 |
*/
|
|
|
49 |
const KEY_EVENT = 0;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Source object key.
|
|
|
53 |
*/
|
|
|
54 |
const KEY_SOURCE = 1;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Static register of all observable objects, i.e. `Hoa\Event\Source`
|
|
|
58 |
* object, i.e. object that can send event.
|
|
|
59 |
*/
|
|
|
60 |
private static $_register = [];
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Collection of callables, i.e. observer objects.
|
|
|
64 |
*/
|
|
|
65 |
protected $_callable = [];
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Privatize the constructor.
|
|
|
69 |
*/
|
|
|
70 |
private function __construct()
|
|
|
71 |
{
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Manage multiton of events, with the principle of asynchronous
|
|
|
77 |
* attachments.
|
|
|
78 |
*/
|
|
|
79 |
public static function getEvent(string $eventId): self
|
|
|
80 |
{
|
|
|
81 |
if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
|
|
|
82 |
self::$_register[$eventId] = [
|
|
|
83 |
self::KEY_EVENT => new self(),
|
|
|
84 |
self::KEY_SOURCE => null,
|
|
|
85 |
];
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return self::$_register[$eventId][self::KEY_EVENT];
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Declares a new object in the observable collection.
|
|
|
93 |
* Note: Hoa's libraries use `hoa://Event/anID` for their observable objects.
|
|
|
94 |
*/
|
|
|
95 |
public static function register(string $eventId, /* Source|string */ $source)
|
|
|
96 |
{
|
|
|
97 |
if (true === self::eventExists($eventId)) {
|
|
|
98 |
throw new EventException('Cannot redeclare an event with the same ID, i.e. the event '.'ID %s already exists.', 0, $eventId);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if (\is_object($source) && !($source instanceof EventSource)) {
|
|
|
102 |
throw new EventException('The source must implement \Hoa\Event\Source '.'interface; given %s.', 1, \get_class($source));
|
|
|
103 |
} else {
|
|
|
104 |
$reflection = new \ReflectionClass($source);
|
|
|
105 |
|
|
|
106 |
if (false === $reflection->implementsInterface('\Psy\Readline\Hoa\EventSource')) {
|
|
|
107 |
throw new EventException('The source must implement \Hoa\Event\Source '.'interface; given %s.', 2, $source);
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if (!isset(self::$_register[$eventId][self::KEY_EVENT])) {
|
|
|
112 |
self::$_register[$eventId][self::KEY_EVENT] = new self();
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
self::$_register[$eventId][self::KEY_SOURCE] = $source;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Undeclares an object in the observable collection.
|
|
|
120 |
*
|
|
|
121 |
* If `$hard` is set to `true, then the source and its attached callables
|
|
|
122 |
* will be deleted.
|
|
|
123 |
*/
|
|
|
124 |
public static function unregister(string $eventId, bool $hard = false)
|
|
|
125 |
{
|
|
|
126 |
if (false !== $hard) {
|
|
|
127 |
unset(self::$_register[$eventId]);
|
|
|
128 |
} else {
|
|
|
129 |
self::$_register[$eventId][self::KEY_SOURCE] = null;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Attach an object to an event.
|
|
|
135 |
*
|
|
|
136 |
* It can be a callable or an accepted callable form (please, see the
|
|
|
137 |
* `Hoa\Consistency\Xcallable` class).
|
|
|
138 |
*/
|
|
|
139 |
public function attach($callable): self
|
|
|
140 |
{
|
|
|
141 |
$callable = Xcallable::from($callable);
|
|
|
142 |
$this->_callable[$callable->getHash()] = $callable;
|
|
|
143 |
|
|
|
144 |
return $this;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Detaches an object to an event.
|
|
|
149 |
*
|
|
|
150 |
* Please see `self::attach` method.
|
|
|
151 |
*/
|
|
|
152 |
public function detach($callable): self
|
|
|
153 |
{
|
|
|
154 |
unset($this->_callable[Xcallable::from($callable)->getHash()]);
|
|
|
155 |
|
|
|
156 |
return $this;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Checks if at least one callable is attached to an event.
|
|
|
161 |
*/
|
|
|
162 |
public function isListened(): bool
|
|
|
163 |
{
|
|
|
164 |
return !empty($this->_callable);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Notifies, i.e. send data to observers.
|
|
|
169 |
*/
|
|
|
170 |
public static function notify(string $eventId, EventSource $source, EventBucket $data)
|
|
|
171 |
{
|
|
|
172 |
if (false === self::eventExists($eventId)) {
|
|
|
173 |
throw new EventException('Event ID %s does not exist, cannot send notification.', 3, $eventId);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
$data->setSource($source);
|
|
|
177 |
$event = self::getEvent($eventId);
|
|
|
178 |
|
|
|
179 |
foreach ($event->_callable as $callable) {
|
|
|
180 |
$callable($data);
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Checks whether an event exists.
|
|
|
186 |
*/
|
|
|
187 |
public static function eventExists(string $eventId): bool
|
|
|
188 |
{
|
|
|
189 |
return
|
|
|
190 |
\array_key_exists($eventId, self::$_register) &&
|
|
|
191 |
self::$_register[$eventId][self::KEY_SOURCE] !== null;
|
|
|
192 |
}
|
|
|
193 |
}
|