HttpFoundationExtension.php 3.7 KB

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