|
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\Extension\Twig; |
|
|
13 |
|
|
|
14 |
use Assetic\Factory\Loader\FormulaLoaderInterface; |
|
|
15 |
use Assetic\Factory\Resource\ResourceInterface; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Loads asset formulae from Twig templates. |
|
|
19 |
* |
|
|
20 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
21 |
*/ |
|
|
22 |
class TwigFormulaLoader implements FormulaLoaderInterface |
|
|
23 |
{ |
|
|
24 |
private $twig; |
|
|
25 |
|
|
|
26 |
public function __construct(\Twig_Environment $twig) |
|
|
27 |
{ |
|
|
28 |
$this->twig = $twig; |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
public function load(ResourceInterface $resource) |
|
|
32 |
{ |
|
|
33 |
try { |
|
|
34 |
$tokens = $this->twig->tokenize($resource->getContent(), (string) $resource); |
|
|
35 |
$nodes = $this->twig->parse($tokens); |
|
|
36 |
} catch (\Exception $e) { |
|
|
37 |
return array(); |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
return $this->loadNode($nodes); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
/** |
|
|
44 |
* Loads assets from the supplied node. |
|
|
45 |
* |
|
|
46 |
* @return array An array of asset formulae indexed by name |
|
|
47 |
*/ |
|
|
48 |
private function loadNode(\Twig_Node $node) |
|
|
49 |
{ |
|
|
50 |
$formulae = array(); |
|
|
51 |
|
|
|
52 |
if ($node instanceof AsseticNode) { |
|
|
53 |
$formulae[$node->getAttribute('name')] = array( |
|
|
54 |
$node->getAttribute('inputs'), |
|
|
55 |
$node->getAttribute('filters'), |
|
|
56 |
array( |
|
|
57 |
'output' => $node->getAttribute('asset')->getTargetPath(), |
|
|
58 |
'name' => $node->getAttribute('name'), |
|
|
59 |
'debug' => $node->getAttribute('debug'), |
|
|
60 |
'combine' => $node->getAttribute('combine'), |
|
|
61 |
), |
|
|
62 |
); |
|
|
63 |
} elseif ($node instanceof \Twig_Node_Expression_Function) { |
|
|
64 |
$name = $node->getNode('name')->getAttribute('name'); |
|
|
65 |
if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) { |
|
|
66 |
$arguments = array(); |
|
|
67 |
foreach ($node->getNode('arguments') as $argument) { |
|
|
68 |
$arguments[] = eval('return '.$this->twig->compile($argument).';'); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
$invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name); |
|
|
72 |
|
|
|
73 |
$inputs = isset($arguments[0]) ? (array) $arguments[0] : array(); |
|
|
74 |
$filters = $invoker->getFilters(); |
|
|
75 |
$options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array()); |
|
|
76 |
|
|
|
77 |
if (!isset($options['name'])) { |
|
|
78 |
$options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
$formulae[$options['name']] = array($inputs, $filters, $options); |
|
|
82 |
} |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
foreach ($node as $child) { |
|
|
86 |
if ($child instanceof \Twig_Node) { |
|
|
87 |
$formulae += $this->loadNode($child); |
|
|
88 |
} |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
return $formulae; |
|
|
92 |
} |
|
|
93 |
} |