1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jerowork\RouteAttributeProvider; |
6
|
|
|
|
7
|
|
|
use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder; |
8
|
|
|
use Jerowork\FileClassReflector\PhpDocumentor\PhpDocumentorClassReflectorFactory; |
9
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\Cache\CacheRouteLoaderDecorator; |
10
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\ClassReflector\ClassReflectorRouteLoader; |
11
|
|
|
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface; |
12
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactory; |
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
14
|
|
|
|
15
|
|
|
final class RouteAttributeConfigurator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string[] $directories |
19
|
|
|
*/ |
20
|
|
|
private array $directories = []; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
private RouteAttributeProviderInterface $routeAttributeProvider, |
24
|
|
|
private ?RouteLoaderInterface $routeLoader = null |
25
|
3 |
|
) { |
26
|
|
|
$this->routeLoader = $this->routeLoader ?? new ClassReflectorRouteLoader( |
27
|
|
|
new PhpDocumentorClassReflectorFactory( |
28
|
|
|
ProjectFactory::createInstance(), |
29
|
|
|
new RegexIteratorFileFinder() |
30
|
3 |
|
) |
31
|
3 |
|
); |
32
|
3 |
|
} |
33
|
3 |
|
|
34
|
3 |
|
public function addDirectory(string ...$directories): self |
35
|
|
|
{ |
36
|
1 |
|
foreach ($directories as $directory) { |
37
|
|
|
$this->directories[] = $directory; |
38
|
1 |
|
} |
39
|
1 |
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
1 |
|
|
43
|
|
|
public function enableCache(CacheInterface $cache): self |
44
|
|
|
{ |
45
|
1 |
|
$this->routeLoader = new CacheRouteLoaderDecorator($this->routeLoader, $cache); |
|
|
|
|
46
|
|
|
|
47
|
1 |
|
return $this; |
48
|
|
|
} |
49
|
1 |
|
|
50
|
|
|
public function configure(): void |
51
|
|
|
{ |
52
|
1 |
|
foreach ($this->routeLoader->addDirectory(...$this->directories)->getRoutes() as $loadedRoute) { |
|
|
|
|
53
|
|
|
$this->routeAttributeProvider->configure( |
54
|
1 |
|
$loadedRoute->getClassName(), |
55
|
1 |
|
$loadedRoute->getMethodName(), |
56
|
1 |
|
$loadedRoute->getRoute() |
57
|
|
|
); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
2 |
|
public function getRouteLoader(): RouteLoaderInterface |
62
|
|
|
{ |
63
|
2 |
|
return $this->routeLoader; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|