1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Job\Module; |
4
|
|
|
|
5
|
|
|
use Basis\Application; |
6
|
|
|
use Basis\Context; |
7
|
|
|
use Basis\Dispatcher; |
8
|
|
|
use Basis\Event; |
9
|
|
|
use Basis\Job; |
10
|
|
|
use Basis\Service; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
class Handle extends Job |
14
|
|
|
{ |
15
|
|
|
public $event; |
16
|
|
|
public $eventId; |
17
|
|
|
public $context; |
18
|
|
|
|
19
|
|
|
public $sync = false; |
20
|
|
|
|
21
|
|
|
public function run(Application $app, Dispatcher $dispatcher, Event $event, Service $service) |
22
|
|
|
{ |
23
|
|
|
$start = microtime(1); |
24
|
|
|
$subscription = $event->getSubscription(); |
25
|
|
|
|
26
|
|
|
$patterns = []; |
27
|
|
|
foreach (array_keys($subscription) as $pattern) { |
28
|
|
|
if ($service->eventMatch($this->event, $pattern)) { |
29
|
|
|
$patterns[] = $pattern; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!count($patterns)) { |
34
|
|
|
$existingSubscription = $this->find('event.subscription', [ |
35
|
|
|
'service' => $service->getName(), |
36
|
|
|
]); |
37
|
|
|
foreach ($existingSubscription as $candidate) { |
38
|
|
|
$nick = $candidate->getType()->nick; |
39
|
|
|
if (!array_key_exists($nick, $subscription)) { |
40
|
|
|
$this->dispatch('event.unsubscribe', [ |
41
|
|
|
'event' => $nick, |
42
|
|
|
'service' => $service->getName(), |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
$dispatcher->send('event.feedback', [ |
47
|
|
|
'eventId' => $this->eventId, |
48
|
|
|
'service' => $service->getName(), |
49
|
|
|
'result' => [ |
50
|
|
|
'message' => 'no subscription' |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
return [ |
54
|
|
|
'msg' => 'no subscription', |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->get(Context::class)->event = $this->eventId; |
59
|
|
|
|
60
|
|
|
$parts = explode('.', $this->event); |
61
|
|
|
$action = array_pop($parts); |
62
|
|
|
$space = implode('.', $parts); |
63
|
|
|
|
64
|
|
|
$listeners = []; |
65
|
|
|
foreach ($patterns as $pattern) { |
66
|
|
|
foreach ($subscription[$pattern] as $listener) { |
67
|
|
|
if (!array_key_exists($listener, $listeners)) { |
68
|
|
|
$listeners[$listener] = $app->get('Listener\\'.$listener); |
69
|
|
|
$listeners[$listener]->event = $this->event; |
70
|
|
|
$listeners[$listener]->eventId = $this->eventId; |
71
|
|
|
$listeners[$listener]->context = $this->context; |
72
|
|
|
$listeners[$listener]->space = $space; |
73
|
|
|
$listeners[$listener]->action = $action; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$data = []; |
80
|
|
|
$issues = []; |
81
|
|
|
foreach ($listeners as $nick => $listener) { |
82
|
|
|
try { |
83
|
|
|
$data[$nick] = $app->call([$listener, 'run']); |
84
|
|
|
$event->fireChanges($nick); |
85
|
|
|
} catch (Exception $e) { |
86
|
|
|
$issues[$nick] = [ |
87
|
|
|
'message' => $e->getMessage(), |
88
|
|
|
'trace' => explode(PHP_EOL, $e->getTraceAsString()), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result = [ |
94
|
|
|
'data' => $data, |
95
|
|
|
'issues' => $issues, |
96
|
|
|
'time' => microtime(1) - $start, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
if ($this->sync) { |
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$dispatcher->send('event.feedback', [ |
104
|
|
|
'eventId' => $this->eventId, |
105
|
|
|
'service' => $service->getName(), |
106
|
|
|
'result' => $result |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|