1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\Request\ParamConverter; |
4
|
|
|
|
5
|
|
|
use Drupal\controller_annotations\Configuration\ConfigurationInterface; |
6
|
|
|
use Drupal\controller_annotations\Configuration\ParamConverter; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Fabien Potencier <[email protected]> |
11
|
|
|
* @author Henrik Bjornskov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ParamConverterManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $converters = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $namedConverters = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Applies all converters to the passed configurations and stops when a |
27
|
|
|
* converter is applied it will move on to the next configuration and so on. |
28
|
|
|
* |
29
|
|
|
* @param Request $request |
30
|
|
|
* @param array|object $configurations |
31
|
|
|
*/ |
32
|
13 |
|
public function apply(Request $request, $configurations) |
33
|
|
|
{ |
34
|
13 |
|
if (is_object($configurations)) { |
35
|
1 |
|
$configurations = [$configurations]; |
36
|
|
|
} |
37
|
|
|
|
38
|
13 |
|
foreach ($configurations as $configuration) { |
39
|
7 |
|
$this->applyConverter($request, $configuration); |
40
|
|
|
} |
41
|
11 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Apply converter on request based on the given configuration. |
45
|
|
|
* |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param ParamConverter $configuration |
48
|
|
|
*/ |
49
|
7 |
|
protected function applyConverter(Request $request, ParamConverter $configuration) |
50
|
|
|
{ |
51
|
7 |
|
$value = $request->attributes->get($configuration->getName()); |
52
|
7 |
|
$className = $configuration->getClass(); |
53
|
|
|
|
54
|
|
|
// If the value is already an instance of the class we are trying to |
55
|
|
|
// convert it into we should continue as no conversion is required |
56
|
7 |
|
if (is_object($value) && $value instanceof $className) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
if ($configuration->getConverter()) { |
61
|
3 |
|
$this->applyNamedConverter($request, $configuration); |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
foreach ($this->all() as $converter) { |
66
|
3 |
|
if ($converter->supports($configuration)) { |
67
|
2 |
|
if ($converter->apply($request, $configuration)) { |
68
|
3 |
|
return; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Request $request |
76
|
|
|
* @param ParamConverter $configuration |
77
|
|
|
*/ |
78
|
3 |
|
protected function applyNamedConverter(Request $request, ParamConverter $configuration) |
79
|
|
|
{ |
80
|
3 |
|
$converterName = $configuration->getConverter(); |
81
|
3 |
|
if (!isset($this->namedConverters[$converterName])) { |
82
|
1 |
|
throw new \RuntimeException(sprintf( |
83
|
1 |
|
"No converter named '%s' found for conversion of parameter '%s'.", |
84
|
1 |
|
$converterName, $configuration->getName() |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$converter = $this->namedConverters[$converterName]; |
89
|
|
|
|
90
|
2 |
|
if (!$converter->supports($configuration)) { |
91
|
1 |
|
throw new \RuntimeException(sprintf( |
92
|
1 |
|
"Converter '%s' does not support conversion of parameter '%s'.", |
93
|
1 |
|
$converterName, $configuration->getName() |
94
|
|
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$converter->apply($request, $configuration); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Adds a parameter converter. |
102
|
|
|
* |
103
|
|
|
* Converters match either explicitly via $name or by iteration over all |
104
|
|
|
* converters with a $priority. If you pass a $priority = null then the |
105
|
|
|
* added converter will not be part of the iteration chain and can only |
106
|
|
|
* be invoked explicitly. |
107
|
|
|
* |
108
|
|
|
* @param ParamConverterInterface $converter A ParamConverterInterface instance |
109
|
|
|
* @param int $priority The priority (between -10 and 10). |
110
|
|
|
* @param string $name Name of the converter. |
111
|
|
|
*/ |
112
|
13 |
|
public function add(ParamConverterInterface $converter, $priority = 0, $name = null) |
113
|
|
|
{ |
114
|
13 |
|
if ($priority !== null) { |
115
|
13 |
|
if (!isset($this->converters[$priority])) { |
116
|
13 |
|
$this->converters[$priority] = []; |
117
|
|
|
} |
118
|
|
|
|
119
|
13 |
|
$this->converters[$priority][] = $converter; |
120
|
|
|
} |
121
|
|
|
|
122
|
13 |
|
if (null !== $name) { |
123
|
2 |
|
$this->namedConverters[$name] = $converter; |
124
|
|
|
} |
125
|
13 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns all registered param converters. |
129
|
|
|
* |
130
|
|
|
* @return array An array of param converters |
131
|
|
|
*/ |
132
|
4 |
|
public function all() |
133
|
|
|
{ |
134
|
4 |
|
krsort($this->converters); |
135
|
|
|
|
136
|
4 |
|
$converters = array(); |
137
|
4 |
|
foreach ($this->converters as $all) { |
138
|
4 |
|
$converters = array_merge($converters, $all); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
return $converters; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|