AssetReference.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Asset;
  11. use Assetic\AssetManager;
  12. use Assetic\Filter\FilterInterface;
  13. /**
  14. * A reference to an asset in the asset manager.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class AssetReference implements AssetInterface
  19. {
  20. private $am;
  21. private $name;
  22. private $filters = array();
  23. public function __construct(AssetManager $am, $name)
  24. {
  25. $this->am = $am;
  26. $this->name = $name;
  27. }
  28. public function ensureFilter(FilterInterface $filter)
  29. {
  30. $this->filters[] = $filter;
  31. }
  32. public function getFilters()
  33. {
  34. $this->flushFilters();
  35. return $this->callAsset(__FUNCTION__);
  36. }
  37. public function clearFilters()
  38. {
  39. $this->filters = array();
  40. $this->callAsset(__FUNCTION__);
  41. }
  42. public function load(FilterInterface $additionalFilter = null)
  43. {
  44. $this->flushFilters();
  45. return $this->callAsset(__FUNCTION__, array($additionalFilter));
  46. }
  47. public function dump(FilterInterface $additionalFilter = null)
  48. {
  49. $this->flushFilters();
  50. return $this->callAsset(__FUNCTION__, array($additionalFilter));
  51. }
  52. public function getContent()
  53. {
  54. return $this->callAsset(__FUNCTION__);
  55. }
  56. public function setContent($content)
  57. {
  58. $this->callAsset(__FUNCTION__, array($content));
  59. }
  60. public function getSourceRoot()
  61. {
  62. return $this->callAsset(__FUNCTION__);
  63. }
  64. public function getSourcePath()
  65. {
  66. return $this->callAsset(__FUNCTION__);
  67. }
  68. public function getTargetPath()
  69. {
  70. return $this->callAsset(__FUNCTION__);
  71. }
  72. public function setTargetPath($targetPath)
  73. {
  74. $this->callAsset(__FUNCTION__, array($targetPath));
  75. }
  76. public function getLastModified()
  77. {
  78. return $this->callAsset(__FUNCTION__);
  79. }
  80. // private
  81. private function callAsset($method, $arguments = array())
  82. {
  83. $asset = $this->am->get($this->name);
  84. return call_user_func_array(array($asset, $method), $arguments);
  85. }
  86. private function flushFilters()
  87. {
  88. $asset = $this->am->get($this->name);
  89. while ($filter = array_shift($this->filters)) {
  90. $asset->ensureFilter($filter);
  91. }
  92. }
  93. }