[7265] | 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; |
---|
| 13 | |
---|
| 14 | use Assetic\Asset\Iterator\AssetCollectionFilterIterator; |
---|
| 15 | use Assetic\Asset\Iterator\AssetCollectionIterator; |
---|
| 16 | use Assetic\Filter\FilterCollection; |
---|
| 17 | use Assetic\Filter\FilterInterface; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * A collection of assets. |
---|
| 21 | * |
---|
| 22 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
| 23 | */ |
---|
| 24 | class AssetCollection implements \IteratorAggregate, AssetCollectionInterface |
---|
| 25 | { |
---|
| 26 | private $assets; |
---|
| 27 | private $filters; |
---|
| 28 | private $sourceRoot; |
---|
| 29 | private $targetPath; |
---|
| 30 | private $content; |
---|
| 31 | private $clones; |
---|
| 32 | private $vars; |
---|
| 33 | private $values; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Constructor. |
---|
| 37 | * |
---|
| 38 | * @param array $assets Assets for the current collection |
---|
| 39 | * @param array $filters Filters for the current collection |
---|
| 40 | * @param string $sourceRoot The root directory |
---|
| 41 | * @param array $vars |
---|
| 42 | */ |
---|
| 43 | public function __construct($assets = array(), $filters = array(), $sourceRoot = null, array $vars = array()) |
---|
| 44 | { |
---|
| 45 | $this->assets = array(); |
---|
| 46 | foreach ($assets as $asset) { |
---|
| 47 | $this->add($asset); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | $this->filters = new FilterCollection($filters); |
---|
| 51 | $this->sourceRoot = $sourceRoot; |
---|
| 52 | $this->clones = new \SplObjectStorage(); |
---|
| 53 | $this->vars = $vars; |
---|
| 54 | $this->values = array(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | public function all() |
---|
| 58 | { |
---|
| 59 | return $this->assets; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public function add(AssetInterface $asset) |
---|
| 63 | { |
---|
| 64 | $this->assets[] = $asset; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | public function removeLeaf(AssetInterface $needle, $graceful = false) |
---|
| 68 | { |
---|
| 69 | foreach ($this->assets as $i => $asset) { |
---|
| 70 | $clone = isset($this->clones[$asset]) ? $this->clones[$asset] : null; |
---|
| 71 | if (in_array($needle, array($asset, $clone), true)) { |
---|
| 72 | unset($this->clones[$asset], $this->assets[$i]); |
---|
| 73 | |
---|
| 74 | return true; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if ($asset instanceof AssetCollectionInterface && $asset->removeLeaf($needle, true)) { |
---|
| 78 | return true; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if ($graceful) { |
---|
| 83 | return false; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | throw new \InvalidArgumentException('Leaf not found.'); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public function replaceLeaf(AssetInterface $needle, AssetInterface $replacement, $graceful = false) |
---|
| 90 | { |
---|
| 91 | foreach ($this->assets as $i => $asset) { |
---|
| 92 | $clone = isset($this->clones[$asset]) ? $this->clones[$asset] : null; |
---|
| 93 | if (in_array($needle, array($asset, $clone), true)) { |
---|
| 94 | unset($this->clones[$asset]); |
---|
| 95 | $this->assets[$i] = $replacement; |
---|
| 96 | |
---|
| 97 | return true; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | if ($asset instanceof AssetCollectionInterface && $asset->replaceLeaf($needle, $replacement, true)) { |
---|
| 101 | return true; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if ($graceful) { |
---|
| 106 | return false; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | throw new \InvalidArgumentException('Leaf not found.'); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | public function ensureFilter(FilterInterface $filter) |
---|
| 113 | { |
---|
| 114 | $this->filters->ensure($filter); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | public function getFilters() |
---|
| 118 | { |
---|
| 119 | return $this->filters->all(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public function clearFilters() |
---|
| 123 | { |
---|
| 124 | $this->filters->clear(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | public function load(FilterInterface $additionalFilter = null) |
---|
| 128 | { |
---|
| 129 | // loop through leaves and load each asset |
---|
| 130 | $parts = array(); |
---|
| 131 | foreach ($this as $asset) { |
---|
| 132 | $asset->load($additionalFilter); |
---|
| 133 | $parts[] = $asset->getContent(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | $this->content = implode("\n", $parts); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | public function dump(FilterInterface $additionalFilter = null) |
---|
| 140 | { |
---|
| 141 | // loop through leaves and dump each asset |
---|
| 142 | $parts = array(); |
---|
| 143 | foreach ($this as $asset) { |
---|
| 144 | $parts[] = $asset->dump($additionalFilter); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return implode("\n", $parts); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | public function getContent() |
---|
| 151 | { |
---|
| 152 | return $this->content; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | public function setContent($content) |
---|
| 156 | { |
---|
| 157 | $this->content = $content; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | public function getSourceRoot() |
---|
| 161 | { |
---|
| 162 | return $this->sourceRoot; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | public function getSourcePath() |
---|
| 166 | { |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | public function getTargetPath() |
---|
| 170 | { |
---|
| 171 | return $this->targetPath; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | public function setTargetPath($targetPath) |
---|
| 175 | { |
---|
| 176 | $this->targetPath = $targetPath; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Returns the highest last-modified value of all assets in the current collection. |
---|
| 181 | * |
---|
| 182 | * @return integer|null A UNIX timestamp |
---|
| 183 | */ |
---|
| 184 | public function getLastModified() |
---|
| 185 | { |
---|
| 186 | if (!count($this->assets)) { |
---|
| 187 | return; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | $mtime = 0; |
---|
| 191 | foreach ($this as $asset) { |
---|
| 192 | $assetMtime = $asset->getLastModified(); |
---|
| 193 | if ($assetMtime > $mtime) { |
---|
| 194 | $mtime = $assetMtime; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | return $mtime; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Returns an iterator for looping recursively over unique leaves. |
---|
| 203 | */ |
---|
| 204 | public function getIterator() |
---|
| 205 | { |
---|
| 206 | return new \RecursiveIteratorIterator(new AssetCollectionFilterIterator(new AssetCollectionIterator($this, $this->clones))); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | public function getVars() |
---|
| 210 | { |
---|
| 211 | return $this->vars; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | public function setValues(array $values) |
---|
| 215 | { |
---|
| 216 | $this->values = $values; |
---|
| 217 | |
---|
| 218 | foreach ($this as $asset) { |
---|
| 219 | $asset->setValues(array_intersect_key($values, array_flip($asset->getVars()))); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | public function getValues() |
---|
| 224 | { |
---|
| 225 | return $this->values; |
---|
| 226 | } |
---|
| 227 | } |
---|