AppVariable.php 4.5 KB

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