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
|
<?php namespace FluentMail\App\Services\DB;
use FluentMail\App\Services\DB\QueryBuilder\QueryBuilderHandler; use FluentMail\App\Services\DB\QueryBuilder\Raw;
class EventHandler { /** * @var array */ protected $events = array();
/** * @var array */ protected $firedEvents = array();
/** * @return array */ public function getEvents() { return $this->events; }
/** * @param $event * @param $table * * @return callable|null */ public function getEvent($event, $table = ':any') { if ($table instanceof Raw) { return null; } return isset($this->events[$table][$event]) ? $this->events[$table][$event] : null; }
/** * @param $event * @param string $table * @param callable $action * * @return void */ public function registerEvent($event, $table, \Closure $action) { $table = $table ?: ':any';
$this->events[$table][$event] = $action; }
/** * @param $event * @param string $table * * @return void */ public function removeEvent($event, $table = ':any') { unset($this->events[$table][$event]); }
/** * @param \FluentMail\App\Services\DB\src\QueryBuilder\QueryBuilderHandler $queryBuilder * @param $event * @return mixed */ public function fireEvents($queryBuilder, $event) { $originalArgs = func_get_args(); $statements = $queryBuilder->getStatements(); $tables = isset($statements['tables']) ? $statements['tables'] : array();
// Events added with :any will be fired in case of any table, // we are adding :any as a fake table at the beginning. array_unshift($tables, ':any');
// Fire all events foreach ($tables as $table) { // Fire before events for :any table if ($action = $this->getEvent($event, $table)) { // Make an event id, with event type and table $eventId = $event . $table;
// Fire event $handlerParams = $originalArgs; unset($handlerParams[1]); // we do not need $event // Add to fired list $this->firedEvents[] = $eventId; $result = call_user_func_array($action, $handlerParams); if (!is_null($result)) { return $result; }; } } } }
|