source: sandbox/newExpressoMail/newExpressoMail/Assetic/Factory/LazyAssetManager.php @ 7265

Revision 7265, 5.0 KB checked in by gustavo, 12 years ago (diff)

Ticket #0000 - Criado novo modulo para o desenvolvimento do novo ExpressoMail?

  • Property svn:executable set to *
Line 
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
12namespace Assetic\Factory;
13
14use Assetic\AssetManager;
15use Assetic\Factory\Loader\FormulaLoaderInterface;
16use Assetic\Factory\Resource\ResourceInterface;
17
18/**
19 * A lazy asset manager is a composition of a factory and many formula loaders.
20 *
21 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
22 */
23class LazyAssetManager extends AssetManager
24{
25    private $factory;
26    private $loaders;
27    private $resources;
28    private $formulae;
29    private $loaded;
30    private $loading;
31
32    /**
33     * Constructor.
34     *
35     * @param AssetFactory $factory The asset factory
36     * @param array        $loaders An array of loaders indexed by alias
37     */
38    public function __construct(AssetFactory $factory, $loaders = array())
39    {
40        $this->factory = $factory;
41        $this->loaders = array();
42        $this->resources = array();
43        $this->formulae = array();
44        $this->loaded = false;
45        $this->loading = false;
46
47        foreach ($loaders as $alias => $loader) {
48            $this->setLoader($alias, $loader);
49        }
50    }
51
52    /**
53     * Adds a loader to the asset manager.
54     *
55     * @param string                 $alias  An alias for the loader
56     * @param FormulaLoaderInterface $loader A loader
57     */
58    public function setLoader($alias, FormulaLoaderInterface $loader)
59    {
60        $this->loaders[$alias] = $loader;
61        $this->loaded = false;
62    }
63
64    /**
65     * Adds a resource to the asset manager.
66     *
67     * @param ResourceInterface $resource A resource
68     * @param string            $loader   The loader alias for this resource
69     */
70    public function addResource(ResourceInterface $resource, $loader)
71    {
72        $this->resources[$loader][] = $resource;
73        $this->loaded = false;
74    }
75
76    /**
77     * Returns an array of resources.
78     *
79     * @return array An array of resources
80     */
81    public function getResources()
82    {
83        $resources = array();
84        foreach ($this->resources as $r) {
85            $resources = array_merge($resources, $r);
86        }
87
88        return $resources;
89    }
90
91    /**
92     * Checks for an asset formula.
93     *
94     * @param string $name An asset name
95     *
96     * @return Boolean If there is a formula
97     */
98    public function hasFormula($name)
99    {
100        if (!$this->loaded) {
101            $this->load();
102        }
103
104        return isset($this->formulae[$name]);
105    }
106
107    /**
108     * Returns an asset's formula.
109     *
110     * @param string $name An asset name
111     *
112     * @return array The formula
113     *
114     * @throws \InvalidArgumentException If there is no formula by that name
115     */
116    public function getFormula($name)
117    {
118        if (!$this->loaded) {
119            $this->load();
120        }
121
122        if (!isset($this->formulae[$name])) {
123            throw new \InvalidArgumentException(sprintf('There is no "%s" formula.', $name));
124        }
125
126        return $this->formulae[$name];
127    }
128
129    /**
130     * Sets a formula on the asset manager.
131     *
132     * @param string $name    An asset name
133     * @param array  $formula A formula
134     */
135    public function setFormula($name, array $formula)
136    {
137        $this->formulae[$name] = $formula;
138    }
139
140    /**
141     * Loads formulae from resources.
142     *
143     * @throws \LogicException If a resource has been added to an invalid loader
144     */
145    public function load()
146    {
147        if ($this->loading) {
148            return;
149        }
150
151        if ($diff = array_diff(array_keys($this->resources), array_keys($this->loaders))) {
152            throw new \LogicException('The following loader(s) are not registered: '.implode(', ', $diff));
153        }
154
155        $this->loading = true;
156
157        foreach ($this->resources as $loader => $resources) {
158            foreach ($resources as $resource) {
159                $this->formulae = array_replace($this->formulae, $this->loaders[$loader]->load($resource));
160            }
161        }
162
163        $this->loaded = true;
164        $this->loading = false;
165    }
166
167    public function get($name)
168    {
169        if (!$this->loaded) {
170            $this->load();
171        }
172
173        if (!parent::has($name) && isset($this->formulae[$name])) {
174            list($inputs, $filters, $options) = $this->formulae[$name];
175            $options['name'] = $name;
176            parent::set($name, $this->factory->createAsset($inputs, $filters, $options));
177        }
178
179        return parent::get($name);
180    }
181
182    public function has($name)
183    {
184        if (!$this->loaded) {
185            $this->load();
186        }
187
188        return isset($this->formulae[$name]) || parent::has($name);
189    }
190
191    public function getNames()
192    {
193        if (!$this->loaded) {
194            $this->load();
195        }
196
197        return array_unique(array_merge(parent::getNames(), array_keys($this->formulae)));
198    }
199
200    public function isDebug()
201    {
202        return $this->factory->isDebug();
203    }
204}
Note: See TracBrowser for help on using the repository browser.