1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jerowork\RouteAttributeProvider\RouteLoader\ClassReflector; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use Jerowork\FileClassReflector\ClassReflectorFactory; |
9
|
|
|
use Jerowork\RouteAttributeProvider\Api\Route; |
10
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\LoadedRoute; |
11
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface; |
12
|
|
|
|
13
|
|
|
final class ClassReflectorRouteLoader implements RouteLoaderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string[] |
17
|
|
|
*/ |
18
|
|
|
private array $directories = []; |
19
|
|
|
|
20
|
4 |
|
public function __construct(private ClassReflectorFactory $reflectorFactory) |
21
|
|
|
{ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getDirectories() : array |
25
|
|
|
{ |
26
|
|
|
return $this->directories; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function addDirectory(string ...$directories) : RouteLoaderInterface |
30
|
|
|
{ |
31
|
|
|
/** @psalm-suppress DuplicateArrayKey */ |
32
|
2 |
|
$this->directories = [...$this->directories, ...$directories]; |
|
|
|
|
33
|
|
|
|
34
|
2 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function getRoutes() : Generator |
38
|
|
|
{ |
39
|
2 |
|
$reflector = $this->reflectorFactory->create()->addDirectory(...$this->directories); |
40
|
2 |
|
foreach ($reflector->reflect()->getClasses() as $class) { |
41
|
2 |
|
$hasClassAttribute = false; |
42
|
|
|
|
43
|
2 |
|
foreach ($class->getAttributes(Route::class) as $attribute) { |
44
|
2 |
|
$hasClassAttribute = true; |
45
|
|
|
|
46
|
|
|
/** @var Route $route */ |
47
|
2 |
|
$route = $attribute->newInstance(); |
48
|
|
|
|
49
|
2 |
|
yield new LoadedRoute($class->getName(), '__invoke', $route); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
if (!$hasClassAttribute) { |
53
|
2 |
|
foreach ($class->getMethods() as $method) { |
54
|
2 |
|
foreach ($method->getAttributes(Route::class) as $attribute) { |
55
|
|
|
/** @var Route $route */ |
56
|
2 |
|
$route = $attribute->newInstance(); |
57
|
|
|
|
58
|
2 |
|
yield new LoadedRoute($class->getName(), $method->getName(), $route); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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..