Skip to content

Commit 7f66267

Browse files
committed
some tweaks to the controller argument resolver
* update some docblocks * remove the `LegacyArgumentResolver` class * simplify the `TraceableControllerResolver`
1 parent 804b5f0 commit 7f66267

File tree

6 files changed

+48
-217
lines changed

6 files changed

+48
-217
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ CHANGELOG
44
3.1.0
55
-----
66
* deprecated passing objects as URI attributes to the ESI and SSI renderers
7-
* added `Symfony\Component\HttpKernel\Controller\LegacyArgumentResolver`
87
* deprecated `ControllerResolver::getArguments()`
9-
* made `ControllerResolver` extend the `LegacyArgumentResolver` for BC
108
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface`
119
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface` as argument to `HttpKernel`
1210
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolver`

Controller/ArgumentValueResolverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface ArgumentValueResolverInterface
3232
public function supports(Request $request, ArgumentMetadata $argument);
3333

3434
/**
35-
* Yield the possible value(s).
35+
* Returns the possible value(s).
3636
*
3737
* @param Request $request
3838
* @param ArgumentMetadata $argument

Controller/ControllerResolver.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Fabien Potencier <[email protected]>
2525
*/
26-
class ControllerResolver extends LegacyArgumentResolver implements ControllerResolverInterface
26+
class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface
2727
{
2828
private $logger;
2929

@@ -85,23 +85,58 @@ public function getController(Request $request)
8585
/**
8686
* {@inheritdoc}
8787
*
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.
8989
*/
9090
public function getArguments(Request $request, $controller)
9191
{
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);
9393

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());
95104
}
96105

97106
/**
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.
99108
*/
100109
protected function doGetArguments(Request $request, $controller, array $parameters)
101110
{
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+
}
103138

104-
return parent::doGetArguments($request, $controller, $parameters);
139+
return $arguments;
105140
}
106141

107142
/**

Controller/LegacyArgumentResolver.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

Controller/TraceableControllerResolver.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function __construct(ControllerResolverInterface $resolver, Stopwatch $st
4242
if (null === $this->argumentResolver) {
4343
$this->argumentResolver = $resolver;
4444
}
45+
46+
if (!$this->argumentResolver instanceof TraceableArgumentResolver) {
47+
$this->argumentResolver = new TraceableArgumentResolver($this->argumentResolver, $this->stopwatch);
48+
}
4549
}
4650

4751
/**
@@ -65,18 +69,10 @@ public function getController(Request $request)
6569
*/
6670
public function getArguments(Request $request, $controller)
6771
{
68-
@trigger_error(sprintf('This %s method is deprecated as of 3.1 and will be removed in 4.0. Please use the %s instead.', __METHOD__, TraceableArgumentResolver::class), E_USER_DEPRECATED);
69-
70-
if ($this->argumentResolver instanceof TraceableArgumentResolver) {
71-
return $this->argumentResolver->getArguments($request, $controller);
72-
}
73-
74-
$e = $this->stopwatch->start('controller.get_arguments');
72+
@trigger_error(sprintf('The %s method is deprecated as of 3.1 and will be removed in 4.0. Please use the %s instead.', __METHOD__, TraceableArgumentResolver::class), E_USER_DEPRECATED);
7573

7674
$ret = $this->argumentResolver->getArguments($request, $controller);
7775

78-
$e->stop();
79-
8076
return $ret;
8177
}
8278
}

Tests/Controller/LegacyArgumentResolverTest.php

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)