diff --git a/src/Symfony/Bridge/Twig/AppVariable.php b/src/Symfony/Bridge/Twig/AppVariable.php index 6657abf27c..02fe5e5b68 100644 --- a/src/Symfony/Bridge/Twig/AppVariable.php +++ b/src/Symfony/Bridge/Twig/AppVariable.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; /** * Exposes some Symfony parameters and services as an "app" global variable. @@ -48,6 +49,22 @@ class AppVariable $this->debug = (bool) $debug; } + /** + * Returns the current token. + * + * @return TokenInterface|null + * + * @throws \RuntimeException When the TokenStorage is not available + */ + public function getToken() + { + if (null === $tokenStorage = $this->tokenStorage) { + throw new \RuntimeException('The "app.token" variable is not available.'); + } + + return $tokenStorage->getToken(); + } + /** * Returns the current user. * @@ -57,9 +74,7 @@ class AppVariable */ public function getUser() { - if (null !== $this->tokenStorage) { - $tokenStorage = $this->tokenStorage; - } else { + if (null === $tokenStorage = $this->tokenStorage) { throw new \RuntimeException('The "app.user" variable is not available.'); } diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index b4df596fa2..d8b7698fce 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.2.0 +----- + + * added `AppVariable::getToken()` + 2.7.0 ----- diff --git a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php index 51a95bc4b5..c54ad28ea8 100644 --- a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php +++ b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php @@ -67,6 +67,17 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase $this->assertEquals($request, $this->appVariable->getRequest()); } + public function testGetToken() + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); + $this->appVariable->setTokenStorage($tokenStorage); + + $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $tokenStorage->method('getToken')->willReturn($token); + + $this->assertEquals($token, $this->appVariable->getToken()); + } + public function testGetUser() { $this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface')); @@ -81,6 +92,14 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase $this->assertNull($this->appVariable->getUser()); } + public function testGetTokenWithNoToken() + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); + $this->appVariable->setTokenStorage($tokenStorage); + + $this->assertNull($this->appVariable->getToken()); + } + public function testGetUserWithNoToken() { $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); @@ -105,6 +124,14 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase $this->appVariable->getDebug(); } + /** + * @expectedException \RuntimeException + */ + public function testGetTokenWithTokenStorageNotSet() + { + $this->appVariable->getToken(); + } + /** * @expectedException \RuntimeException */