1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pgs\HashIdBundle\Decorator; |
6
|
|
|
|
7
|
|
|
use Pgs\HashIdBundle\ParametersProcessor\Factory\EncodeParametersProcessorFactory; |
8
|
|
|
use Pgs\HashIdBundle\Traits\DecoratorTrait; |
9
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
10
|
|
|
use Symfony\Component\Routing\RequestContext; |
11
|
|
|
use Symfony\Component\Routing\Route; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
|
14
|
|
|
class RouterDecorator implements RouterInterface, WarmableInterface |
15
|
|
|
{ |
16
|
|
|
use DecoratorTrait; |
17
|
|
|
|
18
|
|
|
protected $parametersProcessorFactory; |
19
|
|
|
|
20
|
|
|
public function __construct(RouterInterface $router, EncodeParametersProcessorFactory $parametersProcessorFactory) |
21
|
|
|
{ |
22
|
|
|
$this->object = $router; |
23
|
|
|
$this->parametersProcessorFactory = $parametersProcessorFactory; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getRouter(): RouterInterface |
27
|
|
|
{ |
28
|
|
|
return $this->object; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $name |
33
|
|
|
* @param array $parameters |
34
|
|
|
* @param int $referenceType |
35
|
|
|
*/ |
36
|
|
|
public function generate($name, $parameters = [], $referenceType = RouterInterface::ABSOLUTE_PATH): string |
37
|
|
|
{ |
38
|
|
|
$route = $this->getRouter()->getRouteCollection()->get($name); |
39
|
|
|
$this->processParameters($route, $parameters); |
40
|
|
|
|
41
|
|
|
return $this->getRouter()->generate($name, $parameters, $referenceType); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function processParameters(?Route $route, array &$parameters): void |
45
|
|
|
{ |
46
|
|
|
if (null !== $route) { |
47
|
|
|
$parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route); |
48
|
|
|
if ($parametersProcessor->needToProcess()) { |
49
|
|
|
$parameters = $parametersProcessor->process($parameters); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
public function setContext(RequestContext $context) |
58
|
|
|
{ |
59
|
|
|
$this->getRouter()->setContext($context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @codeCoverageIgnore |
64
|
|
|
*/ |
65
|
|
|
public function getContext() |
66
|
|
|
{ |
67
|
|
|
return $this->getRouter()->getContext(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @codeCoverageIgnore |
72
|
|
|
*/ |
73
|
|
|
public function getRouteCollection() |
74
|
|
|
{ |
75
|
|
|
return $this->getRouter()->getRouteCollection(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @codeCoverageIgnore |
80
|
|
|
*/ |
81
|
|
|
public function match($pathinfo) |
82
|
|
|
{ |
83
|
|
|
return $this->getRouter()->match($pathinfo); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @codeCoverageIgnore |
88
|
|
|
*/ |
89
|
|
|
public function warmUp($cacheDir) |
90
|
|
|
{ |
91
|
|
|
if ($this->getRouter() instanceof WarmableInterface) { |
92
|
|
|
$this->getRouter()->warmUp($cacheDir); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|