|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Dispatcher; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
|
8
|
|
|
use Tarantool\Mapper\Pool; |
|
9
|
|
|
|
|
10
|
|
|
class Event |
|
11
|
|
|
{ |
|
12
|
|
|
use Toolkit; |
|
13
|
|
|
|
|
14
|
7 |
|
public function getSubscription() |
|
15
|
|
|
{ |
|
16
|
7 |
|
$subscription = []; |
|
17
|
7 |
|
foreach ($this->get(Filesystem::class)->listClasses('Listener') as $class) { |
|
18
|
7 |
|
$reflection = new ReflectionClass($class); |
|
19
|
7 |
|
if ($reflection->isAbstract()) { |
|
20
|
|
|
continue; |
|
21
|
|
|
} |
|
22
|
7 |
|
foreach ($reflection->getStaticPropertyValue('events') as $event) { |
|
23
|
7 |
|
if (!array_key_exists($event, $subscription)) { |
|
24
|
7 |
|
$subscription[$event] = []; |
|
25
|
|
|
} |
|
26
|
7 |
|
$subscription[$event][] = substr($class, strlen('Listener\\')); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
return $subscription; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function fire(string $event, $context) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->app->dispatch('event.fire', [ |
|
36
|
1 |
|
'event' => $this->get(Service::class)->getName().'.'.$event, |
|
37
|
1 |
|
'context' => $context, |
|
38
|
|
|
]); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function fireChangesPart(string $producer, int $fraction = 10) |
|
42
|
|
|
{ |
|
43
|
|
|
if (++$this->counter % $fraction === 0) { |
|
|
|
|
|
|
44
|
|
|
return $this->fireChanges($producer); |
|
45
|
|
|
} |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function hasChanges() |
|
50
|
|
|
{ |
|
51
|
|
|
$hasChanges = false; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->get(Pool::class)->getMappers() as $mapper) { |
|
54
|
|
|
if ($mapper->getPlugin(Spy::class)->hasChanges()) { |
|
55
|
|
|
$hasChanges = true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
return $hasChanges; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function fireChanges(string $producer) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->get(Pool::class)->get($this->get(Service::class)->getName()); |
|
64
|
|
|
|
|
65
|
1 |
|
$dispatcher = $this->app->get(Dispatcher::class); |
|
|
|
|
|
|
66
|
1 |
|
$changed = false; |
|
67
|
|
|
|
|
68
|
1 |
|
foreach ($this->get(Pool::class)->getMappers() as $mapper) { |
|
69
|
1 |
|
$spy = $mapper->getPlugin(Spy::class); |
|
70
|
1 |
|
if ($spy->hasChanges()) { |
|
71
|
|
|
// reduce changes list |
|
72
|
|
|
$changes = $spy->getChanges(); |
|
73
|
|
|
foreach ($changes as $action => $collection) { |
|
74
|
|
|
foreach ($collection as $space => $entities) { |
|
75
|
|
|
$event = $this->get(Service::class)->getName().'.'.$space.'.'.$action; |
|
76
|
|
|
|
|
77
|
|
|
if (!$this->get(Service::class)->eventExists($event)) { |
|
78
|
|
|
unset($collection[$space]); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
if (!count($collection)) { |
|
82
|
|
|
unset($changes->$action); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (count(get_object_vars($changes))) { |
|
87
|
|
|
$changed = true; |
|
88
|
|
|
$data = $this->get(Converter::class)->toArray([ |
|
89
|
|
|
'changes' => $changes, |
|
90
|
|
|
'producer' => $producer, |
|
91
|
|
|
'service' => $mapper->serviceName, |
|
92
|
|
|
'context' => $this->get(Context::class), |
|
93
|
|
|
]); |
|
94
|
|
|
$this->getQueue('event.changes')->put($data); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$spy->reset(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
1 |
|
return $changed; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: