GlobalVariables.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Templating;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\Security\Core\SecurityContext;
  15. /**
  16. * GlobalVariables is the entry point for Symfony global variables in PHP templates.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class GlobalVariables
  21. {
  22. protected $container;
  23. public function __construct(ContainerInterface $container)
  24. {
  25. $this->container = $container;
  26. }
  27. /**
  28. * Returns the security context service.
  29. *
  30. * @deprecated since version 2.6, to be removed in 3.0.
  31. *
  32. * @return SecurityContext|null The security context
  33. */
  34. public function getSecurity()
  35. {
  36. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
  37. if ($this->container->has('security.context')) {
  38. return $this->container->get('security.context');
  39. }
  40. }
  41. /**
  42. * Returns the current user.
  43. *
  44. * @return mixed
  45. *
  46. * @see TokenInterface::getUser()
  47. */
  48. public function getUser()
  49. {
  50. if (!$this->container->has('security.token_storage')) {
  51. return;
  52. }
  53. $tokenStorage = $this->container->get('security.token_storage');
  54. if (!$token = $tokenStorage->getToken()) {
  55. return;
  56. }
  57. $user = $token->getUser();
  58. if (!\is_object($user)) {
  59. return;
  60. }
  61. return $user;
  62. }
  63. /**
  64. * Returns the current request.
  65. *
  66. * @return Request|null The HTTP request object
  67. */
  68. public function getRequest()
  69. {
  70. if ($this->container->has('request_stack')) {
  71. return $this->container->get('request_stack')->getCurrentRequest();
  72. }
  73. }
  74. /**
  75. * Returns the current session.
  76. *
  77. * @return Session|null The session
  78. */
  79. public function getSession()
  80. {
  81. if ($request = $this->getRequest()) {
  82. return $request->getSession();
  83. }
  84. }
  85. /**
  86. * Returns the current app environment.
  87. *
  88. * @return string The current environment string (e.g 'dev')
  89. */
  90. public function getEnvironment()
  91. {
  92. return $this->container->getParameter('kernel.environment');
  93. }
  94. /**
  95. * Returns the current app debug mode.
  96. *
  97. * @return bool The current debug mode
  98. */
  99. public function getDebug()
  100. {
  101. return (bool) $this->container->getParameter('kernel.debug');
  102. }
  103. }