forked from Leantime/leantime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkEvents.php
54 lines (40 loc) · 1.6 KB
/
checkEvents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
$projectRootDirectory = dirname(__FILE__);
$project_files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($projectRootDirectory)
);
// The path to the 'app' directory.
$app_path = $projectRootDirectory . '/app';
$dispatched_events = [];
$listeners = [];
foreach ($project_files as $file) {
if (!$file->isFile()) {
continue;
}
$relative_path = str_replace([$app_path, '.php'], ['', ''], $file->getRealPath());
$context = 'leantime' . strtolower(str_replace('/', '.', $relative_path));
$source_code = file_get_contents($file->getRealPath());
if (preg_match_all('/dispatch_(event|filter)\(\s*[\'"](.+?)[\'"]\s*[),]/', $source_code, $matches)) {
$events = array_map(function($eventName) use ($context){
return $context . '.' . strtolower($eventName);
}, $matches[2]);
$dispatched_events = array_merge($dispatched_events, $events);
}
if (preg_match_all('/add_(event|filter)_listener\(\s*[\'"](.+?)[\'"]\s*[),]/', $source_code, $matches)) {
$listeners = array_merge($listeners, array_map('strtolower', $matches[2]));
}
}
$valid_listeners = [];
foreach ($listeners as $listener) {
$listener_parts = explode('*', strtolower($listener));
foreach ($dispatched_events as $event) {
if (strpos(strtolower($event), $listener_parts[0]) === 0) {
$valid_listeners[] = $listener;
break;
}
}
}
$invalid_listeners = array_diff($listeners, $valid_listeners);
foreach ($invalid_listeners as $invalid_listener) {
echo "Invalid listener found for event '$invalid_listener'\n";
}