1 | <?php |
||
2 | /** |
||
3 | * Action observer |
||
4 | * User: moyo |
||
5 | * Date: 25/12/2017 |
||
6 | * Time: 5:56 PM |
||
7 | */ |
||
8 | |||
9 | namespace Carno\Database\SQL\Actions; |
||
10 | |||
11 | use Carno\Database\SQL\Builder; |
||
12 | use Closure; |
||
13 | |||
14 | trait Observer |
||
15 | { |
||
16 | /** |
||
17 | * @var Closure[] |
||
18 | */ |
||
19 | private $actWatchers = []; |
||
20 | |||
21 | /** |
||
22 | * @var Closure[] |
||
23 | */ |
||
24 | private $rsWatchers = []; |
||
25 | |||
26 | /** |
||
27 | * @param int $action |
||
28 | * @param Closure ...$programs |
||
29 | */ |
||
30 | protected function actWatching(int $action, Closure ...$programs) : void |
||
31 | { |
||
32 | $this->actWatchers[$action] = array_merge($this->actWatchers[$action] ?? [], $programs); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param int $action |
||
37 | * @param Builder $instance |
||
38 | */ |
||
39 | protected function actTrigger(int $action, Builder $instance) : void |
||
40 | { |
||
41 | foreach ($this->actWatchers[$action] ?? [] as $watcher) { |
||
42 | $watcher($instance); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param Closure ...$programs |
||
48 | */ |
||
49 | protected function rsWatching(Closure ...$programs) : void |
||
50 | { |
||
51 | $this->rsWatchers = array_merge($this->rsWatchers, $programs); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param array $rows |
||
56 | * @return array |
||
57 | */ |
||
58 | protected function rsTrigger(array $rows) |
||
59 | { |
||
60 | foreach ($this->rsWatchers as $watcher) { |
||
61 | $rows = yield $watcher($rows); |
||
0 ignored issues
–
show
|
|||
62 | } |
||
63 | |||
64 | return $rows; |
||
65 | } |
||
66 | } |
||
67 |