-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[2.8] Move argument resolving from ControllerResolver #14971
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
54a0653
3dfdb61
434f92f
a1d66c6
cf69cc7
3a1d99c
1741b2a
4924e86
1300ed2
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 |
---|---|---|
|
@@ -23,26 +23,30 @@ | |
class ArgumentResolverManager | ||
{ | ||
/** | ||
* @var ArgumentResolverInterface[] | ||
* @var array | ||
*/ | ||
private $resolvers; | ||
private $resolvers = array(); | ||
|
||
/** | ||
* @param ArgumentResolverInterface[] $resolvers | ||
* @var null|ArgumentResolverInterface[] | ||
*/ | ||
public function __construct(array $resolvers = array()) | ||
{ | ||
$this->resolvers = $resolvers; | ||
} | ||
private $sortedResolvers; | ||
|
||
/** | ||
* Adds an argument resolver. | ||
* | ||
* @param ArgumentResolverInterface $resolver | ||
*/ | ||
public function add(ArgumentResolverInterface $resolver) | ||
public function add(ArgumentResolverInterface $resolver, $priority = 0) | ||
{ | ||
$this->resolvers[] = $resolver; | ||
if (!isset($this->resolvers[$priority])) { | ||
$this->resolvers[$priority] = array(); | ||
} | ||
|
||
$this->resolvers[$priority][] = $resolver; | ||
|
||
// force a new sort of the resolvers | ||
$this->sortedResolvers = null; | ||
} | ||
|
||
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. This method will not work for controllers defined like 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. This class is designed to work with the ControllerResolver purely. The ControllerResolver already converts 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. missing phpdoc |
||
|
@@ -64,7 +68,7 @@ public function getArguments(Request $request, $controller) | |
$arguments = array(); | ||
|
||
foreach ($parameters as $parameter) { | ||
foreach ($this->resolvers as $argumentResolver) { | ||
foreach ($this->getSortedResolvers() as $argumentResolver) { | ||
if ($argumentResolver->supports($request, $parameter)) { | ||
$arguments[] = $argumentResolver->resolve($request, $parameter); | ||
continue 2; | ||
|
@@ -88,4 +92,15 @@ public function getArguments(Request $request, $controller) | |
|
||
return $arguments; | ||
} | ||
|
||
private function getSortedResolvers() | ||
{ | ||
if (null === $this->sortedResolvers) { | ||
$this->sortedResolvers = $this->resolvers; | ||
ksort($this->sortedResolvers); | ||
$this->sortedResolvers = call_user_func_array('array_merge', $this->sortedResolvers); | ||
} | ||
|
||
return $this->sortedResolvers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,10 +172,9 @@ protected function instantiateController($class) | |
private function getArgumentResolverManager() | ||
{ | ||
if (null === $this->argumentResolverManager) { | ||
$this->argumentResolverManager = new ArgumentResolverManager(array( | ||
new RequestArgumentResolver(), | ||
new RequestAttributesArgumentResolver(), | ||
)); | ||
$this->argumentResolverManager = new ArgumentResolverManager(); | ||
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 about adding constructor with optional argument |
||
$this->argumentResolverManager->add(new RequestArgumentResolver()); | ||
$this->argumentResolverManager->add(new RequestAttributesArgumentResolver()); | ||
} | ||
|
||
return $this->argumentResolverManager; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,10 +59,9 @@ public function __construct(EventDispatcherInterface $dispatcher, ControllerReso | |
$this->requestStack = $requestStack ?: new RequestStack(); | ||
|
||
if (null === $argumentResolverManager) { | ||
$argumentResolverManager = new ArgumentResolverManager(array( | ||
new RequestArgumentResolver(), | ||
new RequestAttributesArgumentResolver(), | ||
)); | ||
$argumentResolverManager = new ArgumentResolverManager(); | ||
$argumentResolverManager->add(new RequestArgumentResolver()); | ||
$argumentResolverManager->add(new RequestAttributesArgumentResolver()); | ||
} | ||
|
||
if (method_exists($resolver, 'setArgumentResolverManager')) { | ||
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 i create a ControllerResolver and use the method Is there a purpose for having this code here? 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. The This is to be BC. In 3.0, |
||
|
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.
do we really need the priority here ? IMO, we could just rely on having
->add()
being called in the right order, as we do in many other places.The sorting by priority would then be done in the compiler pass at compile time (the priority being used to give control over the order when building the container).
IIRC, the only place where we have the priority handled at runtime is for the event dispatcher, and this is because listeners are not all known at compile time.