AssetManager.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  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 Assetic;
  11. use Assetic\Asset\AssetInterface;
  12. /**
  13. * Manages assets.
  14. *
  15. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  16. */
  17. class AssetManager
  18. {
  19. private $assets = array();
  20. /**
  21. * Gets an asset by name.
  22. *
  23. * @param string $name The asset name
  24. *
  25. * @return AssetInterface The asset
  26. *
  27. * @throws InvalidArgumentException If there is no asset by that name
  28. */
  29. public function get($name)
  30. {
  31. if (!isset($this->assets[$name])) {
  32. throw new \InvalidArgumentException(sprintf('There is no "%s" asset.', $name));
  33. }
  34. return $this->assets[$name];
  35. }
  36. /**
  37. * Checks if the current asset manager has a certain asset.
  38. *
  39. * @param string $name an asset name
  40. *
  41. * @return Boolean True if the asset has been set, false if not
  42. */
  43. public function has($name)
  44. {
  45. return isset($this->assets[$name]);
  46. }
  47. /**
  48. * Registers an asset to the current asset manager.
  49. *
  50. * @param string $name The asset name
  51. * @param AssetInterface $asset The asset
  52. */
  53. public function set($name, AssetInterface $asset)
  54. {
  55. if (!ctype_alnum(str_replace('_', '', $name))) {
  56. throw new \InvalidArgumentException(sprintf('The name "%s" is invalid.', $name));
  57. }
  58. $this->assets[$name] = $asset;
  59. }
  60. /**
  61. * Returns an array of asset names.
  62. *
  63. * @return array An array of asset names
  64. */
  65. public function getNames()
  66. {
  67. return array_keys($this->assets);
  68. }
  69. }