8000 decoupled global variables system in Twig from the Templating one · symfony/symfony@be5a208 · GitHub
[go: up one dir, main page]

Skip to content

Commit be5a208

Browse files
committed
decoupled global variables system in Twig from the Templating one
1 parent 861804b commit be5a208

File tree

5 files changed

+172
-4
lines changed

5 files changed

+172
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Security\Core\SecurityContext;
1919

2020
/**
21-
* GlobalVariables is the entry point for Symfony global variables in Twig templates.
21+
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
2222
*
2323
* @author Fabien Potencier <fabien@symfony.com>
2424
*/
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\HttpFoundation\Session\Session;
17+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19+
use Symfony\Component\Security\Core\SecurityContextInterface;
20+
21+
/**
22+
* Exposes some Symfony parameters and services as an "app" global variable.
23+
*
24+
* @author Fabien Potencier <fabien@symfony.com>
25+
*/
26+
class AppVariable
27+
{
28+
private $security;
29+
private $tokenStorage;
30+
private $requestStack;
31+
private $environment;
32+
private $debug;
33+
34+
/**
35+
* @deprecated since version 2.7, to be removed in 3.0.
36+
*/
37+
public function setSecurity(SecurityContextInterface $security)
38+
{
39+
$this->security = $security;
40+
}
41+
42+
public function setTokenStorage(TokenStorageInterface $tokenStorage)
43+
{
44+
$this->tokenStorage = $tokenStorage;
45+
}
46+
47+
public function setRequestStack(RequestStack $requestStack)
48+
{
49+
$this->requestStack = $requestStack;
50+
}
51+
52+
public function setEnvironment($environment)
53+
{
54+
$this->environment = $environment;
55+
}
56+
57+
public function setDebug($debug)
58+
{
59+
$this->debug = (bool) $debug;
60+
}
61+
62+
/**
63+
* Returns the security context service.
64+
*
65+
* @deprecated since version 2.6, to be removed in 3.0.
66+
*
67+
* @return SecurityContext|null The security context
68+
*/
69+
public function getSecurity()
70+
{
71+
trigger_error('The "app.security" variable is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
72+
73+
if (null === $this->security) {
74+
throw new \RuntimeException('The "app.security" variable is not available.');
75+
}
76+
77+
return $this->security;
78+
}
79+
80+
/**
81+
* Returns the current user.
82+
*
83+
* @return mixed
84+
*
85+
* @see TokenInterface::getUser()
86+
*/
87+
public function getUser()
88+
{
89+
if (null === $this->tokenStorage) {
90+
throw new \RuntimeException('The "app.user" variable is not available.');
91+
}
92+
93+
if (!$token = $this->tokenStorage->getToken()) {
94+
return;
95+
}
96+
97+
$user = $token->getUser();
98+
if (is_object($user)) {
99+
return $user;
100+
}
101+
}
102+
103+
/**
104+
* Returns the current request.
105+
*
106+
* @return Request|null The HTTP request object
107+
*/
108+
public function getRequest()
109+
{
110+
if (null === $this->requestStack) {
111+
throw new \RuntimeException('The "app.request" variable is not available.');
112+
}
113+
114+
return $this->requestStack->getCurrentRequest();
115+
}
116+
117+
/**
118+
* Returns the current session.
119+
*
120+
* @return Session|null The session
121+
*/
122+
public function getSession()
123+
{
124+
if (null === $this->requestStack) {
125+
throw new \RuntimeException('The "app.session" variable is not available.');
126+
}
127+
128+
if ($request = $this->getRequest()) {
129+
return $request->getSession();
130+
}
131+
}
132+
133+
/**
134+
* Returns the current app environment.
135+
*
136+
* @return string The current environment string (e.g 'dev')
137+
*/
138+
public function getEnvironment()
139+
{
140+
if (null === $this->environment) {
141+
throw new \RuntimeException('The "app.environment" variable is not available.');
142+
}
143+
144+
return $this->environment;
145+
}
146+
147+
/**
148+
* Returns the current app debug mode.
149+
*
150+
* @return bool The current debug mode
151+
*/
152+
public function getDebug()
153+
{
154+
if (null === $this->debug) {
155+
throw new \RuntimeException('The "app.debug" variable is not available.');
156+
}
157+
158+
return $this->debug;
159+
}
160+
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@
3434
<argument>%twig.options%</argument>
3535
<call method="addGlobal">
3636
<argument>app</argument>
37-
<argument type="service" id="templating.globals" />
37+
<argument type="service" id="twig.app_variable" />
3838
</call>
3939
</service>
4040

41+
<service id="twig.app_variable" class="Symfony\Bundle\TwigBundle\AppVariable" public="false">
42+
<call method="setEnvironment"><argument>%kernel.environment%</argument></call>
43+
<call method="setDebug"><argument>%kernel.debug%</argument></call>
44+
<call method="setSecurity"><argument type="service" id="security.context" on-invalid="ignore" /></call>
45+
<call method="setTokenStorage"><argument type="service" id="security.token_storage" on-invalid="ignore" /></call>
46+
<call method="setRequestStack"><argument type="service" id="request_stack" on-invalid="ignore" /></call>
47+
</service>
48+
4149
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
4250
<tag name="kernel.cache_warmer" />
4351
<argument type="service" id="service_container" />

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testLoadFullConfiguration($format)
9999
// Globals
100100
$calls = $container->getDefinition('twig')->getMethodCalls();
101101
$this->assertEquals('app', $calls[0][1][0], '->load() registers services as Twig globals');
102-
$this->assertEquals(new Reference('templating.globals'), $calls[0][1][1]);
102+
$this->assertEquals(new Reference('twig.app_variable'), $calls[0][1][1]);
103103
$this->assertEquals('foo', $calls[1][1][0], '->load() registers services as Twig globals');
104104
$this->assertEquals(new Reference('bar'), $calls[1][1][1], '->load() registers services as Twig globals');
105105
$this->assertEquals('baz', $calls[2][1][0], '->load() registers variables as Twig globals');

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/config": "~2.2|~3.0.0",
2929
"symfony/routing": "~2.1|~3.0.0",
3030
"symfony/templating": "~2.1|~3.0.0",
31-
"symfony/framework-bundle": "~2.1|~3.0.0"
31+
"symfony/framework-bundle": "~2.7|~3.0.0"
3232
},
3333
"autoload": {
3434
"psr-0": { "Symfony\\Bundle\\TwigBundle\\": "" }

0 commit comments

Comments
 (0)
0