Skip to content

[WIP] Refactored argument resolving out of the ControllerResolver #11457

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\DependencyInjection\RegisterArgumentResolversPass;

/**
* Bundle.
Expand Down Expand Up @@ -88,6 +89,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TranslationDumperPass());
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new SerializerPass());
$container->addCompilerPass(new RegisterArgumentResolversPass());

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="service_container" />
<argument type="service" id="controller_resolver" />
<argument type="service" id="request_stack" />
<argument type="service" id="argument_resolver.manager" />
</service>

<service id="request_stack" class="Symfony\Component\HttpFoundation\RequestStack" />
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
<argument type="service" id="logger" on-invalid="ignore" />
</service>

<service id="argument_resolver.manager" class="Symfony\Component\HttpKernel\Controller\ArgumentResolverManager" />

<service id="argument_resolver.request" class="Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestArgumentResolver">
<tag name="kernel.argument_resolver" />
</service>

<service id="argument_resolver.request_attributes" class="Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributesArgumentResolver">
<tag name="kernel.argument_resolver" />
</service>

<service id="response_listener" class="Symfony\Component\HttpKernel\EventListener\ResponseListener">
<tag name="kernel.event_subscriber" />
<argument>%kernel.charset%</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public function testGetControllerService()

$resolver = $this->createControllerResolver(null, null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', 'foo:controllerMethod1');
$request->attributes->set('_controller', 'foo:controllerMethod');

$controller = $resolver->getController($request);

$this->assertInstanceOf(get_class($this), $controller[0]);
$this->assertSame('controllerMethod1', $controller[1]);
$this->assertSame('controllerMethod', $controller[1]);
}

public function testGetControllerInvokableService()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.5.9",
"symfony/asset": "~2.7|~3.0",
"symfony/dependency-injection" : "~2.7|~3.0",
"symfony/dependency-injection" : "~3.0",
"symfony/config" : "~2.7|~3.0",
"symfony/event-dispatcher": "~2.7|~3.0",
"symfony/http-foundation": "~2.7|~3.0",
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
3.0.0
-----

* added argument resolvers
* deprecated `Symfony\Component\HttpKernel\Controller\ControllerResolver#getArguments()` and `doGetArguments()`, use argument resolvers instead
* removed `Symfony\Component\HttpKernel\Kernel::init()`
* removed `Symfony\Component\HttpKernel\Kernel::isClassInActiveBundle()` and `Symfony\Component\HttpKernel\KernelInterface::isClassInActiveBundle()`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\ArgumentResolver;

use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Resolves arguments typehinting for the Request object.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class RequestArgumentResolver implements ArgumentResolverInterface
{
/**
* {@inheritdoc}
*/
public function supports(Request $request, \ReflectionParameter $parameter)
{
$class = $parameter->getClass();

return $class && $class->isInstance($request);
}

/**
* {@inheritdoc}
*/
public function resolve(Request $request, \ReflectionParameter $parameter)
{
return $request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\ArgumentResolver;

use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Resolves arguments which names are equal to the name of a request attribute.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class RequestAttributesArgumentResolver implements ArgumentResolverInterface
{
/**
* {@inheritdoc}
*/
public function supports(Request $request, \ReflectionParameter $parameter)
{
return $request->attributes->has($parameter->name);
}

/**
* {@inheritdoc}
*/
public function resolve(Request $request, \ReflectionParameter $parameter)
{
return $request->attributes->get($parameter->name);
}
}
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\HttpFoundation\Request;

/**
* An ArgumentResolverInterface implementation resolves the arguments of
* controllers.
*
* @author Wouter J <wouter@wouterj.nl>
*/
interface ArgumentResolverInterface
{
/**
* Checks if the current parameter can be resolved by this argument
* resolver.
*
* @param Request $request
* @param \ReflectionParameter $parameter
*
* @return bool
*/
public function supports(Request $request, \ReflectionParameter $parameter);

/**
* Resolves the current parameter into an argument.
*
* @param Request $request
* @param \ReflectionParameter $parameter
*
* @return mixed The resolved argument
*/
public function resolve(Request $request, \ReflectionParameter $parameter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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;

/**
* The ArgumentResolverManager chains over the registered argument resolvers to
* resolve all controller arguments.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class ArgumentResolverManager
{
/**
* @var ArgumentResolverInterface[]
*/
protected $resolvers = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private


/**
* Adds an argument resolver.
*
* @param ArgumentResolverInterface $resolver
*/
public function add(ArgumentResolverInterface $resolver)
{
$this->resolvers[] = $resolver;
}

/**
* 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 a parameter cannot be resolved
*/
public function getArguments(Request $request, $controller)
{
if (!is_callable($controller)) {
throw new \InvalidArgumentException(sprintf('Expected a callable as second parameter, got "%s".', is_object($controller) ? get_class($controller) : gettype($controller)));
}

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

$parameters = $controllerReflection->getParameters();
$arguments = array();

foreach ($parameters as $parameter) {
foreach ($this->resolvers as $argumentResolver) {
if ($argumentResolver->supports($request, $parameter)) {
$arguments[] = $argumentResolver->resolve($request, $parameter);
continue 2;
}
}

if ($parameter->isDefaultValueAvailable()) {
$arguments[] = $parameter->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 and none of the argument resolvers could resolve its value).', $repr, $parameter->name));
}
}

return $arguments;
}
}
46 changes: 0 additions & 46 deletions src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,52 +86,6 @@ public function getController(Request $request)
return $callable;
}

/**
* {@inheritdoc}
*
* @api
*/
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)) {
$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;
}

/**
* Returns a callable for the given controller.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,4 @@ interface ControllerResolverInterface
* @api
*/
public function getController(Request $request);

/**
* 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
*
* @api
*/
public function getArguments(Request $request, $controller);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverManager;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -39,10 +40,11 @@ class ContainerAwareHttpKernel extends HttpKernel
* @param ContainerInterface $container A ContainerInterface instance
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
* @param RequestStack $requestStack A stack for master/sub requests
* @param ArgumentResolverManager $argumentResolver An ArgumentResolverManager instance
*/
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack = null)
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack = null, ArgumentResolverManager $argumentResolver = null)
{
parent::__construct($dispatcher, $controllerResolver, $requestStack);
parent::__construct($dispatcher, $controllerResolver, $requestStack, $argumentResolver);

$this->container = $container;

Expand Down
Loading