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