|
23 | 23 | *
|
24 | 24 | * @author Fabien Potencier <[email protected]>
|
25 | 25 | */
|
26 |
| -class ControllerResolver extends LegacyArgumentResolver implements ControllerResolverInterface |
| 26 | +class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface |
27 | 27 | {
|
28 | 28 | private $logger;
|
29 | 29 |
|
@@ -85,23 +85,58 @@ public function getController(Request $request)
|
85 | 85 | /**
|
86 | 86 | * {@inheritdoc}
|
87 | 87 | *
|
88 |
| - * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface or extend the LegacyArgumentResolver instead. |
| 88 | + * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead. |
89 | 89 | */
|
90 | 90 | public function getArguments(Request $request, $controller)
|
91 | 91 | {
|
92 |
| - @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s or extend the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class, LegacyArgumentResolver::class), E_USER_DEPRECATED); |
| 92 | + @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED); |
93 | 93 |
|
94 |
| - return parent::getArguments($request, $controller); |
| 94 | + if (is_array($controller)) { |
| 95 | + $r = new \ReflectionMethod($controller[0], $controller[1]); |
| 96 | + } elseif (is_object($controller) && !$controller instanceof \Closure) { |
| 97 | + $r = new \ReflectionObject($controller); |
| 98 | + $r = $r->getMethod('__invoke'); |
| 99 | + } else { |
| 100 | + $r = new \ReflectionFunction($controller); |
| 101 | + } |
| 102 | + |
| 103 | + return $this->doGetArguments($request, $controller, $r->getParameters()); |
95 | 104 | }
|
96 | 105 |
|
97 | 106 | /**
|
98 |
| - * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface or extend the LegacyArgumentResolver instead. |
| 107 | + * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead. |
99 | 108 | */
|
100 | 109 | protected function doGetArguments(Request $request, $controller, array $parameters)
|
101 | 110 | {
|
102 |
| - @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s or extend the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class, LegacyArgumentResolver::class), E_USER_DEPRECATED); |
| 111 | + @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED); |
| 112 | + |
| 113 | + $attributes = $request->attributes->all(); |
| 114 | + $arguments = array(); |
| 115 | + foreach ($parameters as $param) { |
| 116 | + if (array_key_exists($param->name, $attributes)) { |
| 117 | + if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) { |
| 118 | + $arguments = array_merge($arguments, array_values($attributes[$param->name])); |
| 119 | + } else { |
| 120 | + $arguments[] = $attributes[$param->name]; |
| 121 | + } |
| 122 | + } elseif ($param->getClass() && $param->getClass()->isInstance($request)) { |
| 123 | + $arguments[] = $request; |
| 124 | + } elseif ($param->isDefaultValueAvailable()) { |
| 125 | + $arguments[] = $param->getDefaultValue(); |
| 126 | + } else { |
| 127 | + if (is_array($controller)) { |
| 128 | + $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); |
| 129 | + } elseif (is_object($controller)) { |
| 130 | + $repr = get_class($controller); |
| 131 | + } else { |
| 132 | + $repr = $controller; |
| 133 | + } |
| 134 | + |
| 135 | + throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name)); |
| 136 | + } |
| 137 | + } |
103 | 138 |
|
104 |
| - return parent::doGetArguments($request, $controller, $parameters); |
| 139 | + return $arguments; |
105 | 140 | }
|
106 | 141 |
|
107 | 142 | /**
|
|
0 commit comments