1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jerowork\RouteAttributeProvider; |
6
|
|
|
|
7
|
|
|
use Jerowork\RouteAttributeProvider\ClassNameLoader\ClassNameLoaderInterface; |
8
|
|
|
use Jerowork\RouteAttributeProvider\ClassNameLoader\Tokenizer\TokenizerClassNameLoader; |
9
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\Reflection\ReflectionRouteLoader; |
10
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface; |
11
|
|
|
|
12
|
|
|
final class RouteAttributeConfigurator |
13
|
|
|
{ |
14
|
|
|
private RouteAttributeProviderInterface $routeAttributeProvider; |
15
|
|
|
private ClassNameLoaderInterface $classNameLoader; |
16
|
|
|
private RouteLoaderInterface $routeLoader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string[] $directories |
20
|
|
|
*/ |
21
|
|
|
private array $directories; |
22
|
|
|
|
23
|
1 |
|
public function __construct( |
24
|
|
|
RouteAttributeProviderInterface $routeAttributeProvider, |
25
|
|
|
?ClassNameLoaderInterface $classNameLoader = null, |
26
|
|
|
?RouteLoaderInterface $routeLoader = null |
27
|
|
|
) { |
28
|
1 |
|
$this->routeAttributeProvider = $routeAttributeProvider; |
29
|
1 |
|
$this->classNameLoader = $classNameLoader ?? new TokenizerClassNameLoader(); |
30
|
1 |
|
$this->routeLoader = $routeLoader ?? new ReflectionRouteLoader(); |
31
|
1 |
|
$this->directories = []; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
1 |
|
public function addDirectory(string ...$directories) : self |
35
|
|
|
{ |
36
|
1 |
|
foreach ($directories as $directory) { |
37
|
1 |
|
$this->directories[] = $directory; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function configure(): void |
44
|
|
|
{ |
45
|
1 |
|
foreach ($this->directories as $directory) { |
46
|
1 |
|
foreach ($this->classNameLoader->load($directory) as $className) { |
47
|
1 |
|
$this->configureForClassName($className); |
48
|
|
|
} |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param class-string $className |
|
|
|
|
54
|
|
|
*/ |
55
|
1 |
|
private function configureForClassName(string $className): void |
56
|
|
|
{ |
57
|
1 |
|
$loadedRoutes = $this->routeLoader->load($className); |
58
|
|
|
|
59
|
1 |
|
foreach ($loadedRoutes as $loadedRoute) { |
60
|
1 |
|
$this->routeAttributeProvider->configure( |
61
|
1 |
|
$loadedRoute->getClassName(), |
62
|
1 |
|
$loadedRoute->getMethodName(), |
63
|
1 |
|
$loadedRoute->getRoute() |
64
|
|
|
); |
65
|
|
|
} |
66
|
1 |
|
} |
67
|
|
|
} |
68
|
|
|
|