UrlPackage.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Component\Asset;
  11. use Symfony\Component\Asset\Context\ContextInterface;
  12. use Symfony\Component\Asset\Exception\InvalidArgumentException;
  13. use Symfony\Component\Asset\Exception\LogicException;
  14. use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
  15. /**
  16. * Package that adds a base URL to asset URLs in addition to a version.
  17. *
  18. * The package allows to use more than one base URLs in which case
  19. * it randomly chooses one for each asset; it also guarantees that
  20. * any given path will always use the same base URL to be nice with
  21. * HTTP caching mechanisms.
  22. *
  23. * When the request context is available, this package can choose the
  24. * best base URL to use based on the current request scheme:
  25. *
  26. * * For HTTP request, it chooses between all base URLs;
  27. * * For HTTPs requests, it chooses between HTTPs base URLs and relative protocol URLs
  28. * or falls back to any base URL if no secure ones are available.
  29. *
  30. * @author Fabien Potencier <fabien@symfony.com>
  31. */
  32. class UrlPackage extends Package
  33. {
  34. private $baseUrls = [];
  35. private $sslPackage;
  36. /**
  37. * @param string|string[] $baseUrls Base asset URLs
  38. */
  39. public function __construct($baseUrls, VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
  40. {
  41. parent::__construct($versionStrategy, $context);
  42. if (!\is_array($baseUrls)) {
  43. $baseUrls = (array) $baseUrls;
  44. }
  45. if (!$baseUrls) {
  46. throw new LogicException('You must provide at least one base URL.');
  47. }
  48. foreach ($baseUrls as $baseUrl) {
  49. $this->baseUrls[] = rtrim($baseUrl, '/');
  50. }
  51. $sslUrls = $this->getSslUrls($baseUrls);
  52. if ($sslUrls && $baseUrls !== $sslUrls) {
  53. $this->sslPackage = new self($sslUrls, $versionStrategy);
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getUrl(string $path)
  60. {
  61. if ($this->isAbsoluteUrl($path)) {
  62. return $path;
  63. }
  64. if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
  65. return $this->sslPackage->getUrl($path);
  66. }
  67. $url = $this->getVersionStrategy()->applyVersion($path);
  68. if ($this->isAbsoluteUrl($url)) {
  69. return $url;
  70. }
  71. if ($url && '/' != $url[0]) {
  72. $url = '/'.$url;
  73. }
  74. return $this->getBaseUrl($path).$url;
  75. }
  76. /**
  77. * Returns the base URL for a path.
  78. *
  79. * @return string The base URL
  80. */
  81. public function getBaseUrl(string $path)
  82. {
  83. if (1 === \count($this->baseUrls)) {
  84. return $this->baseUrls[0];
  85. }
  86. return $this->baseUrls[$this->chooseBaseUrl($path)];
  87. }
  88. /**
  89. * Determines which base URL to use for the given path.
  90. *
  91. * Override this method to change the default distribution strategy.
  92. * This method should always return the same base URL index for a given path.
  93. *
  94. * @return int The base URL index for the given path
  95. */
  96. protected function chooseBaseUrl(string $path)
  97. {
  98. return (int) fmod(hexdec(substr(hash('sha256', $path), 0, 10)), \count($this->baseUrls));
  99. }
  100. private function getSslUrls(array $urls)
  101. {
  102. $sslUrls = [];
  103. foreach ($urls as $url) {
  104. if ('https://' === substr($url, 0, 8) || '//' === substr($url, 0, 2)) {
  105. $sslUrls[] = $url;
  106. } elseif (null === parse_url($url, \PHP_URL_SCHEME)) {
  107. throw new InvalidArgumentException(sprintf('"%s" is not a valid URL.', $url));
  108. }
  109. }
  110. return $sslUrls;
  111. }
  112. }