AssetCollection.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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\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. return $asset->getLastModified();
  122. };
  123. return max(array_map($mapper, $this->assets));
  124. }
  125. /**
  126. * Returns an iterator for looping recursively over unique leaves.
  127. */
  128. public function getIterator()
  129. {
  130. return new \RecursiveIteratorIterator(new AssetCollectionFilterIterator(new AssetCollectionIterator($this, $this->clones)));
  131. }
  132. }
  133. /**
  134. * Asset collection filter iterator.
  135. *
  136. * The filter iterator is responsible for de-duplication of leaf assets based
  137. * on both strict equality and source URL.
  138. *
  139. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  140. * @access private
  141. */
  142. class AssetCollectionFilterIterator extends \RecursiveFilterIterator
  143. {
  144. private $visited;
  145. private $sources;
  146. /**
  147. * Constructor.
  148. *
  149. * @param AssetCollectionIterator $iterator The inner iterator
  150. * @param array $visited An array of visited asset objects
  151. * @param array $sources An array of visited source strings
  152. */
  153. public function __construct(AssetCollectionIterator $iterator, array $visited = array(), array $sources = array())
  154. {
  155. parent::__construct($iterator);
  156. $this->visited = $visited;
  157. $this->sources = $sources;
  158. }
  159. /**
  160. * Determines whether the current asset is a duplicate.
  161. *
  162. * De-duplication is performed based on either strict equality or by
  163. * matching sources.
  164. *
  165. * @return Boolean Returns true if we have not seen this asset yet
  166. */
  167. public function accept()
  168. {
  169. $asset = $this->getInnerIterator()->current(true);
  170. $duplicate = false;
  171. // check strict equality
  172. if (in_array($asset, $this->visited, true)) {
  173. $duplicate = true;
  174. } else {
  175. $this->visited[] = $asset;
  176. }
  177. // check source
  178. $sourceRoot = $asset->getSourceRoot();
  179. $sourcePath = $asset->getSourcePath();
  180. if ($sourceRoot && $sourcePath) {
  181. $source = $sourceRoot.'/'.$sourcePath;
  182. if (in_array($source, $this->sources)) {
  183. $duplicate = true;
  184. } else {
  185. $this->sources[] = $source;
  186. }
  187. }
  188. return !$duplicate;
  189. }
  190. /**
  191. * Passes visited objects and source URLs to the child iterator.
  192. */
  193. public function getChildren()
  194. {
  195. return new self($this->getInnerIterator()->getChildren(), $this->visited, $this->sources);
  196. }
  197. }
  198. /**
  199. * Iterates over an asset collection.
  200. *
  201. * The iterator is responsible for cascading filters and target URL patterns
  202. * from parent to child assets.
  203. *
  204. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  205. * @access private
  206. */
  207. class AssetCollectionIterator implements \RecursiveIterator
  208. {
  209. private $assets;
  210. private $filters;
  211. private $output;
  212. private $clones;
  213. public function __construct(AssetCollection $coll, \SplObjectStorage $clones)
  214. {
  215. $this->assets = $coll->all();
  216. $this->filters = $coll->getFilters();
  217. $this->output = $coll->getTargetPath();
  218. $this->clones = $clones;
  219. if (false === $pos = strpos($this->output, '.')) {
  220. $this->output .= '_*';
  221. } else {
  222. $this->output = substr($this->output, 0, $pos).'_*'.substr($this->output, $pos);
  223. }
  224. }
  225. /**
  226. * Returns a copy of the current asset with filters and a target URL applied.
  227. *
  228. * @param Boolean $raw Returns the unmodified asset if true
  229. */
  230. public function current($raw = false)
  231. {
  232. $asset = current($this->assets);
  233. if ($raw) {
  234. return $asset;
  235. }
  236. // clone once
  237. if (!isset($this->clones[$asset])) {
  238. $clone = $this->clones[$asset] = clone $asset;
  239. // generate a target path based on asset name
  240. $name = sprintf('%s_%d', pathinfo($asset->getSourcePath(), PATHINFO_FILENAME) ?: 'part', $this->key() + 1);
  241. $clone->setTargetPath(str_replace('*', $name, $this->output));
  242. } else {
  243. $clone = $this->clones[$asset];
  244. }
  245. // cascade filters
  246. foreach ($this->filters as $filter) {
  247. $clone->ensureFilter($filter);
  248. }
  249. return $clone;
  250. }
  251. public function key()
  252. {
  253. return key($this->assets);
  254. }
  255. public function next()
  256. {
  257. return next($this->assets);
  258. }
  259. public function rewind()
  260. {
  261. return reset($this->assets);
  262. }
  263. public function valid()
  264. {
  265. return false !== current($this->assets);
  266. }
  267. public function hasChildren()
  268. {
  269. return current($this->assets) instanceof AssetCollection;
  270. }
  271. /**
  272. * @uses current()
  273. */
  274. public function getChildren()
  275. {
  276. return new self($this->current(), $this->clones);
  277. }
  278. }