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\Di; |
15
|
|
|
use Phalcon\Events\ManagerInterface; |
16
|
|
|
use Zemit\Support\Helper; |
17
|
|
|
use Zemit\Support\Slug; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The EventsAwareTrait provides methods for managing events within a class. |
21
|
|
|
*/ |
22
|
|
|
trait EventsAwareTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Event prefix to use as a component |
26
|
|
|
* my-component:beforeSomeTask |
27
|
|
|
* my-component:afterSomeTask |
28
|
|
|
*/ |
29
|
|
|
public static ?string $eventsPrefix; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The event manager responsible for handling and triggering events. |
33
|
|
|
*/ |
34
|
|
|
protected ?ManagerInterface $eventsManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the events manager |
38
|
|
|
*/ |
39
|
112 |
|
public function setEventsManager(ManagerInterface $manager): void |
40
|
|
|
{ |
41
|
112 |
|
$this->eventsManager = $manager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the events manager. |
46
|
|
|
*/ |
47
|
4 |
|
public function getEventsManager(): ?ManagerInterface |
48
|
|
|
{ |
49
|
4 |
|
$this->eventsManager ??= Di::getDefault()->get('eventsManager'); |
50
|
4 |
|
return $this->eventsManager; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the event component prefix |
56
|
|
|
* |
57
|
|
|
* @return string|null The event component prefix, or null if not set |
58
|
|
|
*/ |
59
|
4 |
|
public static function getEventsPrefix(): ?string |
60
|
|
|
{ |
61
|
4 |
|
self::$eventsPrefix ??= Helper::slugify( |
62
|
4 |
|
Helper::uncamelize( |
63
|
4 |
|
basename( |
64
|
4 |
|
str_replace( |
65
|
4 |
|
'\\', |
66
|
4 |
|
'/', |
67
|
4 |
|
__CLASS__ |
68
|
4 |
|
) |
69
|
4 |
|
) |
70
|
4 |
|
) |
71
|
4 |
|
); |
72
|
4 |
|
return self::$eventsPrefix; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the events prefix. |
77
|
|
|
* |
78
|
|
|
* @param string|null $eventsPrefix The prefix to be used for events. Pass null to remove the prefix. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
1 |
|
public static function setEventsPrefix(?string $eventsPrefix): void |
83
|
|
|
{ |
84
|
1 |
|
self::$eventsPrefix = $eventsPrefix; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Fire an event. |
89
|
|
|
* |
90
|
|
|
* @param string $task The task to execute. |
91
|
|
|
* @param mixed|null $data The optional data to pass to the event. |
92
|
|
|
* @param bool $cancelable Whether the event is cancelable or not. Defaults to false. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
2 |
|
public function fire(string $task, mixed $data = null, bool $cancelable = false): mixed |
97
|
|
|
{ |
98
|
2 |
|
$eventType = $this->getEventsPrefix() . ':' . $task; |
99
|
2 |
|
$eventsManager = $this->getEventsManager(); |
100
|
|
|
|
101
|
2 |
|
if (!$eventsManager instanceof ManagerInterface) { |
102
|
|
|
throw new \InvalidArgumentException("Events manager must be an instance of '" . ManagerInterface::class . "'."); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $eventsManager->fire($eventType, $this, $data, $cancelable); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|