Package.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Context\NullContext;
  13. use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
  14. /**
  15. * Basic package that adds a version to asset URLs.
  16. *
  17. * @author Kris Wallsmith <kris@symfony.com>
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class Package implements PackageInterface
  21. {
  22. private $versionStrategy;
  23. private $context;
  24. public function __construct(VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
  25. {
  26. $this->versionStrategy = $versionStrategy;
  27. $this->context = $context ?: new NullContext();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getVersion(string $path)
  33. {
  34. return $this->versionStrategy->getVersion($path);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getUrl(string $path)
  40. {
  41. if ($this->isAbsoluteUrl($path)) {
  42. return $path;
  43. }
  44. return $this->versionStrategy->applyVersion($path);
  45. }
  46. /**
  47. * @return ContextInterface
  48. */
  49. protected function getContext()
  50. {
  51. return $this->context;
  52. }
  53. /**
  54. * @return VersionStrategyInterface
  55. */
  56. protected function getVersionStrategy()
  57. {
  58. return $this->versionStrategy;
  59. }
  60. /**
  61. * @return bool
  62. */
  63. protected function isAbsoluteUrl(string $url)
  64. {
  65. return false !== strpos($url, '://') || '//' === substr($url, 0, 2);
  66. }
  67. }