-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Added an ArgumentResolver with clean extension point #18308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Responsible for the creation of the action arguments. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ArgumentResolver implements ArgumentResolverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getArguments(Request $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()); | ||
} | ||
|
||
protected function doGetArguments(Request $request, $controller, array $parameters) | ||
{ | ||
$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 $arguments; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* An ArgumentResolverInterface implementation knows how to determine the | ||
* arguments for a specific action. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
interface ArgumentResolverInterface | ||
{ | ||
/** | ||
* Returns the arguments to pass to the controller. | ||
* | ||
* @param Request $request A Request instance | ||
* @param callable $controller A PHP callable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @return array An array of arguments to pass to the controller | ||
* | ||
* @throws \RuntimeException When value for argument given is not provided | ||
*/ | ||
public function getArguments(Request $request, $controller); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just checked a bunch of files including the ones you mentioned and I think it's github, all files I've checked have a blank line in the end for me |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller; | ||
|
||
use Symfony\Component\Stopwatch\Stopwatch; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class TraceableArgumentResolver implements ArgumentResolverInterface | ||
{ | ||
private $resolver; | ||
private $stopwatch; | ||
|
||
public function __construct(ArgumentResolverInterface $resolver, Stopwatch $stopwatch) | ||
{ | ||
$this->resolver = $resolver; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getArguments(Request $request, $controller) | ||
{ | ||
$e = $this->stopwatch->start('controller.get_arguments'); | ||
|
||
$ret = $this->resolver->getArguments($request, $controller); | ||
|
||
$e->stop(); | ||
|
||
return $ret; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,21 +19,28 @@ | |
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class TraceableControllerResolver implements ControllerResolverInterface | ||
class TraceableControllerResolver implements ControllerResolverInterface, ArgumentResolverInterface | ||
{ | ||
private $resolver; | ||
private $stopwatch; | ||
private $argumentResolver; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ControllerResolverInterface $resolver A ControllerResolverInterface instance | ||
* @param Stopwatch $stopwatch A Stopwatch instance | ||
* @param ControllerResolverInterface $resolver A ControllerResolverInterface instance | ||
* @param Stopwatch $stopwatch A Stopwatch instance | ||
* @param ArgumentResolverInterface $argumentResolver Only required for BC | ||
*/ | ||
public function __construct(ControllerResolverInterface $resolver, Stopwatch $stopwatch) | ||
public function __construct(ControllerResolverInterface $resolver, Stopwatch $stopwatch, ArgumentResolverInterface $argumentResolver = null) | ||
{ | ||
$this->resolver = $resolver; | ||
$this->stopwatch = $stopwatch; | ||
$this->argumentResolver = $argumentResolver; | ||
|
||
if (null === $this->argumentResolver) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is for BC you should add a comment so we don't forget to remove the block in 4.0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be implied by the getArguments method deprecation but adding it just in case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
$this->argumentResolver = $resolver; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -52,12 +59,20 @@ public function getController(Request $request) | |
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. | ||
*/ | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even better could be to always wrap the resolver in a traceable one if not done already, to avoid duplicating the logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's sadly incompatible because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's preventing you from testing it ? (even in a later PR) |
||
return $this->argumentResolver->getArguments($request, $controller); | ||
} | ||
|
||
$e = $this->stopwatch->start('controller.get_arguments'); | ||
|
||
$ret = $this->resolver->getArguments($request, $controller); | ||
$ret = $this->argumentResolver->getArguments($request, $controller); | ||
|
||
$e->stop(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch