|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jerowork\RouteAttributeProvider\RouteLoader\Roave; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use Jerowork\RouteAttributeProvider\Api\Route; |
|
9
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\LoadedRoute; |
|
10
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
13
|
|
|
use Roave\BetterReflection\Reflector\DefaultReflector; |
|
14
|
|
|
use Roave\BetterReflection\Reflector\Reflector; |
|
15
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
|
16
|
|
|
|
|
17
|
|
|
final class RoaveRouterLoader implements RouteLoaderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $directories = []; |
|
23
|
|
|
|
|
24
|
4 |
|
public function __construct( |
|
25
|
|
|
private readonly FileFinder $fileFinder, |
|
26
|
|
|
) { |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getDirectories() : array |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->directories; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function addDirectory(string ...$directories) : RouteLoaderInterface |
|
35
|
|
|
{ |
|
36
|
2 |
|
$this->directories = [...$this->directories, ...$directories]; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
2 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
public function getRoutes() : Generator |
|
42
|
|
|
{ |
|
43
|
2 |
|
foreach ($this->fileFinder->getFiles(...$this->directories) as $file) { |
|
44
|
|
|
/** @var non-empty-string $file */ |
|
45
|
2 |
|
$reflector = $this->createRoaveReflectorForFile($file); |
|
46
|
|
|
|
|
47
|
2 |
|
foreach ($reflector->reflectAllClasses() as $reflectorClass) { |
|
48
|
2 |
|
$hasClassAttribute = false; |
|
49
|
2 |
|
$class = new ReflectionClass($reflectorClass->getName()); |
|
50
|
|
|
|
|
51
|
2 |
|
foreach ($class->getAttributes(Route::class) as $attribute) { |
|
52
|
2 |
|
$hasClassAttribute = true; |
|
53
|
|
|
|
|
54
|
|
|
/** @var Route $route */ |
|
55
|
2 |
|
$route = $attribute->newInstance(); |
|
56
|
|
|
|
|
57
|
2 |
|
yield new LoadedRoute($class->getName(), '__invoke', $route); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
if (!$hasClassAttribute) { |
|
61
|
2 |
|
foreach ($class->getMethods() as $method) { |
|
62
|
2 |
|
foreach ($method->getAttributes(Route::class) as $attribute) { |
|
63
|
|
|
/** @var Route $route */ |
|
64
|
2 |
|
$route = $attribute->newInstance(); |
|
65
|
|
|
|
|
66
|
2 |
|
yield new LoadedRoute($class->getName(), $method->getName(), $route); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param non-empty-string $file |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function createRoaveReflectorForFile(string $file) : Reflector |
|
78
|
|
|
{ |
|
79
|
2 |
|
return new DefaultReflector(new SingleFileSourceLocator($file, (new BetterReflection())->astLocator())); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..