1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pixie; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Pixie\QueryBuilder\QueryBuilderHandler; |
7
|
|
|
use Pixie\QueryBuilder\Raw; |
8
|
|
|
|
9
|
|
|
class EventHandler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array<string, array<string, Closure>> |
13
|
|
|
*/ |
14
|
|
|
protected $events = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
protected $firedEvents = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array<string, array<string, Closure>> |
23
|
|
|
*/ |
24
|
|
|
public function getEvents() |
25
|
|
|
{ |
26
|
|
|
return $this->events; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $event |
31
|
|
|
* @param string|Raw $table |
32
|
|
|
* |
33
|
|
|
* @return Closure|null |
34
|
|
|
*/ |
35
|
|
|
public function getEvent(string $event, $table = ':any'): ?Closure |
36
|
|
|
{ |
37
|
|
|
if ($table instanceof Raw) { |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->events[$table][$event] ?? null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $event |
46
|
|
|
* @param string|null $table |
47
|
|
|
* @param Closure $action |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function registerEvent(string $event, ?string $table, Closure $action) |
52
|
|
|
{ |
53
|
|
|
$table = $table ?? ':any'; |
54
|
|
|
|
55
|
|
|
$this->events[$table][$event] = $action; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $event |
60
|
|
|
* @param string $table |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function removeEvent($event, $table = ':any') |
65
|
|
|
{ |
66
|
|
|
unset($this->events[$table][$event]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param QueryBuilderHandler $queryBuilder |
71
|
|
|
* @param string $event |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function fireEvents(QueryBuilderHandler $queryBuilder, string $event) |
76
|
|
|
{ |
77
|
|
|
$statements = $queryBuilder->getStatements(); |
78
|
|
|
$tables = $statements['tables'] ?? []; |
79
|
|
|
|
80
|
|
|
// Events added with :any will be fired in case of any table, |
81
|
|
|
// we are adding :any as a fake table at the beginning. |
82
|
|
|
array_unshift($tables, ':any'); |
83
|
|
|
|
84
|
|
|
// Fire all events |
85
|
|
|
foreach ($tables as $table) { |
86
|
|
|
// Fire before events for :any table |
87
|
|
|
if ($action = $this->getEvent($event, $table)) { |
88
|
|
|
// Make an event id, with event type and table |
89
|
|
|
$eventId = $event . $table; |
90
|
|
|
|
91
|
|
|
// Fire event |
92
|
|
|
$handlerParams = func_get_args(); |
93
|
|
|
unset($handlerParams[1]); // we do not need $event |
94
|
|
|
// Add to fired list |
95
|
|
|
$this->firedEvents[] = $eventId; |
96
|
|
|
$result = call_user_func_array($action, $handlerParams); |
97
|
|
|
if (!is_null($result)) { |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|