1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Assetic package, an OpenSky project. |
---|
5 | * |
---|
6 | * (c) 2010-2012 OpenSky Project Inc |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | namespace Assetic\Asset\Iterator; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetCollectionInterface; |
---|
15 | |
---|
16 | /** |
---|
17 | * Iterates over an asset collection. |
---|
18 | * |
---|
19 | * The iterator is responsible for cascading filters and target URL patterns |
---|
20 | * from parent to child assets. |
---|
21 | * |
---|
22 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
23 | */ |
---|
24 | class AssetCollectionIterator implements \RecursiveIterator |
---|
25 | { |
---|
26 | private $assets; |
---|
27 | private $filters; |
---|
28 | private $output; |
---|
29 | private $clones; |
---|
30 | |
---|
31 | public function __construct(AssetCollectionInterface $coll, \SplObjectStorage $clones) |
---|
32 | { |
---|
33 | $this->assets = $coll->all(); |
---|
34 | $this->filters = $coll->getFilters(); |
---|
35 | $this->output = $coll->getTargetPath(); |
---|
36 | $this->clones = $clones; |
---|
37 | |
---|
38 | if (false === $pos = strpos($this->output, '.')) { |
---|
39 | $this->output .= '_*'; |
---|
40 | } else { |
---|
41 | $this->output = substr($this->output, 0, $pos).'_*'.substr($this->output, $pos); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Returns a copy of the current asset with filters and a target URL applied. |
---|
47 | * |
---|
48 | * @param Boolean $raw Returns the unmodified asset if true |
---|
49 | * @return \Assetic\Asset\AssetInterface |
---|
50 | */ |
---|
51 | public function current($raw = false) |
---|
52 | { |
---|
53 | $asset = current($this->assets); |
---|
54 | |
---|
55 | if ($raw) { |
---|
56 | return $asset; |
---|
57 | } |
---|
58 | |
---|
59 | // clone once |
---|
60 | if (!isset($this->clones[$asset])) { |
---|
61 | $clone = $this->clones[$asset] = clone $asset; |
---|
62 | |
---|
63 | // generate a target path based on asset name |
---|
64 | $name = sprintf('%s_%d', pathinfo($asset->getSourcePath(), PATHINFO_FILENAME) ?: 'part', $this->key() + 1); |
---|
65 | $clone->setTargetPath(str_replace('*', $name, $this->output)); |
---|
66 | } else { |
---|
67 | $clone = $this->clones[$asset]; |
---|
68 | } |
---|
69 | |
---|
70 | // cascade filters |
---|
71 | foreach ($this->filters as $filter) { |
---|
72 | $clone->ensureFilter($filter); |
---|
73 | } |
---|
74 | |
---|
75 | return $clone; |
---|
76 | } |
---|
77 | |
---|
78 | public function key() |
---|
79 | { |
---|
80 | return key($this->assets); |
---|
81 | } |
---|
82 | |
---|
83 | public function next() |
---|
84 | { |
---|
85 | return next($this->assets); |
---|
86 | } |
---|
87 | |
---|
88 | public function rewind() |
---|
89 | { |
---|
90 | return reset($this->assets); |
---|
91 | } |
---|
92 | |
---|
93 | public function valid() |
---|
94 | { |
---|
95 | return false !== current($this->assets); |
---|
96 | } |
---|
97 | |
---|
98 | public function hasChildren() |
---|
99 | { |
---|
100 | return current($this->assets) instanceof AssetCollectionInterface; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * @uses current() |
---|
105 | */ |
---|
106 | public function getChildren() |
---|
107 | { |
---|
108 | return new self($this->current(), $this->clones); |
---|
109 | } |
---|
110 | } |
---|