|
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\Cache\CacheRouteLoaderDecorator; |
|
10
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\Reflection\ReflectionRouteLoader; |
|
11
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface; |
|
12
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class RouteAttributeConfigurator |
|
15
|
|
|
{ |
|
16
|
|
|
private RouteAttributeProviderInterface $routeAttributeProvider; |
|
17
|
|
|
private ClassNameLoaderInterface $classNameLoader; |
|
18
|
|
|
private RouteLoaderInterface $routeLoader; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] $directories |
|
22
|
|
|
*/ |
|
23
|
|
|
private array $directories; |
|
24
|
|
|
|
|
25
|
3 |
|
public function __construct( |
|
26
|
|
|
RouteAttributeProviderInterface $routeAttributeProvider, |
|
27
|
|
|
?ClassNameLoaderInterface $classNameLoader = null, |
|
28
|
|
|
?RouteLoaderInterface $routeLoader = null |
|
29
|
|
|
) { |
|
30
|
3 |
|
$this->routeAttributeProvider = $routeAttributeProvider; |
|
31
|
3 |
|
$this->classNameLoader = $classNameLoader ?? new TokenizerClassNameLoader(); |
|
32
|
3 |
|
$this->routeLoader = $routeLoader ?? new ReflectionRouteLoader(); |
|
33
|
3 |
|
$this->directories = []; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function addDirectory(string ...$directories): self |
|
37
|
|
|
{ |
|
38
|
1 |
|
foreach ($directories as $directory) { |
|
39
|
1 |
|
$this->directories[] = $directory; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function enableCache(CacheInterface $cache): self |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->routeLoader = new CacheRouteLoaderDecorator($this->routeLoader, $cache); |
|
48
|
|
|
|
|
49
|
1 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function configure(): void |
|
53
|
|
|
{ |
|
54
|
1 |
|
foreach ($this->directories as $directory) { |
|
55
|
1 |
|
foreach ($this->classNameLoader->load($directory) as $className) { |
|
56
|
1 |
|
$this->configureForClassName($className); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function getRouteLoader(): RouteLoaderInterface |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->routeLoader; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param class-string $className |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
1 |
|
private function configureForClassName(string $className): void |
|
70
|
|
|
{ |
|
71
|
1 |
|
$loadedRoutes = $this->routeLoader->load($className); |
|
72
|
|
|
|
|
73
|
1 |
|
foreach ($loadedRoutes as $loadedRoute) { |
|
74
|
1 |
|
$this->routeAttributeProvider->configure( |
|
75
|
1 |
|
$loadedRoute->getClassName(), |
|
76
|
1 |
|
$loadedRoute->getMethodName(), |
|
77
|
1 |
|
$loadedRoute->getRoute() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|