Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69857e5

Browse filesBrowse files
committed
Added GlobalVariables::getToken()
1 parent 456e68b commit 69857e5
Copy full SHA for 69857e5

File tree

2 files changed

+42
-7
lines changed
Filter options

2 files changed

+42
-7
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php
+16-7Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Session\Session;
17+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1718

1819
/**
1920
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
@@ -33,21 +34,29 @@ public function __construct(ContainerInterface $container)
3334
}
3435

3536
/**
36-
* Returns the current user.
37-
*
38-
* @return mixed
37+
* Returns the current token.
3938
*
40-
* @see TokenInterface::getUser()
39+
* @return TokenInterface|null
4140
*/
42-
public function getUser()
41+
public function getToken()
4342
{
4443
if (!$this->container->has('security.token_storage')) {
4544
return;
4645
}
4746

48-
$tokenStorage = $this->container->get('security.token_storage');
47+
return $this->container->get('security.token_storage')->getToken();
48+
}
4949

50-
if (!$token = $tokenStorage->getToken()) {
50+
/**
51+
* Returns the current user.
52+
*
53+
* @return mixed
54+
*
55+
* @see TokenInterface::getUser()
56+
*/
57+
public function getUser()
58+
{
59+
if (!$token = $this->getToken()) {
5160
return;
5261
}
5362

‎src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ protected function setUp()
2626
$this->globals = new GlobalVariables($this->container);
2727
}
2828

29+
public function testGetTokenNoTokenStorage()
30+
{
31+
$this->assertNull($this->globals->getToken());
32+
}
33+
34+
public function testGetTokenNoToken()
35+
{
36+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
37+
$this->container->set('security.token_storage', $tokenStorage);
38+
$this->assertNull($this->globals->getToken());
39+
}
40+
41+
public function testGetToken()
42+
{
43+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
44+
45+
$this->container->set('security.token_storage', $tokenStorage);
46+
47+
$tokenStorage
48+
->expects($this->once())
49+
->method('getToken')
50+
->will($this->returnValue('token'));
51+
52+
$this->assertSame('token', $this->globals->getToken());
53+
}
54+
2955
public function testGetUserNoTokenStorage()
3056
{
3157
$this->assertNull($this->globals->getUser());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.