BaseAsset.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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\Filter\FilterCollection;
  12. use Assetic\Filter\FilterInterface;
  13. /**
  14. * A base abstract asset.
  15. *
  16. * The methods load() and getLastModified() are left undefined, although a
  17. * reusable doLoad() method is available to child classes.
  18. *
  19. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  20. */
  21. abstract class BaseAsset implements AssetInterface
  22. {
  23. private $filters;
  24. private $sourceRoot;
  25. private $sourcePath;
  26. private $targetPath;
  27. private $content;
  28. private $loaded;
  29. /**
  30. * Constructor.
  31. *
  32. * @param array $filters Filters for the asset
  33. */
  34. public function __construct($filters = array(), $sourceRoot = null, $sourcePath = null)
  35. {
  36. $this->filters = new FilterCollection($filters);
  37. $this->sourceRoot = $sourceRoot;
  38. $this->sourcePath = $sourcePath;
  39. $this->loaded = false;
  40. }
  41. public function __clone()
  42. {
  43. $this->filters = clone $this->filters;
  44. }
  45. public function ensureFilter(FilterInterface $filter)
  46. {
  47. $this->filters->ensure($filter);
  48. }
  49. public function getFilters()
  50. {
  51. return $this->filters->all();
  52. }
  53. public function clearFilters()
  54. {
  55. $this->filters->clear();
  56. }
  57. /**
  58. * Encapsulates asset loading logic.
  59. *
  60. * @param string $content The asset content
  61. * @param FilterInterface $additionalFilter An additional filter
  62. */
  63. protected function doLoad($content, FilterInterface $additionalFilter = null)
  64. {
  65. $filter = clone $this->filters;
  66. if ($additionalFilter) {
  67. $filter->ensure($additionalFilter);
  68. }
  69. $asset = clone $this;
  70. $asset->setContent($content);
  71. $filter->filterLoad($asset);
  72. $this->content = $asset->getContent();
  73. $this->loaded = true;
  74. }
  75. public function dump(FilterInterface $additionalFilter = null)
  76. {
  77. if (!$this->loaded) {
  78. $this->load();
  79. }
  80. $filter = clone $this->filters;
  81. if ($additionalFilter) {
  82. $filter->ensure($additionalFilter);
  83. }
  84. $asset = clone $this;
  85. $filter->filterDump($asset);
  86. return $asset->getContent();
  87. }
  88. public function getContent()
  89. {
  90. return $this->content;
  91. }
  92. public function setContent($content)
  93. {
  94. $this->content = $content;
  95. }
  96. public function getSourceRoot()
  97. {
  98. return $this->sourceRoot;
  99. }
  100. public function getSourcePath()
  101. {
  102. return $this->sourcePath;
  103. }
  104. public function getTargetPath()
  105. {
  106. return $this->targetPath;
  107. }
  108. public function setTargetPath($targetPath)
  109. {
  110. $this->targetPath = $targetPath;
  111. }
  112. }