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