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
12namespace Assetic\Factory;
13
18
25{
26 private $factory;
27 private $loaders;
28 private $resources;
29 private $formulae;
30 private $loaded;
31 private $loading;
32
39 public function __construct(AssetFactory $factory, $loaders = array())
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}
sprintf('%.4f', $callTime)
Manages assets.
The asset factory creates asset objects.
A lazy asset manager is a composition of a factory and many formula loaders.
setFormula($name, array $formula)
Sets a formula on the asset manager.
hasFormula($name)
Checks for an asset formula.
getResources()
Returns an array of resources.
has($name)
Checks if the current asset manager has a certain asset.
getFormula($name)
Returns an asset's formula.
setLoader($alias, FormulaLoaderInterface $loader)
Adds a loader to the asset manager.
addResource(ResourceInterface $resource, $loader)
Adds a resource to the asset manager.
__construct(AssetFactory $factory, $loaders=array())
Constructor.
load()
Loads formulae from resources.
getLastModified(AssetInterface $asset)
getNames()
Returns an array of asset names.
An exception for terminatinating execution or to throw for unit testing.
$r
Definition: example_031.php:79
An asset has a mutable URL and content and can be loaded and dumped.
A resource is something formulae can be loaded from.
get($resource)
Loads and returns the value for the supplied cache key.
Definition: ConfigCache.php:75
if(!is_array($argv)) $options