AppVariable.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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($environment)
  36. {
  37. $this->environment = $environment;
  38. }
  39. public function setDebug($debug)
  40. {
  41. $this->debug = (bool) $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 mixed
  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;
  71. }
  72. $user = $token->getUser();
  73. if (is_object($user)) {
  74. return $user;
  75. }
  76. }
  77. /**
  78. * Returns the current request.
  79. *
  80. * @return Request|null The HTTP request object
  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 The session
  93. */
  94. public function getSession()
  95. {
  96. if (null === $this->requestStack) {
  97. throw new \RuntimeException('The "app.session" variable is not available.');
  98. }
  99. if ($request = $this->getRequest()) {
  100. return $request->getSession();
  101. }
  102. }
  103. /**
  104. * Returns the current app environment.
  105. *
  106. * @return string The current environment string (e.g 'dev')
  107. */
  108. public function getEnvironment()
  109. {
  110. if (null === $this->environment) {
  111. throw new \RuntimeException('The "app.environment" variable is not available.');
  112. }
  113. return $this->environment;
  114. }
  115. /**
  116. * Returns the current app debug mode.
  117. *
  118. * @return bool The current debug mode
  119. */
  120. public function getDebug()
  121. {
  122. if (null === $this->debug) {
  123. throw new \RuntimeException('The "app.debug" variable is not available.');
  124. }
  125. return $this->debug;
  126. }
  127. /**
  128. * Returns some or all the existing flash messages:
  129. * * getFlashes() returns all the flash messages
  130. * * getFlashes('notice') returns a simple array with flash messages of that type
  131. * * getFlashes(array('notice', 'error')) returns a nested array of type => messages.
  132. *
  133. * @return array
  134. */
  135. public function getFlashes($types = null)
  136. {
  137. // needed to avoid starting the session automatically when looking for flash messages
  138. try {
  139. $session = $this->getSession();
  140. if (null === $session || !$session->isStarted()) {
  141. return array();
  142. }
  143. } catch (\RuntimeException $e) {
  144. return array();
  145. }
  146. if (null === $types || '' === $types || array() === $types) {
  147. return $session->getFlashBag()->all();
  148. }
  149. if (is_string($types)) {
  150. return $session->getFlashBag()->get($types);
  151. }
  152. $result = array();
  153. foreach ($types as $type) {
  154. $result[$type] = $session->getFlashBag()->get($type);
  155. }
  156. return $result;
  157. }
  158. }