Packages.php 2.9 KB

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