Skip to content

Commit f1dd3f2

Browse files
benjamindulaufabpot
authored andcommitted
[TwigBundle] adding two global variables : environment & debug + some doc blocks
Use case : {% if app.environment == 'prod' %} {# e.g google analytics scripts #} {% endif %}
1 parent 36132cc commit f1dd3f2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Symfony/Bundle/TwigBundle/GlobalVariables.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ public function __construct(ContainerInterface $container)
2727
$this->container = $container;
2828
}
2929

30+
/**
31+
* Returns security context service
32+
*
33+
* @return Symfony\Component\Security\Core\SecurityContext|void The security context
34+
*/
3035
public function getSecurity()
3136
{
3237
if ($this->container->has('security.context')) {
3338
return $this->container->get('security.context');
3439
}
3540
}
3641

42+
/**
43+
* Returns current user
44+
*
45+
* @return mixed|void
46+
* @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()
47+
*/
3748
public function getUser()
3849
{
3950
if (!$security = $this->getSecurity()) {
@@ -52,17 +63,51 @@ public function getUser()
5263
return $user;
5364
}
5465

66+
/**
67+
* Returns security context service
68+
*
69+
* @return Symfony\Component\HttpFoundation\Request|void The http request object
70+
*/
5571
public function getRequest()
5672
{
5773
if ($this->container->has('request') && $request = $this->container->get('request')) {
5874
return $request;
5975
}
6076
}
6177

78+
/**
79+
* Returns security context service
80+
*
81+
* @return Symfony\Component\HttpFoundation\Session|void The session
82+
*/
6283
public function getSession()
6384
{
6485
if ($request = $this->getRequest()) {
6586
return $request->getSession();
6687
}
6788
}
89+
90+
/**
91+
* Returns current app environment
92+
*
93+
* @return string|void The current environment string (e.g 'dev')
94+
*/
95+
public function getEnvironment()
96+
{
97+
if ($this->container->hasParameter('kernel.environment')) {
98+
return $this->container->getParameter('kernel.environment');
99+
}
100+
}
101+
102+
/**
103+
* Returns current app debug mode
104+
*
105+
* @return boolean|void The current debug mode
106+
*/
107+
public function getDebug()
108+
{
109+
if ($this->container->hasParameter('kernel.debug')) {
110+
return (bool)$this->container->getParameter('kernel.debug');
111+
}
112+
}
68113
}

0 commit comments

Comments
 (0)