1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pgs\HashIdBundle\Service; |
6
|
|
|
|
7
|
|
|
use Pgs\HashIdBundle\ParametersProcessor\Factory\DecodeParametersProcessorFactory; |
8
|
|
|
use Pgs\HashIdBundle\ParametersProcessor\ParametersProcessorInterface; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
11
|
|
|
|
12
|
|
|
class DecodeControllerParameters |
13
|
|
|
{ |
14
|
|
|
protected $parametersProcessorFactory; |
15
|
|
|
|
16
|
|
|
protected $paramConverterListener; |
17
|
|
|
|
18
|
2 |
|
public function __construct(DecodeParametersProcessorFactory $parametersProcessorFactory) |
19
|
|
|
{ |
20
|
2 |
|
$this->parametersProcessorFactory = $parametersProcessorFactory; |
21
|
2 |
|
} |
22
|
|
|
|
23
|
2 |
|
public function decodeControllerParameters(FilterControllerEvent $event): void |
24
|
|
|
{ |
25
|
2 |
|
$controller = $event->getController(); |
26
|
2 |
|
if (\is_array($controller)) { |
27
|
|
|
list($controllerObject, $method) = $controller; |
28
|
2 |
|
} elseif (\is_object($controller) && !$controller instanceof \Closure) { |
29
|
2 |
|
$controllerObject = $controller; |
30
|
2 |
|
$method = '__invoke'; |
31
|
|
|
} else { |
32
|
|
|
//Controller is a closure |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$parametersProcessor = $this |
37
|
2 |
|
->getParametersProcessorFactory() |
38
|
2 |
|
->createControllerDecodeParametersProcessor($controllerObject, $method); |
39
|
|
|
|
40
|
2 |
|
$this->processRequestParameters($event, $parametersProcessor); |
41
|
2 |
|
$this->processRequestParametersWithParamConverter($event); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
protected function processRequestParameters( |
45
|
|
|
FilterControllerEvent $event, |
46
|
|
|
ParametersProcessorInterface $parametersProcessor |
47
|
|
|
): void { |
48
|
2 |
|
if ($parametersProcessor->needToProcess()) { |
49
|
2 |
|
$requestParams = $event->getRequest()->attributes->all(); |
50
|
2 |
|
$processedParams = $parametersProcessor->process($requestParams); |
51
|
2 |
|
$event->getRequest()->attributes->replace($processedParams); |
52
|
|
|
} |
53
|
2 |
|
} |
54
|
|
|
|
55
|
2 |
|
protected function processRequestParametersWithParamConverter(FilterControllerEvent $event): void |
56
|
|
|
{ |
57
|
2 |
|
if (null !== $this->getParamConverterListener()) { |
58
|
1 |
|
$this->getParamConverterListener()->onKernelController($event); |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
2 |
|
public function getParametersProcessorFactory(): DecodeParametersProcessorFactory |
63
|
|
|
{ |
64
|
2 |
|
return $this->parametersProcessorFactory; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function getParamConverterListener(): ?ParamConverterListener |
68
|
|
|
{ |
69
|
2 |
|
return $this->paramConverterListener; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function setParamConverterListener(ParamConverterListener $paramConverterListener): void |
73
|
|
|
{ |
74
|
1 |
|
$this->paramConverterListener = $paramConverterListener; |
75
|
1 |
|
} |
76
|
|
|
} |
77
|
|
|
|