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 | /** |
---|
15 | * Asset collection filter iterator. |
---|
16 | * |
---|
17 | * The filter iterator is responsible for de-duplication of leaf assets based |
---|
18 | * on both strict equality and source URL. |
---|
19 | * |
---|
20 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
21 | */ |
---|
22 | class AssetCollectionFilterIterator extends \RecursiveFilterIterator |
---|
23 | { |
---|
24 | private $visited; |
---|
25 | private $sources; |
---|
26 | |
---|
27 | /** |
---|
28 | * Constructor. |
---|
29 | * |
---|
30 | * @param AssetCollectionIterator $iterator The inner iterator |
---|
31 | * @param array $visited An array of visited asset objects |
---|
32 | * @param array $sources An array of visited source strings |
---|
33 | */ |
---|
34 | public function __construct(AssetCollectionIterator $iterator, array $visited = array(), array $sources = array()) |
---|
35 | { |
---|
36 | parent::__construct($iterator); |
---|
37 | |
---|
38 | $this->visited = $visited; |
---|
39 | $this->sources = $sources; |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Determines whether the current asset is a duplicate. |
---|
44 | * |
---|
45 | * De-duplication is performed based on either strict equality or by |
---|
46 | * matching sources. |
---|
47 | * |
---|
48 | * @return Boolean Returns true if we have not seen this asset yet |
---|
49 | */ |
---|
50 | public function accept() |
---|
51 | { |
---|
52 | $asset = $this->getInnerIterator()->current(true); |
---|
53 | $duplicate = false; |
---|
54 | |
---|
55 | // check strict equality |
---|
56 | if (in_array($asset, $this->visited, true)) { |
---|
57 | $duplicate = true; |
---|
58 | } else { |
---|
59 | $this->visited[] = $asset; |
---|
60 | } |
---|
61 | |
---|
62 | // check source |
---|
63 | $sourceRoot = $asset->getSourceRoot(); |
---|
64 | $sourcePath = $asset->getSourcePath(); |
---|
65 | if ($sourceRoot && $sourcePath) { |
---|
66 | $source = $sourceRoot.'/'.$sourcePath; |
---|
67 | if (in_array($source, $this->sources)) { |
---|
68 | $duplicate = true; |
---|
69 | } else { |
---|
70 | $this->sources[] = $source; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return !$duplicate; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Passes visited objects and source URLs to the child iterator. |
---|
79 | */ |
---|
80 | public function getChildren() |
---|
81 | { |
---|
82 | return new self($this->getInnerIterator()->getChildren(), $this->visited, $this->sources); |
---|
83 | } |
---|
84 | } |
---|