These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\EventProjector; |
||
4 | |||
5 | use SplFileInfo; |
||
6 | use Illuminate\Support\Str; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use Symfony\Component\Finder\Finder; |
||
9 | use Spatie\EventProjector\EventHandlers\EventHandler; |
||
10 | |||
11 | final class DiscoverEventHandlers |
||
12 | { |
||
13 | private $directories = []; |
||
14 | |||
15 | private $basePath; |
||
16 | |||
17 | private $rootNamespace = ''; |
||
18 | |||
19 | private $ignoredFiles = []; |
||
20 | |||
21 | public function __construct() |
||
22 | { |
||
23 | $this->basePath = app_path(); |
||
24 | } |
||
25 | |||
26 | public function within(array $directories): self |
||
27 | { |
||
28 | $this->directories = $directories; |
||
29 | |||
30 | return $this; |
||
31 | } |
||
32 | |||
33 | public function useBasePath(string $basePath): self |
||
34 | { |
||
35 | $this->basePath = $basePath; |
||
36 | |||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | public function useRootNamespace(string $rootNamespace): self |
||
41 | { |
||
42 | $this->rootNamespace = $rootNamespace; |
||
43 | |||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | public function ignoringFiles(array $ignoredFiles): self |
||
48 | { |
||
49 | $this->ignoredFiles = $ignoredFiles; |
||
50 | |||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | public function addToProjectionist(Projectionist $projectionist) |
||
55 | { |
||
56 | $files = (new Finder())->files()->in($this->directories); |
||
57 | |||
58 | return collect($files) |
||
59 | ->reject(function (SplFileInfo $file) { |
||
60 | return in_array($file->getPathname(), $this->ignoredFiles); |
||
61 | }) |
||
62 | ->map(function (SplFileInfo $file) { |
||
63 | return static::fullQualifiedClassNameFromFile($file); |
||
0 ignored issues
–
show
|
|||
64 | }) |
||
65 | ->filter(function (string $eventHandlerClass) { |
||
66 | return is_subclass_of($eventHandlerClass, EventHandler::class); |
||
67 | }) |
||
68 | ->pipe(function (Collection $eventHandlers) use ($projectionist) { |
||
69 | $projectionist->addEventHandlers($eventHandlers->toArray()); |
||
70 | }); |
||
71 | } |
||
72 | |||
73 | private function fullQualifiedClassNameFromFile(SplFileInfo $file): string |
||
74 | { |
||
75 | $class = trim(str_replace($this->basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR); |
||
76 | |||
77 | $class = str_replace( |
||
78 | [DIRECTORY_SEPARATOR, 'App\\'], |
||
79 | ['\\', app()->getNamespace()], |
||
80 | ucfirst(Str::replaceLast('.php', '', $class)) |
||
81 | ); |
||
82 | |||
83 | return $this->rootNamespace.$class; |
||
84 | } |
||
85 | } |
||
86 |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.