|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\TwigBundle; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 16 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 17 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 18 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 19 | +use Symfony\Component\Security\Core\SecurityContextInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Exposes some Symfony parameters and services as an "app" global variable. |
| 23 | + * |
| 24 | + * @author Fabien Potencier <fabien@symfony.com> |
| 25 | + */ |
| 26 | +class AppVariable |
| 27 | +{ |
| 28 | + private $security; |
| 29 | + private $tokenStorage; |
| 30 | + private $requestStack; |
| 31 | + private $environment; |
| 32 | + private $debug; |
| 33 | + |
| 34 | + /** |
| 35 | + * @deprecated since version 2.7, to be removed in 3.0. |
| 36 | + */ |
| 37 | + public function setSecurity(SecurityContextInterface $security) |
| 38 | + { |
| 39 | + $this->security = $security; |
| 40 | + } |
| 41 | + |
| 42 | + public function setTokenStorage(TokenStorageInterface $tokenStorage) |
| 43 | + { |
| 44 | + $this->tokenStorage = $tokenStorage; |
| 45 | + } |
| 46 | + |
| 47 | + public function setRequestStack(RequestStack $requestStack) |
| 48 | + { |
| 49 | + $this->requestStack = $requestStack; |
| 50 | + } |
| 51 | + |
| 52 | + public function setEnvironment($environment) |
| 53 | + { |
| 54 | + $this->environment = $environment; |
| 55 | + } |
| 56 | + |
| 57 | + public function setDebug($debug) |
| 58 | + { |
| 59 | + $this->debug = (bool) $debug; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the security context service. |
| 64 | + * |
| 65 | + * @deprecated since version 2.6, to be removed in 3.0. |
| 66 | + * |
| 67 | + * @return SecurityContext|null The security context |
| 68 | + */ |
| 69 | + public function getSecurity() |
| 70 | + { |
| 71 | + trigger_error('The "app.security" variable is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED); |
| 72 | + |
| 73 | + if (null === $this->security) { |
| 74 | + throw new \RuntimeException('The "app.security" variable is not available.'); |
| 75 | + } |
| 76 | + |
| 77 | + return $this->security; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the current user. |
| 82 | + * |
| 83 | + * @return mixed |
| 84 | + * |
| 85 | + * @see TokenInterface::getUser() |
| 86 | + */ |
| 87 | + public function getUser() |
| 88 | + { |
| 89 | + if (null === $this->tokenStorage) { |
| 90 | + throw new \RuntimeException('The "app.user" variable is not available.'); |
| 91 | + } |
| 92 | + |
| 93 | + if (!$token = $this->tokenStorage->getToken()) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $user = $token->getUser(); |
| 98 | + if (is_object($user)) { |
| 99 | + return $user; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the current request. |
| 105 | + * |
| 106 | + * @return Request|null The HTTP request object |
| 107 | + */ |
| 108 | + public function getRequest() |
| 109 | + { |
| 110 | + if (null === $this->requestStack) { |
| 111 | + throw new \RuntimeException('The "app.request" variable is not available.'); |
| 112 | + } |
| 113 | + |
| 114 | + return $this->requestStack->getCurrentRequest(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the current session. |
| 119 | + * |
| 120 | + * @return Session|null The session |
| 121 | + */ |
| 122 | + public function getSession() |
| 123 | + { |
| 124 | + if (null === $this->requestStack) { |
| 125 | + throw new \RuntimeException('The "app.session" variable is not available.'); |
| 126 | + } |
| 127 | + |
| 128 | + if ($request = $this->getRequest()) { |
| 129 | + return $request->getSession(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns the current app environment. |
| 135 | + * |
| 136 | + * @return string The current environment string (e.g 'dev') |
| 137 | + */ |
| 138 | + public function getEnvironment() |
| 139 | + { |
| 140 | + if (null === $this->environment) { |
| 141 | + throw new \RuntimeException('The "app.environment" variable is not available.'); |
| 142 | + } |
| 143 | + |
| 144 | + return $this->environment; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the current app debug mode. |
| 149 | + * |
| 150 | + * @return bool The current debug mode |
| 151 | + */ |
| 152 | + public function getDebug() |
| 153 | + { |
| 154 | + if (null === $this->debug) { |
| 155 | + throw new \RuntimeException('The "app.debug" variable is not available.'); |
| 156 | + } |
| 157 | + |
| 158 | + return $this->debug; |
| 159 | + } |
| 160 | +} |
0 commit comments