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 | |
---|
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 | * @param \Twig_Node $node |
---|
47 | * |
---|
48 | * @return array An array of asset formulae indexed by name |
---|
49 | */ |
---|
50 | private function loadNode(\Twig_Node $node) |
---|
51 | { |
---|
52 | $formulae = array(); |
---|
53 | |
---|
54 | if ($node instanceof AsseticNode) { |
---|
55 | $formulae[$node->getAttribute('name')] = array( |
---|
56 | $node->getAttribute('inputs'), |
---|
57 | $node->getAttribute('filters'), |
---|
58 | array( |
---|
59 | 'output' => $node->getAttribute('asset')->getTargetPath(), |
---|
60 | 'name' => $node->getAttribute('name'), |
---|
61 | 'debug' => $node->getAttribute('debug'), |
---|
62 | 'combine' => $node->getAttribute('combine'), |
---|
63 | 'vars' => $node->getAttribute('vars'), |
---|
64 | ), |
---|
65 | ); |
---|
66 | } elseif ($node instanceof \Twig_Node_Expression_Function) { |
---|
67 | $name = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<') |
---|
68 | ? $node->getNode('name')->getAttribute('name') |
---|
69 | : $node->getAttribute('name'); |
---|
70 | |
---|
71 | if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) { |
---|
72 | $arguments = array(); |
---|
73 | foreach ($node->getNode('arguments') as $argument) { |
---|
74 | $arguments[] = eval('return '.$this->twig->compile($argument).';'); |
---|
75 | } |
---|
76 | |
---|
77 | $invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name); |
---|
78 | |
---|
79 | $inputs = isset($arguments[0]) ? (array) $arguments[0] : array(); |
---|
80 | $filters = $invoker->getFilters(); |
---|
81 | $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array()); |
---|
82 | |
---|
83 | if (!isset($options['name'])) { |
---|
84 | $options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options); |
---|
85 | } |
---|
86 | |
---|
87 | $formulae[$options['name']] = array($inputs, $filters, $options); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | foreach ($node as $child) { |
---|
92 | if ($child instanceof \Twig_Node) { |
---|
93 | $formulae += $this->loadNode($child); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | return $formulae; |
---|
98 | } |
---|
99 | } |
---|