8000 [WIP] Created ArgumentResolverManager by wouterj · Pull Request #3 · wouterj/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Created ArgumentResolverManager #3

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

Merged
merged 3 commits into from
Feb 28, 2014
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpKernel\Controller;

use Symfony\Component\HttpFoundation\Request;

/**
* An ArgumentResolverInterface implementation resolves the arguments of
* controllers.
Expand All @@ -23,18 +25,20 @@ interface ArgumentResolverInterface
* Checks if the current parameter can be resolved by this argument
* resolver.
*
* @param Request $request
* @param \ReflectionParameter $parameter
*
* @return Boolean
*/
public function supports(\ReflectionParameter $parameter);
public function accepts(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(\ReflectionParameter $parameter);
public function resolve(Request $request, \ReflectionParameter $parameter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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 resolver to
* resolve all controller arguments.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class ArgumentResolverManager
{
/**
* @var ArgumentResolverInterface[]
*/
protected $resolvers = array();

/**
* 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_array($controller)) {
$controllerReflection = new \ReflectionMethod($controller[0], $controller[1]);
} elseif (is_object($controller) && !$controller instanceof \Closure) {
$controllerReflection = new \ReflectionObject($controller);
$controllerReflection = $r->getMethod('__invoke');
} else {
$controllerReflection = new \ReflectionFunction($controller);
}

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

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

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, $parameter->name));
}

return $arguments;
}

public function addResolver(ArgumentResolverInterface $resolver)
{
$this->resolvers[] = $resolver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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\Tests\Controller;

use Symfony\Component\HttpKernel\Controller\ArgumentResolverManager;

class ArgumentResolverManagerTest extends \PHPUnit_Framework_TestCase
{
protected $manager;
protected $resolver1;
protected $resolver2;
protected $request;

public function setUp()
{
$this->manager = new ArgumentResolverManager();
$this->resolver1 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
$this->resolver2 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
$this->manager->addResolver($this->resolver1);
$this->manager->addResolver($this->resolver2);

$this->request = $this->getMock('Symfony\Component\HttpFoundation\Request');
}

public function testGetArgumentsFirstResolverAccepts()
{
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(true));
$this->resolver1->expects($this->any())
->method('resolve')
->will($this->returnValue('resolved_value'));

$controller = $this->getControllerWithOneArgument();

$arguments = $this->manager->getArguments($this->request, $controller);
$this->assertEquals(array('resolved_value'), $arguments);
}

public function testGetArgumentsSecondResolverAccepts()
{
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false));
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(true));
$this->resolver2->expects($this->any())
->method('resolve')
->will($this->returnValue('resolved_value'));

$controller = $this->getControllerWithOneArgument();

$arguments = $this->manager->getArguments($this->request, $controller);
$this->assertEquals(array('resolved_value'), $arguments);
}

/**
* @expectedException RuntimeException
*/
public function testGetArgumentsFailsIfNoResolverAccepts()
{
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false));
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(false));

$controller = $this->getControllerWithOneArgument();
$this->manager->getArguments($this->request, $controller);
}

public function testGetArgumentResolvingMultipleArguments()
{
$this->resolver1->expects($this->any())
->method('accepts')
->will($this->onConsecutiveCalls(false, true, true));
$this->resolver1->expects($this->any())
->method('resolve')
->will($this->onConsecutiveCalls('1st resolved by 1', '2nd resolved by 1'));

$this->resolver2->expects($this->any())
->method('accepts')
->will($this->onConsecutiveCalls(true, false, true));
$this->resolver2->expects($this->any())
->method('resolve')
->will($this->onConsecutiveCalls('1st resolved by 2', '2nd resolved by 2'));

$controller = function ($a, $b, $c) { };

$arguments = $this->manager->getArguments($this->request, $controller);
$this->assertEquals(array('1st resolved by 2', '1st resolved by 1', '2nd resolved by 1'), $arguments);
}

protected function getControllerWithOneArgument()
{
return function ($a) { };
}
}
0