|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 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 |
|
|
|
14 |
use Assetic\Asset\AssetCollection; |
|
|
15 |
use Assetic\Asset\AssetInterface; |
|
|
16 |
use Assetic\Asset\AssetReference; |
|
|
17 |
use Assetic\Asset\FileAsset; |
|
|
18 |
use Assetic\Asset\GlobAsset; |
|
|
19 |
use Assetic\Asset\HttpAsset; |
|
|
20 |
use Assetic\AssetManager; |
|
|
21 |
use Assetic\Factory\Worker\WorkerInterface; |
|
|
22 |
use Assetic\FilterManager; |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* The asset factory creates asset objects. |
|
|
26 |
* |
|
|
27 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
28 |
*/ |
|
|
29 |
class AssetFactory |
|
|
30 |
{ |
|
|
31 |
private $root; |
|
|
32 |
private $debug; |
|
|
33 |
private $output; |
|
|
34 |
private $workers; |
|
|
35 |
private $am; |
|
|
36 |
private $fm; |
|
|
37 |
|
|
|
38 |
/** |
|
|
39 |
* Constructor. |
|
|
40 |
* |
|
|
41 |
* @param string $root The default root directory |
|
|
42 |
* @param string $output The default output string |
|
|
43 |
* @param Boolean $debug Filters prefixed with a "?" will be omitted in debug mode |
|
|
44 |
*/ |
|
|
45 |
public function __construct($root, $debug = false) |
|
|
46 |
{ |
|
|
47 |
$this->root = rtrim($root, '/'); |
|
|
48 |
$this->debug = $debug; |
|
|
49 |
$this->output = 'assetic/*'; |
|
|
50 |
$this->workers = array(); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* Sets debug mode for the current factory. |
|
|
55 |
* |
|
|
56 |
* @param Boolean $debug Debug mode |
|
|
57 |
*/ |
|
|
58 |
public function setDebug($debug) |
|
|
59 |
{ |
|
|
60 |
$this->debug = $debug; |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
/** |
|
|
64 |
* Checks if the factory is in debug mode. |
|
|
65 |
* |
|
|
66 |
* @return Boolean Debug mode |
|
|
67 |
*/ |
|
|
68 |
public function isDebug() |
|
|
69 |
{ |
|
|
70 |
return $this->debug; |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
/** |
|
|
74 |
* Sets the default output string. |
|
|
75 |
* |
|
|
76 |
* @param string $output The default output string |
|
|
77 |
*/ |
|
|
78 |
public function setDefaultOutput($output) |
|
|
79 |
{ |
|
|
80 |
$this->output = $output; |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
/** |
|
|
84 |
* Adds a factory worker. |
|
|
85 |
* |
|
|
86 |
* @param WorkerInterface $worker A worker |
|
|
87 |
*/ |
|
|
88 |
public function addWorker(WorkerInterface $worker) |
|
|
89 |
{ |
|
|
90 |
$this->workers[] = $worker; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
/** |
|
|
94 |
* Returns the current asset manager. |
|
|
95 |
* |
|
|
96 |
* @return AssetManager|null The asset manager |
|
|
97 |
*/ |
|
|
98 |
public function getAssetManager() |
|
|
99 |
{ |
|
|
100 |
return $this->am; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
/** |
|
|
104 |
* Sets the asset manager to use when creating asset references. |
|
|
105 |
* |
|
|
106 |
* @param AssetManager $am The asset manager |
|
|
107 |
*/ |
|
|
108 |
public function setAssetManager(AssetManager $am) |
|
|
109 |
{ |
|
|
110 |
$this->am = $am; |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
/** |
|
|
114 |
* Returns the current filter manager. |
|
|
115 |
* |
|
|
116 |
* @return FilterManager|null The filter manager |
|
|
117 |
*/ |
|
|
118 |
public function getFilterManager() |
|
|
119 |
{ |
|
|
120 |
return $this->fm; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
/** |
|
|
124 |
* Sets the filter manager to use when adding filters. |
|
|
125 |
* |
|
|
126 |
* @param FilterManager $fm The filter manager |
|
|
127 |
*/ |
|
|
128 |
public function setFilterManager(FilterManager $fm) |
|
|
129 |
{ |
|
|
130 |
$this->fm = $fm; |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
/** |
|
|
134 |
* Creates a new asset. |
|
|
135 |
* |
|
|
136 |
* Prefixing a filter name with a question mark will cause it to be |
|
|
137 |
* omitted when the factory is in debug mode. |
|
|
138 |
* |
|
|
139 |
* Available options: |
|
|
140 |
* |
|
|
141 |
* * output: An output string |
|
|
142 |
* * name: An asset name for interpolation in output patterns |
|
|
143 |
* * debug: Forces debug mode on or off for this asset |
|
|
144 |
* * root: An array or string of more root directories |
|
|
145 |
* |
|
|
146 |
* @param array|string $inputs An array of input strings |
|
|
147 |
* @param array|string $filters An array of filter names |
|
|
148 |
* @param array $options An array of options |
|
|
149 |
* |
|
|
150 |
* @return AssetCollection An asset collection |
|
|
151 |
*/ |
|
|
152 |
public function createAsset($inputs = array(), $filters = array(), array $options = array()) |
|
|
153 |
{ |
|
|
154 |
if (!is_array($inputs)) { |
|
|
155 |
$inputs = array($inputs); |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
if (!is_array($filters)) { |
|
|
159 |
$filters = array($filters); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
if (!isset($options['output'])) { |
|
|
163 |
$options['output'] = $this->output; |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
if (!isset($options['name'])) { |
|
|
167 |
$options['name'] = $this->generateAssetName($inputs, $filters, $options); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
if (!isset($options['debug'])) { |
|
|
171 |
$options['debug'] = $this->debug; |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
if (!isset($options['root'])) { |
|
|
175 |
$options['root'] = array($this->root); |
|
|
176 |
} else { |
|
|
177 |
if (!is_array($options['root'])) { |
|
|
178 |
$options['root'] = array($options['root']); |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
$options['root'][] = $this->root; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
$asset = $this->createAssetCollection(); |
|
|
185 |
$extensions = array(); |
|
|
186 |
|
|
|
187 |
// inner assets |
|
|
188 |
foreach ($inputs as $input) { |
|
|
189 |
if (is_array($input)) { |
|
|
190 |
// nested formula |
|
|
191 |
$asset->add(call_user_func_array(array($this, 'createAsset'), $input)); |
|
|
192 |
} else { |
|
|
193 |
$asset->add($this->parseInput($input, $options)); |
|
|
194 |
$extensions[pathinfo($input, PATHINFO_EXTENSION)] = true; |
|
|
195 |
} |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
// filters |
|
|
199 |
foreach ($filters as $filter) { |
|
|
200 |
if ('?' != $filter[0]) { |
|
|
201 |
$asset->ensureFilter($this->getFilter($filter)); |
|
|
202 |
} elseif (!$options['debug']) { |
|
|
203 |
$asset->ensureFilter($this->getFilter(substr($filter, 1))); |
|
|
204 |
} |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
// append consensus extension if missing |
|
|
208 |
if (1 == count($extensions) && !pathinfo($options['output'], PATHINFO_EXTENSION) && $extension = key($extensions)) { |
|
|
209 |
$options['output'] .= '.'.$extension; |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
// output --> target url |
|
|
213 |
$asset->setTargetPath(str_replace('*', $options['name'], $options['output'])); |
|
|
214 |
|
|
|
215 |
// apply workers |
|
|
216 |
$this->processAsset($asset); |
|
|
217 |
|
|
|
218 |
return $asset; |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
public function generateAssetName($inputs, $filters, $options = array()) |
|
|
222 |
{ |
|
|
223 |
foreach (array_diff(array_keys($options), array('output', 'debug', 'root')) as $key) { |
|
|
224 |
unset($options[$key]); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
ksort($options); |
|
|
228 |
|
|
|
229 |
return substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7); |
|
|
230 |
} |
|
|
231 |
|
|
|
232 |
/** |
|
|
233 |
* Parses an input string string into an asset. |
|
|
234 |
* |
|
|
235 |
* The input string can be one of the following: |
|
|
236 |
* |
|
|
237 |
* * A reference: If the string starts with an "at" sign it will be interpreted as a reference to an asset in the asset manager |
|
|
238 |
* * An absolute URL: If the string contains "://" or starts with "//" it will be interpreted as an HTTP asset |
|
|
239 |
* * A glob: If the string contains a "*" it will be interpreted as a glob |
|
|
240 |
* * A path: Otherwise the string is interpreted as a filesystem path |
|
|
241 |
* |
|
|
242 |
* Both globs and paths will be absolutized using the current root directory. |
|
|
243 |
* |
|
|
244 |
* @param string $input An input string |
|
|
245 |
* @param array $options An array of options |
|
|
246 |
* |
|
|
247 |
* @return AssetInterface An asset |
|
|
248 |
*/ |
|
|
249 |
protected function parseInput($input, array $options = array()) |
|
|
250 |
{ |
|
|
251 |
if ('@' == $input[0]) { |
|
|
252 |
return $this->createAssetReference(substr($input, 1)); |
|
|
253 |
} |
|
|
254 |
|
|
|
255 |
if (false !== strpos($input, '://') || 0 === strpos($input, '//')) { |
|
|
256 |
return $this->createHttpAsset($input); |
|
|
257 |
} |
|
|
258 |
|
|
|
259 |
if (self::isAbsolutePath($input)) { |
|
|
260 |
if ($root = self::findRootDir($input, $options['root'])) { |
|
|
261 |
$path = ltrim(substr($input, strlen($root)), '/'); |
|
|
262 |
} else { |
|
|
263 |
$path = null; |
|
|
264 |
} |
|
|
265 |
} else { |
|
|
266 |
$root = $this->root; |
|
|
267 |
$path = $input; |
|
|
268 |
$input = $this->root.'/'.$path; |
|
|
269 |
} |
|
|
270 |
if (false !== strpos($input, '*')) { |
|
|
271 |
return $this->createGlobAsset($input, $root); |
|
|
272 |
} else { |
|
|
273 |
return $this->createFileAsset($input, $root, $path); |
|
|
274 |
} |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
protected function createAssetCollection() |
|
|
278 |
{ |
|
|
279 |
return new AssetCollection(); |
|
|
280 |
} |
|
|
281 |
|
|
|
282 |
protected function createAssetReference($name) |
|
|
283 |
{ |
|
|
284 |
if (!$this->am) { |
|
|
285 |
throw new \LogicException('There is no asset manager.'); |
|
|
286 |
} |
|
|
287 |
|
|
|
288 |
return new AssetReference($this->am, $name); |
|
|
289 |
} |
|
|
290 |
|
|
|
291 |
protected function createHttpAsset($sourceUrl) |
|
|
292 |
{ |
|
|
293 |
return new HttpAsset($sourceUrl); |
|
|
294 |
} |
|
|
295 |
|
|
|
296 |
protected function createGlobAsset($glob, $root = null) |
|
|
297 |
{ |
|
|
298 |
return new GlobAsset($glob, array(), $root); |
|
|
299 |
} |
|
|
300 |
|
|
|
301 |
protected function createFileAsset($source, $root = null, $path = null) |
|
|
302 |
{ |
|
|
303 |
return new FileAsset($source, array(), $root, $path); |
|
|
304 |
} |
|
|
305 |
|
|
|
306 |
protected function getFilter($name) |
|
|
307 |
{ |
|
|
308 |
if (!$this->fm) { |
|
|
309 |
throw new \LogicException('There is no filter manager.'); |
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
return $this->fm->get($name); |
|
|
313 |
} |
|
|
314 |
|
|
|
315 |
/** |
|
|
316 |
* Filters an asset through the factory workers. |
|
|
317 |
* |
|
|
318 |
* Each leaf asset will be processed first if the asset is traversable, |
|
|
319 |
* followed by the asset itself. |
|
|
320 |
* |
|
|
321 |
* @param AssetInterface $asset An asset |
|
|
322 |
*/ |
|
|
323 |
private function processAsset(AssetInterface $asset) |
|
|
324 |
{ |
|
|
325 |
if ($asset instanceof \Traversable) { |
|
|
326 |
foreach ($asset as $leaf) { |
|
|
327 |
foreach ($this->workers as $worker) { |
|
|
328 |
$worker->process($leaf); |
|
|
329 |
} |
|
|
330 |
} |
|
|
331 |
} |
|
|
332 |
|
|
|
333 |
foreach ($this->workers as $worker) { |
|
|
334 |
$worker->process($asset); |
|
|
335 |
} |
|
|
336 |
} |
|
|
337 |
|
|
|
338 |
static private function isAbsolutePath($path) |
|
|
339 |
{ |
|
|
340 |
return '/' == $path[0] || '\\' == $path[0] || (3 < strlen($path) && ctype_alpha($path[0]) && $path[1] == ':' && ('\\' == $path[2] || '/' == $path[2])); |
|
|
341 |
} |
|
|
342 |
|
|
|
343 |
/** |
|
|
344 |
* Loops through the root directories and returns the first match. |
|
|
345 |
* |
|
|
346 |
* @param string $path An absolute path |
|
|
347 |
* @param array $roots An array of root directories |
|
|
348 |
* |
|
|
349 |
* @return string|null The matching root directory, if found |
|
|
350 |
*/ |
|
|
351 |
static private function findRootDir($path, array $roots) |
|
|
352 |
{ |
|
|
353 |
foreach ($roots as $root) { |
|
|
354 |
if (0 === strpos($path, $root)) { |
|
|
355 |
return $root; |
|
|
356 |
} |
|
|
357 |
} |
|
|
358 |
} |
|
|
359 |
} |