1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Events; |
13
|
|
|
|
14
|
|
|
use Phalcon\Di; |
15
|
|
|
use Phalcon\Events\Manager; |
16
|
|
|
use Phalcon\Events\ManagerInterface; |
17
|
|
|
use Zemit\Exception; |
18
|
|
|
use Zemit\Utils\Slug; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait EventsAwareTrait |
22
|
|
|
* |
23
|
|
|
* |
24
|
|
|
* |
25
|
|
|
* @package Zemit\Events |
26
|
|
|
*/ |
27
|
|
|
trait EventsAwareTrait |
28
|
|
|
{ |
29
|
|
|
protected ?ManagerInterface $eventsManager; |
30
|
|
|
|
31
|
|
|
public function setEventsManager(ManagerInterface $manager): void |
32
|
|
|
{ |
33
|
|
|
$this->eventsManager = $manager; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getEventsManager(): ?ManagerInterface |
37
|
|
|
{ |
38
|
|
|
$this->eventsManager ??= Di::getDefault()->get('eventsManager'); |
39
|
|
|
return $this->eventsManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
/** |
43
|
|
|
* Event prefix to use as a component |
44
|
7 |
|
* my-component:beforeSomeTask |
45
|
|
|
* my-component:afterSomeTask |
46
|
|
|
*/ |
47
|
|
|
public static ?string $eventsPrefix; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the event component prefix |
51
|
|
|
*/ |
52
|
7 |
|
public static function getEventsPrefix(): ?string |
53
|
|
|
{ |
54
|
7 |
|
self::$eventsPrefix ??= Slug::generate(basename(str_replace('\\', '/', __CLASS__))); |
55
|
7 |
|
return self::$eventsPrefix; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
7 |
|
* Set the event component prefix |
60
|
7 |
|
*/ |
61
|
|
|
public static function setEventsPrefix(?string $eventsPrefix): void |
62
|
|
|
{ |
63
|
|
|
self::$eventsPrefix = $eventsPrefix; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checking if event manager is defined - fire event |
68
|
|
|
* |
69
|
|
|
* @param mixed $task |
70
|
|
|
* @param mixed $data |
71
|
|
|
* @param bool $cancelable |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function fire($task, $data = null, bool $cancelable = false) |
76
|
|
|
{ |
77
|
|
|
$eventType = $this->getEventsPrefix() . ':' . $task; |
78
|
|
|
return $this->getEventsManager()->fire($eventType, $this, $data, $cancelable); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
/** |
82
|
|
|
* Fire "before" event |
83
|
7 |
|
* Run class with parameters |
84
|
7 |
|
* Fire "after" event |
85
|
7 |
|
* Return the holder |
86
|
7 |
|
* |
87
|
7 |
|
* @param $holder |
88
|
7 |
|
* @param null $class |
|
|
|
|
89
|
7 |
|
* @param array $params |
90
|
7 |
|
* @param null $callback |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return mixed|null |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
public function fireSet(&$holder, $class = null, array $params = [], $callback = null) |
96
|
|
|
{ |
97
|
|
|
// prepare event name |
98
|
|
|
$event = basename(str_replace('\\', '//', $class)); |
99
|
|
|
|
100
|
|
|
// fire before event with the holder |
101
|
|
|
$this->fire('before' . $event, func_get_args()); |
102
|
|
|
|
103
|
|
|
// holder not set, apply class to it |
104
|
|
|
if (!isset($holder)) { |
105
|
|
|
|
106
|
|
|
// can be a class path |
107
|
|
|
if (class_exists($class)) { |
108
|
|
|
$holder = new $class(...$params); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// can be a callable |
112
|
7 |
|
elseif (is_callable($class)) { |
113
|
|
|
$holder = $class(...$params); |
114
|
7 |
|
} |
115
|
7 |
|
|
116
|
|
|
// can be the object |
117
|
|
|
elseif (is_object($class)) { |
118
|
|
|
$holder = $class; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// class not found |
122
|
|
|
elseif (is_string($class)) { |
123
|
|
|
throw new \Exception('Class "' . $class . '" not found'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// other error |
127
|
|
|
else { |
128
|
|
|
throw new \Exception('Unknown type "' . $class . '" for "$class"'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
7 |
|
elseif (is_string($holder)) { |
133
|
|
|
|
134
|
|
|
// can be a class path |
135
|
7 |
|
if (class_exists($holder)) { |
136
|
|
|
$holder = new $holder(...$params); |
137
|
|
|
} |
138
|
7 |
|
|
139
|
|
|
// class not founmd |
140
|
|
|
else { |
141
|
7 |
|
throw new \Exception('Class "' . $class . '" not found'); |
142
|
|
|
} |
143
|
7 |
|
} |
144
|
7 |
|
|
145
|
|
|
// run the callback if isset |
146
|
|
|
if (isset($callback) && is_callable($callback)) { |
147
|
|
|
$callback($this); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// fire after event |
151
|
|
|
$this->fire('after' . $event, func_get_args()); |
152
|
|
|
|
153
|
|
|
// return the holder |
154
|
|
|
return $holder; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|