This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php

121 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Templating;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
/**
2011-03-11 21:39:47 +00:00
* GlobalVariables is the entry point for Symfony global variables in Twig templates.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class GlobalVariables
{
protected $container;
/**
* @param ContainerInterface $container The DI container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
2011-02-19 12:02:23 +00:00
* Returns the security context service.
*
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
* @return SecurityContext|null The security context
*/
public function getSecurity()
{
if ($this->container->has('security.context')) {
return $this->container->get('security.context');
}
}
/**
2011-02-19 12:02:23 +00:00
* Returns the current user.
*
* @return mixed
2011-02-19 12:02:23 +00:00
*
* @see TokenInterface::getUser()
*/
public function getUser()
{
if (!$this->container->has('security.token_storage')) {
return;
}
$tokenStorage = $this->container->get('security.token_storage');
if (!$token = $tokenStorage->getToken()) {
return;
}
$user = $token->getUser();
if (!is_object($user)) {
return;
}
return $user;
}
/**
2011-03-11 21:44:32 +00:00
* Returns the current request.
2011-02-19 12:02:23 +00:00
*
2013-12-28 08:32:39 +00:00
* @return Request|null The HTTP request object
*/
public function getRequest()
{
if ($this->container->has('request_stack')) {
return $this->container->get('request_stack')->getCurrentRequest();
}
}
/**
2011-03-11 21:44:32 +00:00
* Returns the current session.
2011-02-19 12:02:23 +00:00
*
* @return Session|null The session
*/
public function getSession()
{
if ($request = $this->getRequest()) {
return $request->getSession();
}
}
2011-02-19 12:02:23 +00:00
/**
2011-02-19 12:02:23 +00:00
* Returns the current app environment.
*
* @return string The current environment string (e.g 'dev')
*/
public function getEnvironment()
{
2011-02-19 12:02:23 +00:00
return $this->container->getParameter('kernel.environment');
}
2011-02-19 12:02:23 +00:00
/**
2011-02-19 12:02:23 +00:00
* Returns the current app debug mode.
*
2014-11-30 13:33:44 +00:00
* @return bool The current debug mode
*/
public function getDebug()
{
return (bool) $this->container->getParameter('kernel.debug');
}
}