vendor/symfony/asset/Packages.php line 105

Open in your IDE?
  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 $defaultPackage;
  22.     private $packages = [];
  23.     /**
  24.      * @param PackageInterface[] $packages Additional packages indexed by name
  25.      */
  26.     public function __construct(PackageInterface $defaultPackage null, array $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.     /**
  38.      * Adds a  package.
  39.      *
  40.      * @param string $name The package name
  41.      */
  42.     public function addPackage($namePackageInterface $package)
  43.     {
  44.         $this->packages[$name] = $package;
  45.     }
  46.     /**
  47.      * Returns an asset package.
  48.      *
  49.      * @param string $name The name of the package or null for the default package
  50.      *
  51.      * @return PackageInterface An asset package
  52.      *
  53.      * @throws InvalidArgumentException If there is no package by that name
  54.      * @throws LogicException           If no default package is defined
  55.      */
  56.     public function getPackage($name null)
  57.     {
  58.         if (null === $name) {
  59.             if (null === $this->defaultPackage) {
  60.                 throw new LogicException('There is no default asset package, configure one first.');
  61.             }
  62.             return $this->defaultPackage;
  63.         }
  64.         if (!isset($this->packages[$name])) {
  65.             throw new InvalidArgumentException(sprintf('There is no "%s" asset package.'$name));
  66.         }
  67.         return $this->packages[$name];
  68.     }
  69.     /**
  70.      * Gets the version to add to public URL.
  71.      *
  72.      * @param string $path        A public path
  73.      * @param string $packageName A package name
  74.      *
  75.      * @return string The current version
  76.      */
  77.     public function getVersion($path$packageName null)
  78.     {
  79.         return $this->getPackage($packageName)->getVersion($path);
  80.     }
  81.     /**
  82.      * Returns the public path.
  83.      *
  84.      * Absolute paths (i.e. http://...) are returned unmodified.
  85.      *
  86.      * @param string $path        A public path
  87.      * @param string $packageName The name of the asset package to use
  88.      *
  89.      * @return string A public path which takes into account the base path and URL path
  90.      */
  91.     public function getUrl($path$packageName null)
  92.     {
  93.         return $this->getPackage($packageName)->getUrl($path);
  94.     }
  95. }