[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/ -> AppVariable.php (source)

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


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1