Packages.php 3.2 KB

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