vendor/symfony/asset/Packages.php line 58

  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\Asset\Exception\LogicException;
  13. /**
  14.  * Helps manage asset URLs.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  * @author Kris Wallsmith <kris@symfony.com>
  18.  */
  19. class Packages
  20. {
  21.     private ?PackageInterface $defaultPackage;
  22.     private array $packages = [];
  23.     /**
  24.      * @param PackageInterface[] $packages Additional packages indexed by name
  25.      */
  26.     public function __construct(PackageInterface $defaultPackage nulliterable $packages = [])
  27.     {
  28.         $this->defaultPackage $defaultPackage;
  29.         foreach ($packages as $name => $package) {
  30.             $this->addPackage($name$package);
  31.         }
  32.     }
  33.     public function setDefaultPackage(PackageInterface $defaultPackage)
  34.     {
  35.         $this->defaultPackage $defaultPackage;
  36.     }
  37.     public function addPackage(string $namePackageInterface $package)
  38.     {
  39.         $this->packages[$name] = $package;
  40.     }
  41.     /**
  42.      * Returns an asset package.
  43.      *
  44.      * @param string|null $name The name of the package or null for the default package
  45.      *
  46.      * @throws InvalidArgumentException If there is no package by that name
  47.      * @throws LogicException           If no default package is defined
  48.      */
  49.     public function getPackage(string $name null): PackageInterface
  50.     {
  51.         if (null === $name) {
  52.             if (null === $this->defaultPackage) {
  53.                 throw new LogicException('There is no default asset package, configure one first.');
  54.             }
  55.             return $this->defaultPackage;
  56.         }
  57.         if (!isset($this->packages[$name])) {
  58.             throw new InvalidArgumentException(sprintf('There is no "%s" asset package.'$name));
  59.         }
  60.         return $this->packages[$name];
  61.     }
  62.     /**
  63.      * Gets the version to add to public URL.
  64.      *
  65.      * @param string      $path        A public path
  66.      * @param string|null $packageName A package name
  67.      */
  68.     public function getVersion(string $pathstring $packageName null): string
  69.     {
  70.         return $this->getPackage($packageName)->getVersion($path);
  71.     }
  72.     /**
  73.      * Returns the public path.
  74.      *
  75.      * Absolute paths (i.e. http://...) are returned unmodified.
  76.      *
  77.      * @param string      $path        A public path
  78.      * @param string|null $packageName The name of the asset package to use
  79.      *
  80.      * @return string A public path which takes into account the base path and URL path
  81.      */
  82.     public function getUrl(string $pathstring $packageName null): string
  83.     {
  84.         return $this->getPackage($packageName)->getUrl($path);
  85.     }
  86. }