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\Shared\Globbing\GlobbingTrait; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Event\Traits\SharedManagerTrait; |
21
|
|
|
use Phossa2\Event\Traits\ListenerAwareTrait; |
22
|
|
|
use Phossa2\Event\Traits\EventPrototypeTrait; |
23
|
|
|
use Phossa2\Event\Interfaces\CountableInterface; |
24
|
|
|
use Phossa2\Event\Interfaces\SharedManagerInterface; |
25
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
26
|
|
|
use Phossa2\Event\Interfaces\EventPrototypeInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* EventDispatcher |
30
|
|
|
* |
31
|
|
|
* Advanced event manager with |
32
|
|
|
* |
33
|
|
|
* - event name globbing |
34
|
|
|
* - shared manager support |
35
|
|
|
* - attach/detach listener |
36
|
|
|
* - able to trigger an event with countable times |
37
|
|
|
* |
38
|
|
|
* @package Phossa2\Event |
39
|
|
|
* @author Hong Zhang <[email protected]> |
40
|
|
|
* @version 2.1.0 |
41
|
|
|
* @since 2.0.0 added |
42
|
|
|
* @since 2.1.0 updated |
43
|
|
|
* @since 2.1.1 added EventPrototype |
44
|
|
|
*/ |
45
|
|
|
class EventDispatcher extends EventManager implements SharedManagerInterface, ListenerAwareInterface, CountableInterface, EventPrototypeInterface |
46
|
|
|
{ |
47
|
|
|
use GlobbingTrait, |
48
|
|
|
CountableTrait, |
49
|
|
|
SharedManagerTrait, |
50
|
|
|
ListenerAwareTrait, |
51
|
|
|
EventPrototypeTrait; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a event manager with defined scopes |
55
|
|
|
* |
56
|
|
|
* @param string|string[] $scopes |
57
|
|
|
* @param EventInterface $event_proto event prototype if any |
58
|
|
|
* @access public |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
$scopes = '', |
62
|
|
|
EventInterface $event_proto = null |
63
|
|
|
) { |
64
|
|
|
// set scopes |
65
|
|
|
if ('' !== $scopes) { |
66
|
|
|
$this->scopes = (array) $scopes; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// set event prototype |
70
|
|
|
$this->setEventPrototype($event_proto); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Override `getMatchedQueue()` in EventManager. |
75
|
|
|
* |
76
|
|
|
* Support for shared manager & name globbing |
77
|
|
|
* |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
protected function getMatchedQueue( |
81
|
|
|
/*# : string */ $eventName |
82
|
|
|
)/*# : EventQueueInterface */ { |
83
|
|
|
// get all shared managers |
84
|
|
|
$managers = $this->getShareables(); |
85
|
|
|
|
86
|
|
|
// add $this manager |
87
|
|
|
array_unshift($managers, $this); |
88
|
|
|
|
89
|
|
|
/* @var $mgr EventDispatcher */ |
90
|
|
|
$nqueue = $this->newEventQueue(); |
91
|
|
|
foreach ($managers as $mgr) { |
92
|
|
|
$nqueue = $nqueue->combine($mgr->matchEventQueues($eventName)); |
93
|
|
|
} |
94
|
|
|
return $nqueue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get all event names of $this manager |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
|
|
protected function getEventNames()/*# : array */ |
104
|
|
|
{ |
105
|
|
|
return array_keys($this->events); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get a merged queue in $this manager for the given event name |
110
|
|
|
* |
111
|
|
|
* @param string $eventName |
112
|
|
|
* @return EventQueueInterface |
113
|
|
|
* @access protected |
114
|
|
|
*/ |
115
|
|
|
protected function matchEventQueues( |
116
|
|
|
/*# string */ $eventName |
117
|
|
|
)/*: EventQueueInterface */ { |
118
|
|
|
$nqueue = $this->newEventQueue(); |
119
|
|
|
$names = $this->globbingNames($eventName, $this->getEventNames()); |
120
|
|
|
foreach ($names as $evtName) { |
121
|
|
|
if ($this->hasEvent($evtName)) { |
122
|
|
|
$nqueue = $nqueue->combine($this->events[$evtName]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $nqueue; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|