AssetCollection.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 collection of assets.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class AssetCollection implements AssetInterface, \IteratorAggregate
  19. {
  20. private $assets;
  21. private $filters;
  22. private $sourceRoot;
  23. private $targetPath;
  24. private $content;
  25. private $clones;
  26. /**
  27. * Constructor.
  28. *
  29. * @param array $assets Assets for the current collection
  30. * @param array $filters Filters for the current collection
  31. * @param string $sourceRoot The root directory
  32. */
  33. public function __construct($assets = array(), $filters = array(), $sourceRoot = null)
  34. {
  35. $this->assets = array();
  36. foreach ($assets as $asset) {
  37. $this->add($asset);
  38. }
  39. $this->filters = new FilterCollection($filters);
  40. $this->sourceRoot = $sourceRoot;
  41. $this->clones = new \SplObjectStorage();
  42. }
  43. /**
  44. * Adds an asset to the current collection.
  45. *
  46. * @param AssetInterface $asset An asset
  47. */
  48. public function add(AssetInterface $asset)
  49. {
  50. $this->assets[] = $asset;
  51. }
  52. public function all()
  53. {
  54. return $this->assets;
  55. }
  56. public function ensureFilter(FilterInterface $filter)
  57. {
  58. $this->filters->ensure($filter);
  59. }
  60. public function getFilters()
  61. {
  62. return $this->filters->all();
  63. }
  64. public function clearFilters()
  65. {
  66. $this->filters->clear();
  67. }
  68. public function load(FilterInterface $additionalFilter = null)
  69. {
  70. // loop through leaves and load each asset
  71. $parts = array();
  72. foreach ($this as $asset) {
  73. $asset->load($additionalFilter);
  74. $parts[] = $asset->getContent();
  75. }
  76. $this->content = implode("\n", $parts);
  77. }
  78. public function dump(FilterInterface $additionalFilter = null)
  79. {
  80. // loop through leaves and dump each asset
  81. $parts = array();
  82. foreach ($this as $asset) {
  83. $parts[] = $asset->dump($additionalFilter);
  84. }
  85. return implode("\n", $parts);
  86. }
  87. public function getContent()
  88. {
  89. return $this->content;
  90. }
  91. public function setContent($content)
  92. {
  93. $this->content = $content;
  94. }
  95. public function getSourceRoot()
  96. {
  97. return $this->sourceRoot;
  98. }
  99. public function getSourcePath()
  100. {
  101. }
  102. public function getTargetPath()
  103. {
  104. return $this->targetPath;
  105. }
  106. public function setTargetPath($targetPath)
  107. {
  108. $this->targetPath = $targetPath;
  109. }
  110. /**
  111. * Returns the highest last-modified value of all assets in the current collection.
  112. *
  113. * @return integer|null A UNIX timestamp
  114. */
  115. public function getLastModified()
  116. {
  117. if (!count($this->assets)) {
  118. return;
  119. }
  120. $mapper = function (AssetInterface $asset)
  121. {
  122. return $asset->getLastModified();
  123. };
  124. return max(array_map($mapper, $this->assets));
  125. }
  126. /**
  127. * Returns an iterator for looping recursively over unique leaves.
  128. */
  129. public function getIterator()
  130. {
  131. return new \RecursiveIteratorIterator(new AssetCollectionFilterIterator(new AssetCollectionIterator($this, $this->clones)));
  132. }
  133. }
  134. /**
  135. * Asset collection filter iterator.
  136. *
  137. * The filter iterator is responsible for de-duplication of leaf assets based
  138. * on both strict equality and source URL.
  139. *
  140. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  141. * @access private
  142. */
  143. class AssetCollectionFilterIterator extends \RecursiveFilterIterator
  144. {
  145. private $visited;
  146. private $sources;
  147. /**
  148. * Constructor.
  149. *
  150. * @param AssetCollectionIterator $iterator The inner iterator
  151. * @param array $visited An array of visited asset objects
  152. * @param array $sources An array of visited source strings
  153. */
  154. public function __construct(AssetCollectionIterator $iterator, array $visited = array(), array $sources = array())
  155. {
  156. parent::__construct($iterator);
  157. $this->visited = $visited;
  158. $this->sources = $sources;
  159. }
  160. /**
  161. * Determines whether the current asset is a duplicate.
  162. *
  163. * De-duplication is performed based on either strict equality or by
  164. * matching sources.
  165. *
  166. * @return Boolean Returns true if we have not seen this asset yet
  167. */
  168. public function accept()
  169. {
  170. $asset = $this->getInnerIterator()->current(true);
  171. $duplicate = false;
  172. // check strict equality
  173. if (in_array($asset, $this->visited, true)) {
  174. $duplicate = true;
  175. } else {
  176. $this->visited[] = $asset;
  177. }
  178. // check source
  179. $sourceRoot = $asset->getSourceRoot();
  180. $sourcePath = $asset->getSourcePath();
  181. if ($sourceRoot && $sourcePath) {
  182. $source = $sourceRoot.'/'.$sourcePath;
  183. if (in_array($source, $this->sources)) {
  184. $duplicate = true;
  185. } else {
  186. $this->sources[] = $source;
  187. }
  188. }
  189. return !$duplicate;
  190. }
  191. /**
  192. * Passes visited objects and source URLs to the child iterator.
  193. */
  194. public function getChildren()
  195. {
  196. return new self($this->getInnerIterator()->getChildren(), $this->visited, $this->sources);
  197. }
  198. }
  199. /**
  200. * Iterates over an asset collection.
  201. *
  202. * The iterator is responsible for cascading filters and target URL patterns
  203. * from parent to child assets.
  204. *
  205. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  206. * @access private
  207. */
  208. class AssetCollectionIterator implements \RecursiveIterator
  209. {
  210. private $assets;
  211. private $filters;
  212. private $output;
  213. private $clones;
  214. public function __construct(AssetCollection $coll, \SplObjectStorage $clones)
  215. {
  216. $this->assets = $coll->all();
  217. $this->filters = $coll->getFilters();
  218. $this->output = $coll->getTargetPath();
  219. $this->clones = $clones;
  220. if (false === $pos = strpos($this->output, '.')) {
  221. $this->output .= '_*';
  222. } else {
  223. $this->output = substr($this->output, 0, $pos).'_*'.substr($this->output, $pos);
  224. }
  225. }
  226. /**
  227. * Returns a copy of the current asset with filters and a target URL applied.
  228. *
  229. * @param Boolean $raw Returns the unmodified asset if true
  230. */
  231. public function current($raw = false)
  232. {
  233. $asset = current($this->assets);
  234. if ($raw) {
  235. return $asset;
  236. }
  237. // clone once
  238. if (!isset($this->clones[$asset])) {
  239. $clone = $this->clones[$asset] = clone $asset;
  240. // generate a target path based on asset name
  241. $name = sprintf('%s_%d', pathinfo($asset->getSourcePath(), PATHINFO_FILENAME) ?: 'part', $this->key() + 1);
  242. $clone->setTargetPath(str_replace('*', $name, $this->output));
  243. } else {
  244. $clone = $this->clones[$asset];
  245. }
  246. // cascade filters
  247. foreach ($this->filters as $filter) {
  248. $clone->ensureFilter($filter);
  249. }
  250. return $clone;
  251. }
  252. public function key()
  253. {
  254. return key($this->assets);
  255. }
  256. public function next()
  257. {
  258. return next($this->assets);
  259. }
  260. public function rewind()
  261. {
  262. return reset($this->assets);
  263. }
  264. public function valid()
  265. {
  266. return false !== current($this->assets);
  267. }
  268. public function hasChildren()
  269. {
  270. return current($this->assets) instanceof AssetCollection;
  271. }
  272. /**
  273. * @uses current()
  274. */
  275. public function getChildren()
  276. {
  277. return new self($this->current(), $this->clones);
  278. }
  279. }