-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Extracting the argument resolving from the ControllerResolver #18187
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 all commits
c0331a0
a57d6ae
4c997f5
70c6341
b3ed1f0
750dae9
c20eb4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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); | ||
} | ||
|
||
F438 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 | ||
* | ||
* @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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ControllerResolver implements ControllerResolverInterface | ||
class ControllerResolver extends ArgumentResolver implements ControllerResolverInterface | ||
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. As the min. PHP version is 5.5.9, IMO this could be trait not "normal" class, WDYT? 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. How would you envision this as a trait? By extending I make sure the children of this class will keep working. By making a trait I would have to also add this trait in the argument resolver. I've thought about decoration instead, but that would break any child class implementing 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. Yes, Drupal does use doGetArguments |
||
{ | ||
private $logger; | ||
|
||
|
@@ -84,50 +84,24 @@ 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 ArgumentResolver instead. | ||
*/ | ||
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); | ||
} | ||
@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, ArgumentResolver::class), E_USER_DEPRECATED); | ||
|
||
return $this->doGetArguments($request, $controller, $r->getParameters()); | ||
return parent::getArguments($request, $controller); | ||
} | ||
|
||
/** | ||
* @deprecated this method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface or extend the ArgumentResolver instead. | ||
*/ | ||
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)); | ||
} | ||
} | ||
@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, ArgumentResolver::class), E_USER_DEPRECATED); | ||
|
||
return $arguments; | ||
return parent::doGetArguments($request, $controller, $parameters); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,8 @@ public function getController(Request $request); | |
* @return array An array of arguments to pass to the controller | ||
* | ||
* @throws \RuntimeException When value for argument given is not provided | ||
* | ||
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Please use the {@see ArgumentResolverInterface} instead. | ||
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. To keep BC this interface shouldn't extend the new one? 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 thought about it, but that would result in being unable to detect whether the I rather have the interfaces in the new layout and have the current implementation provide a BC layer. 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 is definitely a BC break that we can't afford in a minor version. 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. Implementations won't break from this unless explicitly using the edit: and when it IS used elsewhere, it will be ducktyping to the ControllerResolver in the default cases either way, thus not break. 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.
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. @fabpot would you want the interfaces extending each other?
Or do you want to have:
I prefer to leave it as is and take the "break" for granted, if you can really call it a break. 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. We won't remove a method from an interface, that's a BC break and we never allowed that to happen in a minor version. But, we did deprecate methods on interfaces before, we just always found a way to make it work in all cases. I haven't had a look at how this could be possible, but I will later on. |
||
*/ | ||
public function getArguments(Request $request, $controller); | ||
} |
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) { | ||
$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); | ||
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. Woops, a small typo, |
||
|
||
if ($this->argumentResolver instanceof TraceableArgumentResolver) { | ||
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.
Is it a conscious decision to not include the "callable" typehint to the $controller variable? It is included in the docs.
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.
Changing the actual typehint would be a BC break. I know this was not done for 2.* because of the 5.3 support (introduced in 5.4 afaik). Maybe this typehint could be added in 4.0.