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