| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Events;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Reflector;
|
|
|
6 |
use Illuminate\Support\Str;
|
|
|
7 |
use ReflectionClass;
|
|
|
8 |
use ReflectionException;
|
|
|
9 |
use ReflectionMethod;
|
|
|
10 |
use SplFileInfo;
|
|
|
11 |
use Symfony\Component\Finder\Finder;
|
|
|
12 |
|
|
|
13 |
class DiscoverEvents
|
|
|
14 |
{
|
|
|
15 |
/**
|
|
|
16 |
* Get all of the events and listeners by searching the given listener directory.
|
|
|
17 |
*
|
|
|
18 |
* @param string $listenerPath
|
|
|
19 |
* @param string $basePath
|
|
|
20 |
* @return array
|
|
|
21 |
*/
|
|
|
22 |
public static function within($listenerPath, $basePath)
|
|
|
23 |
{
|
|
|
24 |
$listeners = collect(static::getListenerEvents(
|
|
|
25 |
(new Finder)->files()->in($listenerPath), $basePath
|
|
|
26 |
));
|
|
|
27 |
|
|
|
28 |
$discoveredEvents = [];
|
|
|
29 |
|
|
|
30 |
foreach ($listeners as $listener => $events) {
|
|
|
31 |
foreach ($events as $event) {
|
|
|
32 |
if (! isset($discoveredEvents[$event])) {
|
|
|
33 |
$discoveredEvents[$event] = [];
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
$discoveredEvents[$event][] = $listener;
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
return $discoveredEvents;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Get all of the listeners and their corresponding events.
|
|
|
45 |
*
|
|
|
46 |
* @param iterable $listeners
|
|
|
47 |
* @param string $basePath
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
protected static function getListenerEvents($listeners, $basePath)
|
|
|
51 |
{
|
|
|
52 |
$listenerEvents = [];
|
|
|
53 |
|
|
|
54 |
foreach ($listeners as $listener) {
|
|
|
55 |
try {
|
|
|
56 |
$listener = new ReflectionClass(
|
|
|
57 |
static::classFromFile($listener, $basePath)
|
|
|
58 |
);
|
|
|
59 |
} catch (ReflectionException $e) {
|
|
|
60 |
continue;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if (! $listener->isInstantiable()) {
|
|
|
64 |
continue;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
foreach ($listener->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
|
|
|
68 |
if ((! Str::is('handle*', $method->name) && ! Str::is('__invoke', $method->name)) ||
|
|
|
69 |
! isset($method->getParameters()[0])) {
|
|
|
70 |
continue;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$listenerEvents[$listener->name.'@'.$method->name] =
|
|
|
74 |
Reflector::getParameterClassNames($method->getParameters()[0]);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return array_filter($listenerEvents);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Extract the class name from the given file path.
|
|
|
83 |
*
|
|
|
84 |
* @param \SplFileInfo $file
|
|
|
85 |
* @param string $basePath
|
|
|
86 |
* @return string
|
|
|
87 |
*/
|
|
|
88 |
protected static function classFromFile(SplFileInfo $file, $basePath)
|
|
|
89 |
{
|
|
|
90 |
$class = trim(Str::replaceFirst($basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR);
|
|
|
91 |
|
|
|
92 |
return str_replace(
|
|
|
93 |
[DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())).'\\'],
|
|
|
94 |
['\\', app()->getNamespace()],
|
|
|
95 |
ucfirst(Str::replaceLast('.php', '', $class))
|
|
|
96 |
);
|
|
|
97 |
}
|
|
|
98 |
}
|