[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> HttpFoundationExtension.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\Extension;
  13  
  14  use Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\HttpFoundation\RequestStack;
  16  use Symfony\Component\Routing\RequestContext;
  17  use Twig\Extension\AbstractExtension;
  18  use Twig\TwigFunction;
  19  
  20  /**
  21   * Twig extension for the Symfony HttpFoundation component.
  22   *
  23   * @author Fabien Potencier <fabien@symfony.com>
  24   */
  25  class HttpFoundationExtension extends AbstractExtension
  26  {
  27      private $requestStack;
  28      private $requestContext;
  29  
  30      public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
  31      {
  32          $this->requestStack = $requestStack;
  33          $this->requestContext = $requestContext;
  34      }
  35  
  36      /**
  37       * {@inheritdoc}
  38       */
  39      public function getFunctions()
  40      {
  41          return [
  42              new TwigFunction('absolute_url', [$this, 'generateAbsoluteUrl']),
  43              new TwigFunction('relative_path', [$this, 'generateRelativePath']),
  44          ];
  45      }
  46  
  47      /**
  48       * Returns the absolute URL for the given absolute or relative path.
  49       *
  50       * This method returns the path unchanged if no request is available.
  51       *
  52       * @param string $path The path
  53       *
  54       * @return string The absolute URL
  55       *
  56       * @see Request::getUriForPath()
  57       */
  58      public function generateAbsoluteUrl($path)
  59      {
  60          if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
  61              return $path;
  62          }
  63  
  64          if (!$request = $this->requestStack->getMasterRequest()) {
  65              if (null !== $this->requestContext && '' !== $host = $this->requestContext->getHost()) {
  66                  $scheme = $this->requestContext->getScheme();
  67                  $port = '';
  68  
  69                  if ('http' === $scheme && 80 != $this->requestContext->getHttpPort()) {
  70                      $port = ':'.$this->requestContext->getHttpPort();
  71                  } elseif ('https' === $scheme && 443 != $this->requestContext->getHttpsPort()) {
  72                      $port = ':'.$this->requestContext->getHttpsPort();
  73                  }
  74  
  75                  if ('#' === $path[0]) {
  76                      $queryString = $this->requestContext->getQueryString();
  77                      $path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
  78                  } elseif ('?' === $path[0]) {
  79                      $path = $this->requestContext->getPathInfo().$path;
  80                  }
  81  
  82                  if ('/' !== $path[0]) {
  83                      $path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
  84                  }
  85  
  86                  return $scheme.'://'.$host.$port.$path;
  87              }
  88  
  89              return $path;
  90          }
  91  
  92          if ('#' === $path[0]) {
  93              $path = $request->getRequestUri().$path;
  94          } elseif ('?' === $path[0]) {
  95              $path = $request->getPathInfo().$path;
  96          }
  97  
  98          if (!$path || '/' !== $path[0]) {
  99              $prefix = $request->getPathInfo();
 100              $last = \strlen($prefix) - 1;
 101              if ($last !== $pos = strrpos($prefix, '/')) {
 102                  $prefix = substr($prefix, 0, $pos).'/';
 103              }
 104  
 105              return $request->getUriForPath($prefix.$path);
 106          }
 107  
 108          return $request->getSchemeAndHttpHost().$path;
 109      }
 110  
 111      /**
 112       * Returns a relative path based on the current Request.
 113       *
 114       * This method returns the path unchanged if no request is available.
 115       *
 116       * @param string $path The path
 117       *
 118       * @return string The relative path
 119       *
 120       * @see Request::getRelativeUriForPath()
 121       */
 122      public function generateRelativePath($path)
 123      {
 124          if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
 125              return $path;
 126          }
 127  
 128          if (!$request = $this->requestStack->getMasterRequest()) {
 129              return $path;
 130          }
 131  
 132          return $request->getRelativeUriForPath($path);
 133      }
 134  
 135      /**
 136       * Returns the name of the extension.
 137       *
 138       * @return string The extension name
 139       */
 140      public function getName()
 141      {
 142          return 'request';
 143      }
 144  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1