-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 all commits
54a0653
3dfdb61
434f92f
a1d66c6
cf69cc7
3a1d99c
1741b2a
4924e86
1300ed2
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,14 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="argument_resolver.psr_message" class="Symfony\Component\HttpKernel\Controller\ArgumentResolver\PsrServerRequestArgumentResolver"> | ||
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. I'm also not sure if the naming is chosen really well. Imagine that someone actually has packages under an 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. yeah. This services will never be accessed directly, so the service id can be less generic. 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. So what ID do you propose? Symfony's service IDs are all pretty generic... |
||
<tag name="kernel.argument_resolver" /> | ||
</service> | ||
|
||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
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 license header 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. added |
||
/* | ||
* 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\Bundle\SecurityBundle\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. Maybe this would be better suited in the Component? |
||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ArgumentResolverInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
||
/** | ||
* Resolves a typehint for UserInterface in a controller to the current user. | ||
* | ||
* @author Wouter J <wouter@wouterj.nl> | ||
*/ | ||
class UserArgumentResolver implements ArgumentResolverInterface | ||
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 think this can be confused if we want to resolve User by ParamConverter ? 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. Sorry, I'm not sure I understand what you try to say. 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. Hmm, do you mean using the Doctrine Param Converter? That's why I made this resolver only work with Btw, this may be a good reason to implement priority for argument resolvers. 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 I mean Doctrine Param Converter and implement priority sound good to me. |
||
{ | ||
/** | ||
* @var TokenStorageInterface | ||
*/ | ||
private $tokenStorage; | ||
|
||
public function __construct(TokenStorageInterface $tokenStorage) | ||
{ | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, \ReflectionParameter $parameter) | ||
{ | ||
$class = $parameter->getClass(); | ||
$userInterface = 'Symfony\Component\Security\Core\User\UserInterface'; | ||
$userImplementation = 'Symfony\Component\Security\Core\User\User'; | ||
|
||
return null !== $class && ($userInterface === $class->getName() || $userImplementation === $class->getName()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, \ReflectionParameter $parameter) | ||
{ | ||
if (null === $token = $this->tokenStorage->getToken()) { | ||
return; | ||
} | ||
|
||
if (!is_object($user = $token->getUser())) { | ||
// e.g. anonymous authentication | ||
return; | ||
} | ||
|
||
return $user; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\ArgumentResolver; | ||
|
||
use Symfony\Bundle\SecurityBundle\ArgumentResolver\UserArgumentResolver; | ||
use Symfony\Component\Security\Core\Role\Role; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* @author Wouter J <wouter@wouterj.nl> | ||
*/ | ||
class UserArgumentResolverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $resolver; | ||
private $tokenStorage; | ||
|
||
protected function setUp() | ||
{ | ||
$this->tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); | ||
$this->resolver = new UserArgumentResolver($this->tokenStorage); | ||
} | ||
|
||
/** | ||
* @dataProvider provideClasses | ||
*/ | ||
public function testSupports($class, $supported = true) | ||
{ | ||
$this->assertEquals($supported, $this->resolver->supports($this->getRequestMock(), $this->getReflectionParameterMock($class))); | ||
} | ||
|
||
public function provideClasses() | ||
{ | ||
return array( | ||
array('Symfony\Component\Security\Core\User\UserInterface'), | ||
array('Symfony\Component\Security\Core\User\User'), | ||
array('Symfony\Bundle\SecurityBundle\Tests\ArgumentResolver\UserFixture', false), | ||
array('\stdClass', false), | ||
); | ||
} | ||
|
||
public function testResolvesToNullWhenNoUserIsAvailable() | ||
{ | ||
$this->tokenStorage->expects($this->any())->method('getToken')->willReturn(null); | ||
|
||
$this->assertNull($this->resolver->resolve($this->getRequestMock(), $this->getReflectionParameterMock())); | ||
} | ||
|
||
public function testResolvesToNullWhenUserIsAnonymous() | ||
{ | ||
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); | ||
$token->expects($this->any())->method('getUser')->willReturn('anon.'); | ||
|
||
$this->tokenStorage->expects($this->any())->method('getToken')->willReturn($token); | ||
|
||
$this->assertNull($this->resolver->resolve($this->getRequestMock(), $this->getReflectionParameterMock())); | ||
} | ||
|
||
public function testResolvesToUser() | ||
{ | ||
$user = $this->getMock('Symfony\component\Security\Core\User\User'); | ||
|
||
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); | ||
$token->expects($this->any())->method('getUser')->willReturn($user); | ||
|
||
$this->tokenStorage->expects($this->any())->method('getToken')->willReturn($token); | ||
|
||
$this->assertEquals($user, $this->resolver->resolve($this->getRequestMock(), $this->getReflectionParameterMock())); | ||
} | ||
|
||
private function getRequestMock() | ||
{ | ||
return $this->getMock('Symfony\Component\HttpFoundation\Request'); | ||
} | ||
|
||
private function getReflectionParameterMock($class = null) | ||
{ | ||
$reflectionParameter = $this->getMockBuilder('\ReflectionParameter')->disableOriginalConstructor()->getMock(); | ||
|
||
if (null !== $class) { | ||
$reflectionClass = $this->getMockBuilder('\ReflectionClass')->disableOriginalConstructor()->getMock(); | ||
$reflectionClass->expects($this->any())->method('getName')->willReturn($class); | ||
$reflectionParameter->expects($this->any())->method('getClass')->willReturn($reflectionClass); | ||
} | ||
|
||
return $reflectionParameter; | ||
} | ||
} | ||
|
||
class UserFixture implements UserInterface | ||
{ | ||
public function getRoles() {} | ||
public function getPassword() {} | ||
public function getSalt() {} | ||
public function getUsername() {} | ||
public function eraseCredentials() {} | ||
} |
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\ArgumentResolver; | ||
|
||
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 pameter can be esolved 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,52 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Converts HttpFoundation Request to PSR-7 ServerRequest using the bridge. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
* @author Wouter J <wouter@wouterj.nl> | ||
*/ | ||
class PsrServerRequestArgumentResolver implements ArgumentResolverInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private static $supportedTypes = array( | ||
'Psr\Http\Message\ServerRequestInterface' => true, | ||
'Psr\Http\Message\RequestInterface' => true, | ||
'Psr\Http\Message\MessageInterface' => true, | ||
); | ||
|
||
/** | ||
* @var HttpMessageFactoryInterface | ||
*/ | ||
private $httpMessageFactory; | ||
|
||
public function __construct(HttpMessageFactoryInterface $httpMessageFactory) | ||
{ | ||
$this->httpMessageFactory = $httpMessageFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, \ReflectionParameter $parameter) | ||
{ | ||
$class = $parameter->getClass(); | ||
|
||
return null !== $class && isset(self::$supportedTypes[$class->getName()]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, \ReflectionParameter $parameter) | ||
{ | ||
return $this->httpMessageFactory->createRequest($request); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\HttpFoundation\Request; | ||
|
||
/** | ||
* Resolves arguments typehinting for the HttpFoundation 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; | ||
} | ||
} |
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.
there should also be a config setting to force disabling it in case you don't want to use the parameter converter, allowing to get rid of its overhead
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.
In that case, we should have a configure option for all resolvers imo (so one config option in SecurityBundle too). I'm not sure I like that.