1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Events; |
12
|
|
|
|
13
|
|
|
use Phalcon\Di; |
14
|
|
|
use Phalcon\Events\Manager; |
15
|
|
|
use Phalcon\Events\ManagerInterface; |
16
|
|
|
use Zemit\Exception; |
17
|
|
|
use Zemit\Utils\Slug; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait EventsAwareTrait |
21
|
|
|
* |
22
|
|
|
* @author Julien Turbide <[email protected]> |
23
|
|
|
* @copyright Zemit Team <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @since 1.0 |
26
|
|
|
* @version 1.0 |
27
|
|
|
* |
28
|
|
|
* @package Zemit\Events |
29
|
|
|
*/ |
30
|
|
|
trait EventsAwareTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Manager |
34
|
|
|
*/ |
35
|
|
|
protected $eventsManager = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set Event Manager Service Provider |
39
|
|
|
* |
40
|
|
|
* @param Manager $eventsManager |
41
|
|
|
*/ |
42
|
4 |
|
public function setEventsManager(ManagerInterface $manager) |
43
|
|
|
{ |
44
|
4 |
|
$this->eventsManager = $manager; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* return event manager |
49
|
|
|
* |
50
|
|
|
* @return Manager|null |
51
|
|
|
*/ |
52
|
4 |
|
public function getEventsManager() : ?ManagerInterface |
53
|
|
|
{ |
54
|
4 |
|
if (!empty($this->eventsManager)) { |
55
|
4 |
|
$manager = $this->eventsManager; |
56
|
|
|
} elseif (Di::getDefault()->has('eventsManager')) { |
57
|
|
|
$manager = Di::getDefault()->get('eventsManager'); |
58
|
|
|
} |
59
|
4 |
|
if (isset($manager) && $manager instanceof Manager) { |
60
|
4 |
|
return $manager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Event slug to use as a component |
68
|
|
|
* my-component:beforeSomeTask |
69
|
|
|
* my-component:afterSomeTask |
70
|
|
|
* |
71
|
|
|
* @var null|string |
72
|
|
|
*/ |
73
|
|
|
public static $_eventsSlug = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the event component slug |
77
|
|
|
* |
78
|
|
|
* @return null|string Component slug |
79
|
|
|
* @throws \Zemit\Exception |
80
|
|
|
*/ |
81
|
4 |
|
public static function getEventsSlug() |
82
|
|
|
{ |
83
|
|
|
return |
84
|
4 |
|
self::$_eventsSlug ?? |
85
|
|
|
self::$_eventsSlug = |
86
|
1 |
|
Slug::generate( |
87
|
1 |
|
basename( |
88
|
4 |
|
str_replace('\\', '/', __CLASS__) |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the event component slug |
95
|
|
|
* |
96
|
|
|
* @param $eventSlug |
97
|
|
|
*/ |
98
|
|
|
public static function setEventSlug($eventSlug) |
99
|
|
|
{ |
100
|
|
|
self::$_eventsSlug = $eventSlug; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Checking if event manager is defined - fire event |
105
|
|
|
* |
106
|
|
|
* @param $task |
107
|
|
|
* @param null $data |
|
|
|
|
108
|
|
|
* @param bool $cancelable |
109
|
|
|
* |
110
|
|
|
* @throws Exception |
111
|
|
|
*/ |
112
|
4 |
|
public function fire($task, $data = null, $cancelable = false) |
113
|
|
|
{ |
114
|
4 |
|
if ($manager = $this->getEventsManager()) { |
115
|
4 |
|
$manager->fire($this->getEventsSlug() . ':' . $task, $this, $data, $cancelable); |
116
|
|
|
} else { |
117
|
|
|
throw new Exception('Events Manager Service Provider \'eventsManager\' does not exists in DI of \'' . get_class($this) . '\''); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add possibility to parse an holder and run a callback after |
123
|
|
|
* |
124
|
|
|
* @param $holder |
125
|
|
|
* @param null $class |
|
|
|
|
126
|
|
|
* @param array $params |
127
|
|
|
* @param null $callback |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return mixed|null |
130
|
|
|
* @throws Exception |
131
|
|
|
*/ |
132
|
4 |
|
public function fireSet(&$holder, $class = null, array $params = [], $callback = null) |
133
|
|
|
{ |
134
|
|
|
// prepare event name |
135
|
4 |
|
$event = basename(str_replace('\\', '//', $class)); |
136
|
|
|
|
137
|
|
|
// fire before event with the holder |
138
|
4 |
|
$this->fire('before' . $event, func_get_args()); |
139
|
|
|
|
140
|
|
|
// holder not set, apply class to it |
141
|
4 |
|
if (!isset($holder)) { |
142
|
|
|
// can be a class path |
143
|
4 |
|
if (class_exists($class)) { |
144
|
4 |
|
$holder = new $class(...$params); |
145
|
|
|
} |
146
|
|
|
// can be a callable |
147
|
|
|
elseif (is_callable($class)) { |
148
|
|
|
$holder = $class(...$params); |
149
|
|
|
} |
150
|
|
|
// can be the object |
151
|
|
|
elseif (is_object($class)) { |
152
|
|
|
$holder = $class; |
153
|
|
|
} |
154
|
|
|
// class not found |
155
|
|
|
elseif (is_string($class)) { |
156
|
|
|
throw new Exception('Class "' . $class . '" not found'); |
157
|
|
|
} |
158
|
|
|
// other error |
159
|
|
|
else { |
160
|
4 |
|
throw new Exception('Unknown type "' . $class . '" for "$class"'); |
161
|
|
|
} |
162
|
4 |
|
} elseif (is_string($holder)) { |
163
|
|
|
// can be a class path |
164
|
|
|
if (class_exists($holder)) { |
165
|
|
|
$holder = new $holder(...$params); |
166
|
|
|
} |
167
|
|
|
// class not founmd |
168
|
|
|
else { |
169
|
|
|
throw new Exception('Class "' . $class . '" not found'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// run the callback if isset |
174
|
4 |
|
if (isset($callback) && is_callable($callback)) { |
175
|
4 |
|
$callback($this); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// fire after event |
179
|
4 |
|
$this->fire('after' . $event, func_get_args()); |
180
|
|
|
|
181
|
|
|
// return the holder |
182
|
4 |
|
return $holder; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.