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\ListenerInterface; |
20
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
21
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implementation of EventCapableInterface |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Event |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see EventCapableInterface |
29
|
|
|
* @version 2.1.5 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
* @since 2.1.1 updated |
32
|
|
|
* @since 2.1.5 added attachSelfToEventManager() |
33
|
|
|
*/ |
34
|
|
|
trait EventCapableTrait |
35
|
|
|
{ |
36
|
|
|
use EventPrototypeTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* event manager or dispatcher |
40
|
|
|
* |
41
|
|
|
* @var EventManagerInterface |
42
|
|
|
* @access protected |
43
|
|
|
*/ |
44
|
|
|
protected $event_manager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* flag for attachListener |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
* @access protected |
51
|
|
|
* @since 2.1.5 |
52
|
|
|
*/ |
53
|
|
|
protected $listener_attached = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
* |
58
|
|
|
* @since 2.1.5 moved attachListener to getEventManager |
59
|
|
|
*/ |
60
|
|
|
public function setEventManager( |
61
|
|
|
EventManagerInterface $eventManager |
62
|
|
|
) { |
63
|
|
|
$this->event_manager = $eventManager; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
* |
70
|
|
|
* @since 2.1.5 added attachSelfToEventManager() |
71
|
|
|
*/ |
72
|
|
|
public function getEventManager()/*# : EventManagerInterface */ |
73
|
|
|
{ |
74
|
|
|
// create the default slave |
75
|
|
|
if (is_null($this->event_manager)) { |
76
|
|
|
// add own classname as scope |
77
|
|
|
$this->setEventManager(new EventDispatcher(get_class($this))); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// attach self to the event manager if not yet |
81
|
|
|
$this->attachSelfToEventManager(); |
82
|
|
|
|
83
|
|
|
return $this->event_manager; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function trigger( |
90
|
|
|
/*# string */ $eventName, |
91
|
|
|
array $parameters = [] |
92
|
|
|
)/*# : bool */ { |
93
|
|
|
$evt = $this->newEvent($eventName, $this, $parameters); |
94
|
|
|
return $this->getEventManager()->trigger($evt); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
public function triggerEvent( |
101
|
|
|
/*# string */ $eventName, |
102
|
|
|
array $parameters = [] |
103
|
|
|
)/*# : EventInterface */ { |
104
|
|
|
$evt = $this->newEvent($eventName, $this, $parameters); |
105
|
|
|
$this->getEventManager()->trigger($evt); |
106
|
|
|
return $evt; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Attach $this to the event manager if not yet |
111
|
|
|
* |
112
|
|
|
* @access protected |
113
|
|
|
* @since 2.1.5 |
114
|
|
|
*/ |
115
|
|
|
protected function attachSelfToEventManager() |
116
|
|
|
{ |
117
|
|
|
if (!$this->listener_attached) { |
118
|
|
|
if ($this->event_manager instanceof ListenerAwareInterface && |
119
|
|
|
$this instanceof ListenerInterface |
120
|
|
|
) { |
121
|
|
|
$this->event_manager->attachListener($this); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
$this->listener_attached = true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: