vendor/symfony/asset/Package.php line 29

  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 VersionStrategyInterface $versionStrategy;
  23.     private ContextInterface $context;
  24.     public function __construct(VersionStrategyInterface $versionStrategyContextInterface $context null)
  25.     {
  26.         $this->versionStrategy $versionStrategy;
  27.         $this->context $context ?? new NullContext();
  28.     }
  29.     public function getVersion(string $path): string
  30.     {
  31.         return $this->versionStrategy->getVersion($path);
  32.     }
  33.     public function getUrl(string $path): string
  34.     {
  35.         if ($this->isAbsoluteUrl($path)) {
  36.             return $path;
  37.         }
  38.         return $this->versionStrategy->applyVersion($path);
  39.     }
  40.     protected function getContext(): ContextInterface
  41.     {
  42.         return $this->context;
  43.     }
  44.     protected function getVersionStrategy(): VersionStrategyInterface
  45.     {
  46.         return $this->versionStrategy;
  47.     }
  48.     protected function isAbsoluteUrl(string $url): bool
  49.     {
  50.         return str_contains($url'://') || str_starts_with($url'//');
  51.     }
  52. }