1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Event |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Event; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Traits\CountableTrait; |
18
|
|
|
use Phossa2\Event\Traits\NameGlobbingTrait; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Event\Traits\SharedManagerTrait; |
21
|
|
|
use Phossa2\Event\Traits\ListenerAwareTrait; |
22
|
|
|
use Phossa2\Event\Interfaces\CountableInterface; |
23
|
|
|
use Phossa2\Event\Interfaces\SharedManagerInterface; |
24
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* EventDispatcher |
28
|
|
|
* |
29
|
|
|
* Advanced event manager with |
30
|
|
|
* |
31
|
|
|
* - event name globbing |
32
|
|
|
* - shared manager support |
33
|
|
|
* - attach/detach listener |
34
|
|
|
* - able to trigger an event with countable times |
35
|
|
|
* |
36
|
|
|
* @package Phossa2\Event |
37
|
|
|
* @author Hong Zhang <[email protected]> |
38
|
|
|
* @version 2.0.0 |
39
|
|
|
* @since 2.0.0 added |
40
|
|
|
*/ |
41
|
|
|
class EventDispatcher extends EventManager implements SharedManagerInterface, ListenerAwareInterface, CountableInterface |
42
|
|
|
{ |
43
|
|
|
use CountableTrait, |
44
|
|
|
NameGlobbingTrait, |
45
|
|
|
SharedManagerTrait, |
46
|
|
|
ListenerAwareTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* event prototype |
50
|
|
|
* |
51
|
|
|
* @var EventInterface|null |
52
|
|
|
* @access protected |
53
|
|
|
*/ |
54
|
|
|
protected $event_proto; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a event manager with defined scopes |
58
|
|
|
* |
59
|
|
|
* @param string|array $scopes |
60
|
|
|
* @param EventInterface $event_proto event prototype if any |
61
|
|
|
* @access public |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
$scopes = '', |
65
|
|
|
EventInterface $event_proto = null |
66
|
|
|
) { |
67
|
|
|
// set scopes |
68
|
|
|
if ('' !== $scopes) { |
69
|
|
|
$this->scopes = (array) $scopes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// set event prototype |
73
|
|
|
$this->event_proto = $event_proto; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Override `newEvent()` in EventManager. |
78
|
|
|
* |
79
|
|
|
* Added event prototype support |
80
|
|
|
* |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
protected function newEvent( |
84
|
|
|
$eventName, |
85
|
|
|
$context, |
86
|
|
|
array $properties |
87
|
|
|
)/*# : EventInterface */ { |
88
|
|
|
if (is_object($eventName)) { |
89
|
|
|
return $eventName; |
90
|
|
|
} elseif ($this->event_proto) { |
91
|
|
|
$evt = clone $this->event_proto; |
92
|
|
|
return $evt($eventName, $context, $properties); |
93
|
|
|
} else { |
94
|
|
|
return new Event($eventName, $context, $properties); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Override `getMatchedQueue()` in EventManager. |
100
|
|
|
* |
101
|
|
|
* Support for shared manager & name globbing |
102
|
|
|
* |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
protected function getMatchedQueue( |
106
|
|
|
/*# : string */ $eventName |
107
|
|
|
)/*# : EventQueueInterface */ { |
108
|
|
|
// get all shared managers |
109
|
|
|
$managers = $this->getShareables(); |
110
|
|
|
|
111
|
|
|
// add $this manager |
112
|
|
|
array_unshift($managers, $this); |
113
|
|
|
|
114
|
|
|
/* @var $mgr EventDispatcher */ |
115
|
|
|
$nqueue = $this->newEventQueue(); |
116
|
|
|
foreach ($managers as $mgr) { |
117
|
|
|
$nqueue = $nqueue->combine($mgr->matchEventQueues($eventName)); |
118
|
|
|
} |
119
|
|
|
return $nqueue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get all event names of $this manager |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
* @access protected |
127
|
|
|
*/ |
128
|
|
|
protected function getEventNames()/*# : array */ |
129
|
|
|
{ |
130
|
|
|
return array_keys($this->events); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get a merged queue in $this manager for the given event name |
135
|
|
|
* |
136
|
|
|
* @param string $eventName |
137
|
|
|
* @return EventQueueInterface |
138
|
|
|
* @access protected |
139
|
|
|
*/ |
140
|
|
|
protected function matchEventQueues( |
141
|
|
|
/*# string */ $eventName |
142
|
|
|
)/*: EventQueueInterface */ { |
143
|
|
|
$nqueue = $this->newEventQueue(); |
144
|
|
|
$names = $this->globEventNames($eventName, $this->getEventNames()); |
145
|
|
|
foreach ($names as $evtName) { |
146
|
|
|
if ($this->hasEvent($evtName)) { |
147
|
|
|
$nqueue = $nqueue->combine($this->events[$evtName]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $nqueue; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|