[ Index ]

PHP Cross Reference of phpBB-3.2.11-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\DependencyInjection\ContainerInterface;
  15  use Symfony\Component\HttpFoundation\Request;
  16  use Symfony\Component\HttpFoundation\RequestStack;
  17  use Symfony\Component\HttpFoundation\Session\Session;
  18  use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19  use Symfony\Component\Security\Core\SecurityContext;
  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 $container;
  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 setContainer(ContainerInterface $container)
  38      {
  39          $this->container = $container;
  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 Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
  72  
  73          if (null === $this->container) {
  74              throw new \RuntimeException('The "app.security" variable is not available.');
  75          }
  76  
  77          if ($this->container->has('security.context')) {
  78              return $this->container->get('security.context');
  79          }
  80      }
  81  
  82      /**
  83       * Returns the current user.
  84       *
  85       * @return mixed
  86       *
  87       * @see TokenInterface::getUser()
  88       */
  89      public function getUser()
  90      {
  91          if (null === $this->tokenStorage) {
  92              if (null === $this->container) {
  93                  throw new \RuntimeException('The "app.user" variable is not available.');
  94              } elseif (!$this->container->has('security.context')) {
  95                  return;
  96              }
  97  
  98              $this->tokenStorage = $this->container->get('security.context');
  99          }
 100  
 101          if (!$token = $this->tokenStorage->getToken()) {
 102              return;
 103          }
 104  
 105          $user = $token->getUser();
 106          if (\is_object($user)) {
 107              return $user;
 108          }
 109      }
 110  
 111      /**
 112       * Returns the current request.
 113       *
 114       * @return Request|null The HTTP request object
 115       */
 116      public function getRequest()
 117      {
 118          if (null === $this->requestStack) {
 119              if (null === $this->container) {
 120                  throw new \RuntimeException('The "app.request" variable is not available.');
 121              }
 122  
 123              $this->requestStack = $this->container->get('request_stack');
 124          }
 125  
 126          return $this->requestStack->getCurrentRequest();
 127      }
 128  
 129      /**
 130       * Returns the current session.
 131       *
 132       * @return Session|null The session
 133       */
 134      public function getSession()
 135      {
 136          if (null === $this->requestStack && null === $this->container) {
 137              throw new \RuntimeException('The "app.session" variable is not available.');
 138          }
 139  
 140          if ($request = $this->getRequest()) {
 141              return $request->getSession();
 142          }
 143      }
 144  
 145      /**
 146       * Returns the current app environment.
 147       *
 148       * @return string The current environment string (e.g 'dev')
 149       */
 150      public function getEnvironment()
 151      {
 152          if (null === $this->environment) {
 153              throw new \RuntimeException('The "app.environment" variable is not available.');
 154          }
 155  
 156          return $this->environment;
 157      }
 158  
 159      /**
 160       * Returns the current app debug mode.
 161       *
 162       * @return bool The current debug mode
 163       */
 164      public function getDebug()
 165      {
 166          if (null === $this->debug) {
 167              throw new \RuntimeException('The "app.debug" variable is not available.');
 168          }
 169  
 170          return $this->debug;
 171      }
 172  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1