Skip to content

Commit

Permalink
minor #18495 [HttpKernel] some tweaks to the controller argument reso…
Browse files Browse the repository at this point in the history
…lver (xabbuh)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[HttpKernel] some tweaks to the controller argument resolver

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

* update some docblocks

* remove the `LegacyArgumentResolver` class

* simplify the `TraceableControllerResolver`

Commits
-------

3519770 some tweaks to the controller argument resolver
  • Loading branch information
fabpot committed Apr 11, 2016
2 parents 0b67fa3 + 3519770 commit a073a65
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 217 deletions.
2 changes: 0 additions & 2 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Expand Up @@ -4,9 +4,7 @@ CHANGELOG
3.1.0
-----
* deprecated passing objects as URI attributes to the ESI and SSI renderers
* added `Symfony\Component\HttpKernel\Controller\LegacyArgumentResolver`
* deprecated `ControllerResolver::getArguments()`
* made `ControllerResolver` extend the `LegacyArgumentResolver` for BC
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface`
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface` as argument to `HttpKernel`
* added `Symfony\Component\HttpKernel\Controller\ArgumentResolver`
Expand Down
Expand Up @@ -32,7 +32,7 @@ interface ArgumentValueResolverInterface
public function supports(Request $request, ArgumentMetadata $argument);

/**
* Yield the possible value(s).
* Returns the possible value(s).
*
* @param Request $request
* @param ArgumentMetadata $argument
Expand Down
49 changes: 42 additions & 7 deletions src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
Expand Up @@ -23,7 +23,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ControllerResolver extends LegacyArgumentResolver implements ControllerResolverInterface
class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface
{
private $logger;

Expand Down Expand Up @@ -85,23 +85,58 @@ public function getController(Request $request)
/**
* {@inheritdoc}
*
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface or extend the LegacyArgumentResolver instead.
* @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.
*/
public function getArguments(Request $request, $controller)
{
@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);
@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);

return parent::getArguments($request, $controller);
if (is_array($controller)) {
$r = new \ReflectionMethod($controller[0], $controller[1]);
} elseif (is_object($controller) && !$controller instanceof \Closure) {
$r = new \ReflectionObject($controller);
$r = $r->getMethod('__invoke');
} else {
$r = new \ReflectionFunction($controller);
}

return $this->doGetArguments($request, $controller, $r->getParameters());
}

/**
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface or extend the LegacyArgumentResolver instead.
* @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.
*/
protected function doGetArguments(Request $request, $controller, array $parameters)
{
@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);
@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);

$attributes = $request->attributes->all();
$arguments = array();
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
} else {
$arguments[] = $attributes[$param->name];
}
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue();
} else {
if (is_array($controller)) {
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
} elseif (is_object($controller)) {
$repr = get_class($controller);
} else {
$repr = $controller;
}

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));
}
}

return parent::doGetArguments($request, $controller, $parameters);
return $arguments;
}

/**
Expand Down

This file was deleted.

Expand Up @@ -42,6 +42,10 @@ public function __construct(ControllerResolverInterface $resolver, Stopwatch $st
if (null === $this->argumentResolver) {
$this->argumentResolver = $resolver;
}

if (!$this->argumentResolver instanceof TraceableArgumentResolver) {
$this->argumentResolver = new TraceableArgumentResolver($this->argumentResolver, $this->stopwatch);
}
}

/**
Expand All @@ -65,18 +69,10 @@ public function getController(Request $request)
*/
public function getArguments(Request $request, $controller)
{
@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);

if ($this->argumentResolver instanceof TraceableArgumentResolver) {
return $this->argumentResolver->getArguments($request, $controller);
}

$e = $this->stopwatch->start('controller.get_arguments');
@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);

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

$e->stop();

return $ret;
}
}

This file was deleted.

0 comments on commit a073a65

Please sign in to comment.