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\Util\PathUtils; |
---|
15 | |
---|
16 | use Assetic\Filter\FilterInterface; |
---|
17 | |
---|
18 | /** |
---|
19 | * A collection of assets loaded by glob. |
---|
20 | * |
---|
21 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
22 | */ |
---|
23 | class GlobAsset extends AssetCollection |
---|
24 | { |
---|
25 | private $globs; |
---|
26 | private $initialized; |
---|
27 | |
---|
28 | /** |
---|
29 | * Constructor. |
---|
30 | * |
---|
31 | * @param string|array $globs A single glob path or array of paths |
---|
32 | * @param array $filters An array of filters |
---|
33 | * @param string $root The root directory |
---|
34 | * @param array $vars |
---|
35 | */ |
---|
36 | public function __construct($globs, $filters = array(), $root = null, array $vars = array()) |
---|
37 | { |
---|
38 | $this->globs = (array) $globs; |
---|
39 | $this->initialized = false; |
---|
40 | |
---|
41 | parent::__construct(array(), $filters, $root, $vars); |
---|
42 | } |
---|
43 | |
---|
44 | public function all() |
---|
45 | { |
---|
46 | if (!$this->initialized) { |
---|
47 | $this->initialize(); |
---|
48 | } |
---|
49 | |
---|
50 | return parent::all(); |
---|
51 | } |
---|
52 | |
---|
53 | public function load(FilterInterface $additionalFilter = null) |
---|
54 | { |
---|
55 | if (!$this->initialized) { |
---|
56 | $this->initialize(); |
---|
57 | } |
---|
58 | |
---|
59 | parent::load($additionalFilter); |
---|
60 | } |
---|
61 | |
---|
62 | public function dump(FilterInterface $additionalFilter = null) |
---|
63 | { |
---|
64 | if (!$this->initialized) { |
---|
65 | $this->initialize(); |
---|
66 | } |
---|
67 | |
---|
68 | return parent::dump($additionalFilter); |
---|
69 | } |
---|
70 | |
---|
71 | public function getLastModified() |
---|
72 | { |
---|
73 | if (!$this->initialized) { |
---|
74 | $this->initialize(); |
---|
75 | } |
---|
76 | |
---|
77 | return parent::getLastModified(); |
---|
78 | } |
---|
79 | |
---|
80 | public function getIterator() |
---|
81 | { |
---|
82 | if (!$this->initialized) { |
---|
83 | $this->initialize(); |
---|
84 | } |
---|
85 | |
---|
86 | return parent::getIterator(); |
---|
87 | } |
---|
88 | |
---|
89 | public function setValues(array $values) |
---|
90 | { |
---|
91 | parent::setValues($values); |
---|
92 | $this->initialized = false; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Initializes the collection based on the glob(s) passed in. |
---|
97 | */ |
---|
98 | private function initialize() |
---|
99 | { |
---|
100 | foreach ($this->globs as $glob) { |
---|
101 | $glob = PathUtils::resolvePath($glob, $this->getVars(), $this->getValues()); |
---|
102 | |
---|
103 | if (false !== $paths = glob($glob)) { |
---|
104 | foreach ($paths as $path) { |
---|
105 | if (is_file($path)) { |
---|
106 | $this->add(new FileAsset($path, array(), $this->getSourceRoot())); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | $this->initialized = true; |
---|
113 | } |
---|
114 | } |
---|