Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ParamConverter;
|
Chris@0
|
4
|
Chris@0
|
5 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
Chris@0
|
6 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Manages converter services for converting request parameters to full objects.
|
Chris@0
|
10 *
|
Chris@0
|
11 * A typical use case for this would be upcasting (converting) a node id to a
|
Chris@0
|
12 * node entity.
|
Chris@0
|
13 */
|
Chris@0
|
14 class ParamConverterManager implements ParamConverterManagerInterface {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Array of loaded converter services keyed by their ids.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $converters = [];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 public function addConverter(ParamConverterInterface $param_converter, $id) {
|
Chris@0
|
27 $this->converters[$id] = $param_converter;
|
Chris@0
|
28 return $this;
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 */
|
Chris@0
|
34 public function getConverter($converter) {
|
Chris@0
|
35 if (isset($this->converters[$converter])) {
|
Chris@0
|
36 return $this->converters[$converter];
|
Chris@0
|
37 }
|
Chris@0
|
38 else {
|
Chris@0
|
39 throw new \InvalidArgumentException(sprintf('No converter has been registered for %s', $converter));
|
Chris@0
|
40 }
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function setRouteParameterConverters(RouteCollection $routes) {
|
Chris@0
|
47 foreach ($routes->all() as $route) {
|
Chris@0
|
48 if (!$parameters = $route->getOption('parameters')) {
|
Chris@0
|
49 // Continue with the next route if no parameters have been defined.
|
Chris@0
|
50 continue;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 // Loop over all defined parameters and look up the right converter.
|
Chris@0
|
54 foreach ($parameters as $name => &$definition) {
|
Chris@0
|
55 if (isset($definition['converter'])) {
|
Chris@0
|
56 // Skip parameters that already have a manually set converter.
|
Chris@0
|
57 continue;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 foreach (array_keys($this->converters) as $converter) {
|
Chris@0
|
61 if ($this->getConverter($converter)->applies($definition, $name, $route)) {
|
Chris@0
|
62 $definition['converter'] = $converter;
|
Chris@0
|
63 break;
|
Chris@0
|
64 }
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 // Override the parameters array.
|
Chris@0
|
69 $route->setOption('parameters', $parameters);
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public function convert(array $defaults) {
|
Chris@0
|
77 /** @var $route \Symfony\Component\Routing\Route */
|
Chris@0
|
78 $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
|
Chris@0
|
79
|
Chris@0
|
80 // Skip this enhancer if there are no parameter definitions.
|
Chris@0
|
81 if (!$parameters = $route->getOption('parameters')) {
|
Chris@0
|
82 return $defaults;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 // Invoke the registered converter for each parameter.
|
Chris@0
|
86 foreach ($parameters as $name => $definition) {
|
Chris@0
|
87 if (!isset($defaults[$name])) {
|
Chris@0
|
88 // Do not try to convert anything that is already set to NULL.
|
Chris@0
|
89 continue;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 if (!isset($definition['converter'])) {
|
Chris@0
|
93 // Continue if no converter has been specified.
|
Chris@0
|
94 continue;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 // If a converter returns NULL it means that the parameter could not be
|
Chris@0
|
98 // converted.
|
Chris@0
|
99 $value = $defaults[$name];
|
Chris@0
|
100 $defaults[$name] = $this->getConverter($definition['converter'])->convert($value, $definition, $name, $defaults);
|
Chris@0
|
101 if (!isset($defaults[$name])) {
|
Chris@0
|
102 $message = 'The "%s" parameter was not converted for the path "%s" (route name: "%s")';
|
Chris@0
|
103 $route_name = $defaults[RouteObjectInterface::ROUTE_NAME];
|
Chris@0
|
104 throw new ParamNotConvertedException(sprintf($message, $name, $route->getPath(), $route_name), 0, NULL, $route_name, [$name => $value]);
|
Chris@0
|
105 }
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 return $defaults;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 }
|