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