Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit1cde01d

Browse files
committed
feature#20773 [FrameworkBundle] Added GlobalVariables::getToken() (HeahDude)
This PR was merged into the 3.3-dev branch.Discussion----------[FrameworkBundle] Added GlobalVariables::getToken()| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |symfony/symfony-docs#7191 comments| License | MIT| Doc PR |symfony/symfony-docs#7191I propose this feature as bug fix in 3.2, since I don't use the PHP templating I forgot to add the method in the `FrameworkBundle`, to keep it align with the `TwigBridge` in#19991.Is this acceptable or should it go in master?Commits-------099b848 Added GlobalVariables::getToken()
2 parents95ba10d +099b848 commit1cde01d

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* Added`GlobalVariables::getToken()`
8+
49
3.2.0
510
-----
611

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Component\DependencyInjection\ContainerInterface;
1515
useSymfony\Component\HttpFoundation\Request;
1616
useSymfony\Component\HttpFoundation\Session\Session;
17+
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
1718

1819
/**
1920
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
@@ -33,21 +34,27 @@ 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-
publicfunctiongetUser()
41+
publicfunctiongetToken()
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+
* @see TokenInterface::getUser()
54+
*/
55+
publicfunctiongetUser()
56+
{
57+
if (!$token =$this->getToken()) {
5158
return;
5259
}
5360

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ protected function setUp()
2626
$this->globals =newGlobalVariables($this->container);
2727
}
2828

29+
publicfunctiontestGetTokenNoTokenStorage()
30+
{
31+
$this->assertNull($this->globals->getToken());
32+
}
33+
34+
publicfunctiontestGetTokenNoToken()
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+
publicfunctiontestGetToken()
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
publicfunctiontestGetUserNoTokenStorage()
3056
{
3157
$this->assertNull($this->globals->getUser());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp