|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\controller_annotations\Configuration\ParamConverter; |
|
6
|
|
|
use Drupal\controller_annotations\Request\ParamConverter\ParamConverterManager; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
|
|
12
|
|
|
class ParamConverterEventSubscriber implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ParamConverterManager |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $manager; |
|
18
|
|
|
|
|
19
|
|
|
protected $autoConvert; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $isParameterTypeSupported; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param ParamConverterManager $manager A ParamConverterManager instance |
|
30
|
|
|
* @param bool $autoConvert Auto convert non-configured objects |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function __construct(ParamConverterManager $manager, $autoConvert = true) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->manager = $manager; |
|
35
|
6 |
|
$this->autoConvert = $autoConvert; |
|
36
|
6 |
|
$this->isParameterTypeSupported = method_exists('ReflectionParameter', 'getType'); |
|
37
|
6 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Modifies the ParamConverterManager instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param FilterControllerEvent $event A FilterControllerEvent instance |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function onKernelController(FilterControllerEvent $event) |
|
45
|
|
|
{ |
|
46
|
6 |
|
$controller = $event->getController(); |
|
47
|
6 |
|
$request = $event->getRequest(); |
|
48
|
6 |
|
$configurations = array(); |
|
49
|
|
|
|
|
50
|
6 |
|
if ($configuration = $request->attributes->get('_converters')) { |
|
51
|
|
|
foreach (is_array($configuration) ? $configuration : array($configuration) as $configuration) { |
|
52
|
|
|
$configurations[$configuration->getName()] = $configuration; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
if (is_array($controller)) { |
|
57
|
6 |
|
$r = new \ReflectionMethod($controller[0], $controller[1]); |
|
58
|
|
|
} elseif (is_object($controller) && is_callable($controller, '__invoke')) { |
|
59
|
|
|
$r = new \ReflectionMethod($controller, '__invoke'); |
|
60
|
|
|
} else { |
|
61
|
|
|
$r = new \ReflectionFunction($controller); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// automatically apply conversion for non-configured objects |
|
65
|
6 |
|
if ($this->autoConvert) { |
|
66
|
6 |
|
$configurations = $this->autoConfigure($r, $request, $configurations); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
6 |
|
$this->manager->apply($request, $configurations); |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
private function autoConfigure(\ReflectionFunctionAbstract $r, Request $request, $configurations) |
|
73
|
|
|
{ |
|
74
|
6 |
|
foreach ($r->getParameters() as $param) { |
|
75
|
1 |
|
if ($param->getClass() && $param->getClass()->isInstance($request)) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
$name = $param->getName(); |
|
|
|
|
|
|
80
|
1 |
|
$class = $param->getClass(); |
|
81
|
1 |
|
$hasType = $this->isParameterTypeSupported && $param->hasType(); |
|
82
|
|
|
|
|
83
|
1 |
|
if ($class || $hasType) { |
|
84
|
|
|
if (!isset($configurations[$name])) { |
|
85
|
|
|
$configuration = new ParamConverter([]); |
|
86
|
|
|
$configuration->setName($name); |
|
87
|
|
|
|
|
88
|
|
|
$configurations[$name] = $configuration; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($class && null === $configurations[$name]->getClass()) { |
|
92
|
|
|
$configurations[$name]->setClass($class->getName()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
if (isset($configurations[$name])) { |
|
97
|
1 |
|
$configurations[$name]->setIsOptional($param->isOptional() || $param->isDefaultValueAvailable() || $hasType && $param->getType()->allowsNull()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
return $configurations; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
6 |
|
public static function getSubscribedEvents() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
KernelEvents::CONTROLLER => [ |
|
111
|
|
|
['onKernelController', 100], |
|
112
|
6 |
|
], |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|