1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Controller; |
4
|
|
|
|
5
|
|
|
use Basis\Application; |
6
|
|
|
use Basis\Event as BasisEvent; |
7
|
|
|
use Basis\Service; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
class Event |
11
|
|
|
{ |
12
|
|
|
public function index(Application $app, BasisEvent $event, Service $service) |
13
|
|
|
{ |
14
|
|
|
try { |
15
|
|
|
$info = $this->getEventInfo(); |
16
|
|
|
$subscription = $event->getSubscription(); |
17
|
|
|
|
18
|
|
|
$patterns = []; |
19
|
|
|
foreach (array_keys($subscription) as $pattern) { |
20
|
|
|
if ($service->eventMatch($info->event, $pattern)) { |
21
|
|
|
$patterns[] = $pattern; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (!count($patterns)) { |
26
|
|
|
$service->unsubscribe($info->event); |
27
|
|
|
throw new Exception("No subscription on event ".$info->event); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$listeners = []; |
31
|
|
|
foreach ($patterns as $pattern) { |
32
|
|
|
foreach ($subscription[$pattern] as $listener) { |
33
|
|
|
if (!array_key_exists($listener, $listeners)) { |
34
|
|
|
$listeners[$listener] = $app->get('Listener\\'.$listener); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$result = []; |
40
|
|
|
$issues = []; |
41
|
|
|
foreach ($listeners as $nick => $listener) { |
42
|
|
|
$result[$nick] = $this->handleEvent($app, $listener, $info); |
43
|
|
|
try { |
44
|
|
|
$event->fireChanges($nick); |
45
|
|
|
} catch (Exception $e) { |
46
|
|
|
$issues[$nick] = $e->getMessage(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'success' => true, |
53
|
|
|
'data' => $result, |
54
|
|
|
'issues' => $issues, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
} catch (Exception $e) { |
58
|
|
|
return ['success' => false, 'message' => $e->getMessage()]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getEventInfo() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (!array_key_exists('event', $_REQUEST)) { |
65
|
|
|
throw new Exception('No event defined'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!array_key_exists('context', $_REQUEST)) { |
69
|
|
|
throw new Exception('No context defined'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$context = json_decode($_REQUEST['context']); |
73
|
|
|
|
74
|
|
|
if (!$context) { |
75
|
|
|
throw new Exception('Invalid context'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return [ |
79
|
|
|
'event' => $_REQUEST['event'], |
80
|
|
|
'context' => $context, |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function handleEvent($app, $instance, $info) |
85
|
|
|
{ |
86
|
|
|
foreach ($info as $k => $v) { |
87
|
|
|
$instance->$k = $v; |
88
|
|
|
} |
89
|
|
|
if (!method_exists($instance, 'run')) { |
90
|
|
|
throw new Exception('No run method for '.$class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $app->call([$instance, 'run']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: