Repository navigation
Expand file tree
/
Copy pathHasEvents.php
More file actions
168 lines (152 loc) · 4.96 KB
/
Copy pathHasEvents.php
File metadata and controls
168 lines (152 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Concerns;
use Hyperf\Database\Model\Events\Booted;
use Hyperf\Database\Model\Events\Booting;
use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Events\Creating;
use Hyperf\Database\Model\Events\Deleted;
use Hyperf\Database\Model\Events\Deleting;
use Hyperf\Database\Model\Events\ForceDeleted;
use Hyperf\Database\Model\Events\ForceDeleting;
use Hyperf\Database\Model\Events\Restored;
use Hyperf\Database\Model\Events\Restoring;
use Hyperf\Database\Model\Events\Retrieved;
use Hyperf\Database\Model\Events\Saved;
use Hyperf\Database\Model\Events\Saving;
use Hyperf\Database\Model\Events\Updated;
use Hyperf\Database\Model\Events\Updating;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
/**
* @method retrieved(Retrieved $event)
* @method creating(Creating $event)
* @method created(Created $event)
* @method updating(Updating $event)
* @method updated(Updated $event)
* @method saving(Saving $event)
* @method saved(Saved $event)
* @method restoring(Restoring $event)
* @method restored(Restored $event)
* @method deleting(Deleting $event)
* @method deleted(Deleted $event)
* @method forceDeleted(ForceDeleted $event)
* @method forceDeleting(ForceDeleting $event)
*/
trait HasEvents
{
/**
* User exposed events.
*/
protected array $events = [];
/**
* Set the user-defined event names.
*/
public function setEvents(array $events): self
{
foreach ($events as $key => $value) {
if (is_numeric($key) && is_string($value)) {
$events[$value] = '';
unset($events[$key]);
}
}
$this->events = $events;
return $this;
}
/**
* Add some observable event.
*
* @param array|string $events
*/
public function addEvents($events): void
{
$this->events = array_unique(array_merge($this->events, is_array($events) ? $events : func_get_args()));
}
/**
* Remove some registered event.
*/
public function removeEvents(array $events): void
{
foreach ($events as $value) {
if (isset($this->events[$value])) {
// When passing the key of event.
unset($this->events[$value]);
} elseif (class_exists($value) && $key = array_search($value, $this->events)) {
// When passing the class of event.
unset($this->events[$key]);
}
}
}
/**
* Get the available event names, the custom event will override the default event.
*
* @return array [EventMethodName => EventClass]
*/
public function getAvailableEvents(): array
{
return array_replace($this->getDefaultEvents(), $this->events);
}
/**
* Get the default events of Hyperf Database Model.
*/
protected function getDefaultEvents(): array
{
return [
'booting' => Booting::class,
'booted' => Booted::class,
'retrieved' => Retrieved::class,
'creating' => Creating::class,
'created' => Created::class,
'updating' => Updating::class,
'updated' => Updated::class,
'saving' => Saving::class,
'saved' => Saved::class,
'restoring' => Restoring::class,
'restored' => Restored::class,
'deleting' => Deleting::class,
'deleted' => Deleted::class,
'forceDeleted' => ForceDeleted::class,
'forceDeleting' => ForceDeleting::class,
];
}
/**
* Fire the given event for the model.
*
* @return null|object|StoppableEventInterface
*/
protected function fireModelEvent(string $event): ?object
{
$dispatcher = $this->getEventDispatcher();
if (! $dispatcher instanceof EventDispatcherInterface) {
return null;
}
$result = $this->fireCustomModelEvent($event);
// If custom event does not exist, the fireCustomModelEvent() method will return null.
if (! is_null($result)) {
return $result;
}
// If the model is not running in Hyperf, then the listener method of model will not bind to the EventDispatcher automatically.
$eventName = $this->getDefaultEvents()[$event];
return $dispatcher->dispatch(new $eventName($this, $event));
}
/**
* Fire a custom model event for the given event.
*
* @return null|object|StoppableEventInterface
*/
protected function fireCustomModelEvent(string $event)
{
if (! isset($this->events[$event])) {
return;
}
return $this->getEventDispatcher()->dispatch(new $this->events[$event]($this));
}
}