Skip to content

Commit

Permalink
[TwigBundle] adding two global variables : environment & debug + some…
Browse files Browse the repository at this point in the history
… doc blocks

Use case :
    {% if app.environment == 'prod' %}
        {# e.g google analytics scripts #}
    {% endif %}
  • Loading branch information
benjamindulau authored and fabpot committed Feb 19, 2011
1 parent 36132cc commit f1dd3f2
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Symfony/Bundle/TwigBundle/GlobalVariables.php
Expand Up @@ -27,13 +27,24 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

/**
* Returns security context service
*
* @return Symfony\Component\Security\Core\SecurityContext|void The security context
*/
public function getSecurity()
{
if ($this->container->has('security.context')) {
return $this->container->get('security.context');
}
}

/**
* Returns current user
*
* @return mixed|void
* @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()
*/
public function getUser()
{
if (!$security = $this->getSecurity()) {
Expand All @@ -52,17 +63,51 @@ public function getUser()
return $user;
}

/**
* Returns security context service
*
* @return Symfony\Component\HttpFoundation\Request|void The http request object
*/
public function getRequest()
{
if ($this->container->has('request') && $request = $this->container->get('request')) {
return $request;
}
}

/**
* Returns security context service
*
* @return Symfony\Component\HttpFoundation\Session|void The session
*/
public function getSession()
{
if ($request = $this->getRequest()) {
return $request->getSession();
}
}

/**
* Returns current app environment
*
* @return string|void The current environment string (e.g 'dev')
*/
public function getEnvironment()
{
if ($this->container->hasParameter('kernel.environment')) {
return $this->container->getParameter('kernel.environment');
}
}

/**
* Returns current app debug mode
*
* @return boolean|void The current debug mode
*/
public function getDebug()
{
if ($this->container->hasParameter('kernel.debug')) {
return (bool)$this->container->getParameter('kernel.debug');
}
}
}

0 comments on commit f1dd3f2

Please sign in to comment.