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
|
|
|
private $app; |
13
|
|
|
private $filesystem; |
14
|
|
|
private $service; |
15
|
|
|
private $pool; |
16
|
|
|
|
17
|
7 |
|
public function __construct(Application $app, Service $service, Pool $pool, Filesystem $filesystem) |
18
|
|
|
{ |
19
|
7 |
|
$this->app = $app; |
20
|
7 |
|
$this->filesystem = $filesystem; |
21
|
7 |
|
$this->service = $service; |
22
|
7 |
|
$this->pool = $pool; |
23
|
7 |
|
} |
24
|
|
|
|
25
|
6 |
|
public function getSubscription() |
26
|
|
|
{ |
27
|
6 |
|
$subscription = []; |
28
|
6 |
|
foreach ($this->filesystem->listClasses('Listener') as $class) { |
29
|
6 |
|
$reflection = new ReflectionClass($class); |
30
|
6 |
|
if ($reflection->isAbstract()) { |
31
|
|
|
continue; |
32
|
|
|
} |
33
|
6 |
|
foreach ($reflection->getStaticPropertyValue('events') as $event) { |
34
|
6 |
|
if (!array_key_exists($event, $subscription)) { |
35
|
6 |
|
$subscription[$event] = []; |
36
|
|
|
} |
37
|
6 |
|
$subscription[$event][] = substr($class, strlen('Listener\\')); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
return $subscription; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function fire(string $event, $context) |
45
|
|
|
{ |
46
|
1 |
|
$this->app->dispatch('event.fire', [ |
47
|
1 |
|
'event' => $this->service->getName().'.'.$event, |
48
|
1 |
|
'context' => $context, |
49
|
|
|
]); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
public function fireChangesPart(string $producer, int $fraction = 10) |
53
|
|
|
{ |
54
|
|
|
if (++$this->counter % $fraction === 0) { |
|
|
|
|
55
|
|
|
return $this->fireChanges($producer); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function fireChanges(string $producer) |
60
|
|
|
{ |
61
|
|
|
$this->pool->get($this->service->getName()); |
62
|
|
|
|
63
|
|
|
$dispatcher = $this->app->get(Dispatcher::class); |
64
|
|
|
|
65
|
|
|
foreach ($this->pool->getMappers() as $mapper) { |
66
|
|
|
$spy = $mapper->getPlugin(Spy::class); |
67
|
|
|
if ($spy->hasChanges()) { |
68
|
|
|
// reduce changes list |
69
|
|
|
$changes = $spy->getChanges(); |
70
|
|
|
foreach ($changes as $action => $collection) { |
71
|
|
|
foreach ($collection as $space => $entities) { |
72
|
|
|
$event = $this->service->getName().'.'.$space.'.'.$action; |
73
|
|
|
|
74
|
|
|
if (!$this->service->eventExists($event)) { |
75
|
|
|
unset($collection[$space]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
if (!count($collection)) { |
79
|
|
|
unset($changes->$action); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (count(get_object_vars($changes))) { |
84
|
|
|
$dispatcher->send('event.changes', [ |
85
|
|
|
'changes' => $changes, |
86
|
|
|
'producer' => $producer, |
87
|
|
|
'service' => $mapper->serviceName, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$spy->reset(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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: