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\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Event; |
18
|
|
|
use Phossa2\Event\EventDispatcher; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Event\Interfaces\ListenerInterface; |
21
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
22
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Implementation of EventCapableInterface |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Event |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see EventCapableInterface |
30
|
|
|
* @version 2.0.0 |
31
|
|
|
* @since 2.0.0 added |
32
|
|
|
*/ |
33
|
|
|
trait EventCapableTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* event manager or dispatcher |
37
|
|
|
* |
38
|
|
|
* @var EventManagerInterface |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $event_manager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* event prototype |
45
|
|
|
* |
46
|
|
|
* @var EventInterface |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $event_proto; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function setEventManager( |
55
|
|
|
EventManagerInterface $eventManager |
56
|
|
|
) { |
57
|
|
|
$this->event_manager = $eventManager; |
58
|
|
|
|
59
|
|
|
// attach events from $this |
60
|
|
|
if ($eventManager instanceof ListenerAwareInterface && |
61
|
|
|
$this instanceof ListenerInterface |
62
|
|
|
) { |
63
|
|
|
$eventManager->attachListener($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function getEventManager()/*# : EventManagerInterface */ |
73
|
|
|
{ |
74
|
|
|
if (is_null($this->event_manager)) { |
75
|
|
|
$this->setEventManager(new EventDispatcher(get_class($this))); |
76
|
|
|
} |
77
|
|
|
return $this->event_manager; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
public function setEventPrototype(EventInterface $eventPrototype) |
84
|
|
|
{ |
85
|
|
|
$this->event_proto = $eventPrototype; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
public function trigger( |
93
|
|
|
/*# string */ $eventName, |
94
|
|
|
array $properties = [] |
95
|
|
|
)/*# : bool */ { |
96
|
|
|
// trigger the event |
97
|
|
|
$evt = $this->triggerEvent($eventName, $properties); |
98
|
|
|
|
99
|
|
|
// success or stopped |
100
|
|
|
return !$evt->isPropagationStopped(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function triggerEvent( |
107
|
|
|
/*# string */ $eventName, |
108
|
|
|
array $properties = [] |
109
|
|
|
)/*# : EventInterface */ { |
110
|
|
|
// create an event with context $this |
111
|
|
|
$evt = $this->createEvent($eventName, $properties); |
112
|
|
|
|
113
|
|
|
// process it |
114
|
|
|
$this->getEventManager()->trigger($evt); |
115
|
|
|
|
116
|
|
|
// return the event |
117
|
|
|
return $evt; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Create an event with $eventName and properties |
122
|
|
|
* |
123
|
|
|
* @param string $eventName |
124
|
|
|
* @param array $properties |
125
|
|
|
* @return EventInterface |
126
|
|
|
* @access protected |
127
|
|
|
*/ |
128
|
|
|
protected function createEvent( |
129
|
|
|
/*# string */ $eventName, |
130
|
|
|
array $properties |
131
|
|
|
)/*# : EventInterface */ { |
132
|
|
|
// get event prototype |
133
|
|
|
if (is_null($this->event_proto)) { |
134
|
|
|
return new Event($eventName, $this, $properties); |
135
|
|
|
} else { |
136
|
|
|
// clone the prototype |
137
|
|
|
$evt = clone $this->event_proto; |
138
|
|
|
|
139
|
|
|
// init the event and return it |
140
|
|
|
return $evt($eventName, $this, $properties); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|