CoreAssetsHelper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Templating\Helper;
  11. use Symfony\Component\Templating\Asset\PackageInterface;
  12. /**
  13. * CoreAssetsHelper helps manage asset URLs.
  14. *
  15. * Usage:
  16. *
  17. * <code>
  18. * <img src="<?php echo $view['assets']->getUrl('foo.png') ?>" />
  19. * </code>
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Kris Wallsmith <kris@symfony.com>
  23. */
  24. class CoreAssetsHelper extends Helper implements PackageInterface
  25. {
  26. protected $defaultPackage;
  27. protected $namedPackages;
  28. /**
  29. * Constructor.
  30. *
  31. * @param PackageInterface $defaultPackage The default package
  32. * @param array $namedPackages Additional packages indexed by name
  33. */
  34. public function __construct(PackageInterface $defaultPackage, array $namedPackages = array())
  35. {
  36. $this->defaultPackage = $defaultPackage;
  37. $this->namedPackages = array();
  38. foreach ($namedPackages as $name => $package) {
  39. $this->addPackage($name, $package);
  40. }
  41. }
  42. /**
  43. * Sets the default package.
  44. *
  45. * @param PackageInterface $defaultPackage The default package
  46. */
  47. public function setDefaultPackage(PackageInterface $defaultPackage)
  48. {
  49. $this->defaultPackage = $defaultPackage;
  50. }
  51. /**
  52. * Adds an asset package to the helper.
  53. *
  54. * @param string $name The package name
  55. * @param PackageInterface $package The package
  56. */
  57. public function addPackage($name, PackageInterface $package)
  58. {
  59. $this->namedPackages[$name] = $package;
  60. }
  61. /**
  62. * Returns an asset package.
  63. *
  64. * @param string $name The name of the package or null for the default package
  65. *
  66. * @return PackageInterface An asset package
  67. *
  68. * @throws InvalidArgumentException If there is no package by that name
  69. */
  70. public function getPackage($name = null)
  71. {
  72. if (null === $name) {
  73. return $this->defaultPackage;
  74. }
  75. if (!isset($this->namedPackages[$name])) {
  76. throw new \InvalidArgumentException(sprintf('There is no "%s" asset package.', $name));
  77. }
  78. return $this->namedPackages[$name];
  79. }
  80. /**
  81. * Gets the version to add to public URL.
  82. *
  83. * @param string $packageName A package name
  84. *
  85. * @return string The current version
  86. */
  87. public function getVersion($packageName = null)
  88. {
  89. return $this->getPackage($packageName)->getVersion();
  90. }
  91. /**
  92. * Returns the public path.
  93. *
  94. * Absolute paths (i.e. http://...) are returned unmodified.
  95. *
  96. * @param string $path A public path
  97. * @param string $packageName The name of the asset package to use
  98. *
  99. * @return string A public path which takes into account the base path and URL path
  100. */
  101. public function getUrl($path, $packageName = null)
  102. {
  103. return $this->getPackage($packageName)->getUrl($path);
  104. }
  105. /**
  106. * Returns the canonical name of this helper.
  107. *
  108. * @return string The canonical name
  109. */
  110. public function getName()
  111. {
  112. return 'assets';
  113. }
  114. }