|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\EventDispatcher\ListenerProviders\Discover; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Utility\Oop; |
|
6
|
|
|
use Nip\Utility\Str; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
|
|
|
|
|
9
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
|
|
|
|
|
|
10
|
|
|
use Roave\BetterReflection\Reflector\DefaultReflector; |
|
|
|
|
|
|
11
|
|
|
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; |
|
|
|
|
|
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DiscoverEvents |
|
17
|
|
|
* @package ByTIC\EventDispatcher\ListenerProviders\Discover |
|
18
|
|
|
* |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
class DiscoverEvents |
|
22
|
|
|
{ |
|
23
|
2 |
|
/** |
|
24
|
|
|
* Get all of the events and listeners by searching the given listener directory. |
|
25
|
2 |
|
* |
|
26
|
2 |
|
* @param array $listenerPaths |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function within($listenerPaths): array |
|
30
|
|
|
{ |
|
31
|
|
|
return static::getListenerEvents( |
|
32
|
|
|
static::getListenerClasses($listenerPaths) |
|
33
|
|
|
); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
/** |
|
37
|
2 |
|
* @param array $paths |
|
38
|
2 |
|
* @return array |
|
39
|
2 |
|
*/ |
|
40
|
2 |
|
protected static function getListenerClasses($paths): iterable |
|
41
|
|
|
{ |
|
42
|
|
|
$paths = is_array($paths) ? $paths : [$paths]; |
|
|
|
|
|
|
43
|
|
|
$files = (new Finder)->files()->in($paths); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($files as $file) { |
|
46
|
|
|
yield from static::classFromFile($file); |
|
|
|
|
|
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
// $astLocator = (new BetterReflection())->astLocator(); |
|
50
|
2 |
|
// $directoriesSourceLocator = new DirectoriesSourceLocator($paths, $astLocator); |
|
51
|
|
|
// if (class_exists(\Roave\BetterReflection\Reflector\ClassReflector::class)) { |
|
52
|
2 |
|
// $reflector = new \Roave\BetterReflection\Reflector\ClassReflector($directoriesSourceLocator); |
|
53
|
2 |
|
// return $reflector->getAllClasses(); |
|
54
|
2 |
|
// } |
|
55
|
|
|
// |
|
56
|
|
|
// $reflector = new DefaultReflector($directoriesSourceLocator); |
|
57
|
2 |
|
// return $reflector->reflectAllClasses(); |
|
58
|
2 |
|
} |
|
59
|
2 |
|
|
|
60
|
|
|
/** |
|
61
|
2 |
|
* Extract the class name from the given file path. |
|
62
|
2 |
|
* |
|
63
|
|
|
* @param \SplFileInfo $file |
|
64
|
|
|
* @param string $basePath |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
2 |
|
protected static function classFromFile(SplFileInfo $file) |
|
68
|
|
|
{ |
|
69
|
|
|
return Oop::classesInFile($file); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \Roave\BetterReflection\Reflection\ReflectionClass[] $listeners |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected static function getListenerEvents(iterable $listeners): array |
|
77
|
|
|
{ |
|
78
|
|
|
$listenerEvents = []; |
|
79
|
|
|
foreach ($listeners as $listener) { |
|
80
|
|
|
try { |
|
81
|
|
|
$listener = new ReflectionClass($listener); |
|
82
|
|
|
foreach ($listener->getMethods() as $method) { |
|
83
|
|
|
if (!$method->isPublic()) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
if (!Str::is('handle*', $method->name) || |
|
87
|
|
|
!isset($method->getParameters()[0])) { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
$eventName = $method->getParameters()[0]->getClass()->getName(); |
|
91
|
|
|
$listenerEvents[$eventName][] = [$listener->getName(), $method->getName()]; |
|
92
|
|
|
} |
|
93
|
|
|
} catch (\ReflectionException $e) { |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return array_filter($listenerEvents); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths