2011-01-11 16:42:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-11 16:42:34 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-03-16 12:11:29 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2012-12-16 12:02:54 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
use Symfony\Component\Security\Core\SecurityContext;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2011-01-11 16:42:34 +00:00
|
|
|
/**
|
2011-03-11 21:39:47 +00:00
|
|
|
* GlobalVariables is the entry point for Symfony global variables in Twig templates.
|
2011-01-11 16:42:34 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-11 16:42:34 +00:00
|
|
|
*/
|
|
|
|
class GlobalVariables
|
|
|
|
{
|
|
|
|
protected $container;
|
|
|
|
|
2013-09-13 12:48:12 +01:00
|
|
|
/**
|
|
|
|
* @param ContainerInterface $container The DI container
|
|
|
|
*/
|
2011-01-11 16:42:34 +00:00
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
2011-02-19 12:02:23 +00:00
|
|
|
* Returns the security context service.
|
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @return SecurityContext|null The security context
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
2011-01-11 16:42:34 +00:00
|
|
|
public function getSecurity()
|
|
|
|
{
|
|
|
|
if ($this->container->has('security.context')) {
|
2011-01-12 16:26:46 +00:00
|
|
|
return $this->container->get('security.context');
|
2011-01-11 16:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
2011-02-19 12:02:23 +00:00
|
|
|
* Returns the current user.
|
|
|
|
*
|
2013-09-13 12:48:12 +01:00
|
|
|
* @return mixed
|
2011-02-19 12:02:23 +00:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @see TokenInterface::getUser()
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
2011-01-11 16:42:34 +00:00
|
|
|
public function getUser()
|
|
|
|
{
|
2011-02-11 00:07:59 +00:00
|
|
|
if (!$security = $this->getSecurity()) {
|
|
|
|
return;
|
2011-01-11 16:42:34 +00:00
|
|
|
}
|
2011-02-11 00:07:59 +00:00
|
|
|
|
|
|
|
if (!$token = $security->getToken()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $token->getUser();
|
|
|
|
if (!is_object($user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
2011-01-11 16:42:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
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
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
2011-01-11 16:42:34 +00:00
|
|
|
public function getRequest()
|
|
|
|
{
|
|
|
|
if ($this->container->has('request') && $request = $this->container->get('request')) {
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
2011-03-11 21:44:32 +00:00
|
|
|
* Returns the current session.
|
2011-02-19 12:02:23 +00:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @return Session|null The session
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
2011-01-11 16:42:34 +00:00
|
|
|
public function getSession()
|
|
|
|
{
|
|
|
|
if ($request = $this->getRequest()) {
|
|
|
|
return $request->getSession();
|
|
|
|
}
|
|
|
|
}
|
2011-02-19 12:02:23 +00:00
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
2011-02-19 12:02:23 +00:00
|
|
|
* Returns the current app environment.
|
|
|
|
*
|
|
|
|
* @return string The current environment string (e.g 'dev')
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
|
|
|
public function getEnvironment()
|
|
|
|
{
|
2011-02-19 12:02:23 +00:00
|
|
|
return $this->container->getParameter('kernel.environment');
|
2011-02-18 13:22:45 +00:00
|
|
|
}
|
2011-02-19 12:02:23 +00:00
|
|
|
|
2011-02-18 13:22:45 +00:00
|
|
|
/**
|
2011-02-19 12:02:23 +00:00
|
|
|
* Returns the current app debug mode.
|
|
|
|
*
|
|
|
|
* @return Boolean The current debug mode
|
2011-02-18 13:22:45 +00:00
|
|
|
*/
|
|
|
|
public function getDebug()
|
|
|
|
{
|
2011-04-27 06:01:12 +01:00
|
|
|
return (Boolean) $this->container->getParameter('kernel.debug');
|
2011-02-18 13:22:45 +00:00
|
|
|
}
|
2011-01-11 16:42:34 +00:00
|
|
|
}
|