ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
LazyAssetManager.php
Go to the documentation of this file.
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 
12 namespace Assetic\Factory;
13 
18 
25 {
26  private $factory;
27  private $loaders;
28  private $resources;
29  private $formulae;
30  private $loaded;
31  private $loading;
32 
40  {
41  $this->factory = $factory;
42  $this->loaders = array();
43  $this->resources = array();
44  $this->formulae = array();
45  $this->loaded = false;
46  $this->loading = false;
47 
48  foreach ($loaders as $alias => $loader) {
49  $this->setLoader($alias, $loader);
50  }
51  }
52 
59  public function setLoader($alias, FormulaLoaderInterface $loader)
60  {
61  $this->loaders[$alias] = $loader;
62  $this->loaded = false;
63  }
64 
71  public function addResource(ResourceInterface $resource, $loader)
72  {
73  $this->resources[$loader][] = $resource;
74  $this->loaded = false;
75  }
76 
82  public function getResources()
83  {
84  $resources = array();
85  foreach ($this->resources as $r) {
86  $resources = array_merge($resources, $r);
87  }
88 
89  return $resources;
90  }
91 
99  public function hasFormula($name)
100  {
101  if (!$this->loaded) {
102  $this->load();
103  }
104 
105  return isset($this->formulae[$name]);
106  }
107 
117  public function getFormula($name)
118  {
119  if (!$this->loaded) {
120  $this->load();
121  }
122 
123  if (!isset($this->formulae[$name])) {
124  throw new \InvalidArgumentException(sprintf('There is no "%s" formula.', $name));
125  }
126 
127  return $this->formulae[$name];
128  }
129 
136  public function setFormula($name, array $formula)
137  {
138  $this->formulae[$name] = $formula;
139  }
140 
146  public function load()
147  {
148  if ($this->loading) {
149  return;
150  }
151 
152  if ($diff = array_diff(array_keys($this->resources), array_keys($this->loaders))) {
153  throw new \LogicException('The following loader(s) are not registered: '.implode(', ', $diff));
154  }
155 
156  $this->loading = true;
157 
158  foreach ($this->resources as $loader => $resources) {
159  foreach ($resources as $resource) {
160  $this->formulae = array_replace($this->formulae, $this->loaders[$loader]->load($resource));
161  }
162  }
163 
164  $this->loaded = true;
165  $this->loading = false;
166  }
167 
168  public function get($name)
169  {
170  if (!$this->loaded) {
171  $this->load();
172  }
173 
174  if (!parent::has($name) && isset($this->formulae[$name])) {
175  list($inputs, $filters, $options) = $this->formulae[$name];
176  $options['name'] = $name;
177  parent::set($name, $this->factory->createAsset($inputs, $filters, $options));
178  }
179 
180  return parent::get($name);
181  }
182 
183  public function has($name)
184  {
185  if (!$this->loaded) {
186  $this->load();
187  }
188 
189  return isset($this->formulae[$name]) || parent::has($name);
190  }
191 
192  public function getNames()
193  {
194  if (!$this->loaded) {
195  $this->load();
196  }
197 
198  return array_unique(array_merge(parent::getNames(), array_keys($this->formulae)));
199  }
200 
201  public function isDebug()
202  {
203  return $this->factory->isDebug();
204  }
205 
206  public function getLastModified(AssetInterface $asset)
207  {
208  return $this->factory->getLastModified($asset);
209  }
210 }
Manages assets.
addResource(ResourceInterface $resource, $loader)
Adds a resource to the asset manager.
setFormula($name, array $formula)
Sets a formula on the asset manager.
A lazy asset manager is a composition of a factory and many formula loaders.
load()
Loads formulae from resources.
getFormula($name)
Returns an asset&#39;s formula.
A resource is something formulae can be loaded from.
$r
Definition: example_031.php:79
setLoader($alias, FormulaLoaderInterface $loader)
Adds a loader to the asset manager.
if(!is_array($argv)) $options
getResources()
Returns an array of resources.
hasFormula($name)
Checks for an asset formula.
An asset has a mutable URL and content and can be loaded and dumped.
Create styles array
The data for the language used.
getLastModified(AssetInterface $asset)
The asset factory creates asset objects.
__construct(AssetFactory $factory, $loaders=array())
Constructor.