* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Bundle\TwigBundle; use Symfony\Component\DependencyInjection\ContainerInterface; /** * AppVariables is the entry point for Symfony global variables in Twig templates. * * @author Fabien Potencier */ class GlobalVariables { protected $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function getSecurity() { if ($this->container->has('security.context')) { return $this->container->get('security.context'); } } public function getUser() { if (!$security = $this->getSecurity()) { return; } if (!$token = $security->getToken()) { return; } $user = $token->getUser(); if (!is_object($user)) { return; } return $user; } public function getRequest() { if ($this->container->has('request') && $request = $this->container->get('request')) { return $request; } } public function getSession() { if ($request = $this->getRequest()) { return $request->getSession(); } } }