vendor/symfony/twig-bridge/AppVariable.php line 168

Open in your IDE?
  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\Bridge\Twig;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. /**
  18. * Exposes some Symfony parameters and services as an "app" global variable.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class AppVariable
  23. {
  24. private $tokenStorage;
  25. private $requestStack;
  26. private $environment;
  27. private $debug;
  28. public function setTokenStorage(TokenStorageInterface $tokenStorage)
  29. {
  30. $this->tokenStorage = $tokenStorage;
  31. }
  32. public function setRequestStack(RequestStack $requestStack)
  33. {
  34. $this->requestStack = $requestStack;
  35. }
  36. public function setEnvironment(string $environment)
  37. {
  38. $this->environment = $environment;
  39. }
  40. public function setDebug(bool $debug)
  41. {
  42. $this->debug = $debug;
  43. }
  44. /**
  45. * Returns the current token.
  46. *
  47. * @return TokenInterface|null
  48. *
  49. * @throws \RuntimeException When the TokenStorage is not available
  50. */
  51. public function getToken()
  52. {
  53. if (null === $tokenStorage = $this->tokenStorage) {
  54. throw new \RuntimeException('The "app.token" variable is not available.');
  55. }
  56. return $tokenStorage->getToken();
  57. }
  58. /**
  59. * Returns the current user.
  60. *
  61. * @return UserInterface|null
  62. *
  63. * @see TokenInterface::getUser()
  64. */
  65. public function getUser()
  66. {
  67. if (null === $tokenStorage = $this->tokenStorage) {
  68. throw new \RuntimeException('The "app.user" variable is not available.');
  69. }
  70. if (!$token = $tokenStorage->getToken()) {
  71. return null;
  72. }
  73. $user = $token->getUser();
  74. // @deprecated since Symfony 5.4, $user will always be a UserInterface instance
  75. return \is_object($user) ? $user : null;
  76. }
  77. /**
  78. * Returns the current request.
  79. *
  80. * @return Request|null
  81. */
  82. public function getRequest()
  83. {
  84. if (null === $this->requestStack) {
  85. throw new \RuntimeException('The "app.request" variable is not available.');
  86. }
  87. return $this->requestStack->getCurrentRequest();
  88. }
  89. /**
  90. * Returns the current session.
  91. *
  92. * @return Session|null
  93. */
  94. public function getSession()
  95. {
  96. if (null === $this->requestStack) {
  97. throw new \RuntimeException('The "app.session" variable is not available.');
  98. }
  99. $request = $this->getRequest();
  100. return $request && $request->hasSession() ? $request->getSession() : null;
  101. }
  102. /**
  103. * Returns the current app environment.
  104. *
  105. * @return string
  106. */
  107. public function getEnvironment()
  108. {
  109. if (null === $this->environment) {
  110. throw new \RuntimeException('The "app.environment" variable is not available.');
  111. }
  112. return $this->environment;
  113. }
  114. /**
  115. * Returns the current app debug mode.
  116. *
  117. * @return bool
  118. */
  119. public function getDebug()
  120. {
  121. if (null === $this->debug) {
  122. throw new \RuntimeException('The "app.debug" variable is not available.');
  123. }
  124. return $this->debug;
  125. }
  126. /**
  127. * Returns some or all the existing flash messages:
  128. * * getFlashes() returns all the flash messages
  129. * * getFlashes('notice') returns a simple array with flash messages of that type
  130. * * getFlashes(['notice', 'error']) returns a nested array of type => messages.
  131. *
  132. * @return array
  133. */
  134. public function getFlashes($types = null)
  135. {
  136. try {
  137. if (null === $session = $this->getSession()) {
  138. return [];
  139. }
  140. } catch (\RuntimeException $e) {
  141. return [];
  142. }
  143. if (null === $types || '' === $types || [] === $types) {
  144. return $session->getFlashBag()->all();
  145. }
  146. if (\is_string($types)) {
  147. return $session->getFlashBag()->get($types);
  148. }
  149. $result = [];
  150. foreach ($types as $type) {
  151. $result[$type] = $session->getFlashBag()->get($type);
  152. }
  153. return $result;
  154. }
  155. }