1 | <?php |
||
10 | class Event |
||
11 | { |
||
12 | public function __process(Application $app, BasisEvent $event, Service $service) |
||
16 | |||
17 | 1 | public function index(Application $app, BasisEvent $event, Service $service) |
|
18 | { |
||
19 | 1 | $start = microtime(1); |
|
20 | |||
21 | try { |
||
22 | 1 | $info = $this->getEventInfo(); |
|
23 | 1 | $subscription = $event->getSubscription(); |
|
24 | |||
25 | 1 | $patterns = []; |
|
26 | 1 | foreach (array_keys($subscription) as $pattern) { |
|
27 | 1 | if ($service->eventMatch($info->event, $pattern)) { |
|
28 | 1 | $patterns[] = $pattern; |
|
29 | } |
||
30 | } |
||
31 | |||
32 | 1 | if (!count($patterns)) { |
|
33 | $service->unsubscribe($info->event); |
||
34 | throw new Exception("No subscription on event ".$info->event); |
||
35 | } |
||
36 | |||
37 | 1 | $listeners = []; |
|
38 | 1 | foreach ($patterns as $pattern) { |
|
39 | 1 | foreach ($subscription[$pattern] as $listener) { |
|
40 | 1 | if (!array_key_exists($listener, $listeners)) { |
|
41 | 1 | $listeners[$listener] = $app->get('Listener\\'.$listener); |
|
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | 1 | $result = []; |
|
47 | 1 | $issues = []; |
|
48 | 1 | foreach ($listeners as $nick => $listener) { |
|
49 | 1 | $result[$nick] = $this->handleEvent($app, $listener, $info); |
|
50 | try { |
||
51 | 1 | $event->fireChanges($nick); |
|
52 | 1 | } catch (Exception $e) { |
|
53 | 1 | $issues[$nick] = $e->getMessage(); |
|
54 | } |
||
55 | } |
||
56 | |||
57 | |||
58 | return [ |
||
59 | 1 | 'success' => true, |
|
60 | 1 | 'data' => $result, |
|
61 | 1 | 'issues' => $issues, |
|
62 | 1 | 'time' => microtime(1) - $start, |
|
63 | ]; |
||
64 | |||
65 | } catch (Exception $e) { |
||
66 | return ['success' => false, 'message' => $e->getMessage()]; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | 1 | private function getEventInfo() |
|
96 | |||
97 | 1 | private function handleEvent($app, $instance, $info) |
|
108 | } |
||
109 |