|
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 triggerEvent( |
|
93
|
|
|
/*# string */ $eventName, |
|
94
|
|
|
array $properties = [] |
|
95
|
|
|
)/*# : EventInterface */ { |
|
96
|
|
|
// create an event with context $this |
|
97
|
|
|
$evt = $this->createEvent($eventName, $properties); |
|
98
|
|
|
|
|
99
|
|
|
// process it |
|
100
|
|
|
$this->getEventManager()->trigger($evt); |
|
101
|
|
|
|
|
102
|
|
|
// return the event |
|
103
|
|
|
return $evt; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Create an event with $eventName and properties |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $eventName |
|
110
|
|
|
* @param array $properties |
|
111
|
|
|
* @return EventInterface |
|
112
|
|
|
* @access protected |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function createEvent( |
|
115
|
|
|
/*# string */ $eventName, |
|
116
|
|
|
array $properties |
|
117
|
|
|
)/*# : EventInterface */ { |
|
118
|
|
|
// get event prototype |
|
119
|
|
|
if (is_null($this->event_proto)) { |
|
120
|
|
|
return new Event($eventName, $this, $properties); |
|
121
|
|
|
} else { |
|
122
|
|
|
// clone the prototype |
|
123
|
|
|
$evt = clone $this->event_proto; |
|
124
|
|
|
|
|
125
|
|
|
// init the event and return it |
|
126
|
|
|
return $evt($eventName, $this, $properties); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|