|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony framework. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * This source file is subject to the MIT license that is bundled |
|
9 * with this source code in the file LICENSE. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\AsseticBundle\Twig; |
|
13 |
|
14 use Assetic\Extension\Twig\AsseticFilterFunction; |
|
15 |
|
16 /** |
|
17 * Assetic node visitor. |
|
18 * |
|
19 * @author Kris Wallsmith <kris@symfony.com> |
|
20 */ |
|
21 class AsseticNodeVisitor implements \Twig_NodeVisitorInterface |
|
22 { |
|
23 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) |
|
24 { |
|
25 return $node; |
|
26 } |
|
27 |
|
28 public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) |
|
29 { |
|
30 if (!$formula = $this->checkNode($node, $env)) { |
|
31 return $node; |
|
32 } |
|
33 |
|
34 list($input, $filters, $options) = $formula; |
|
35 $line = $node->getLine(); |
|
36 |
|
37 // check context and call either asset() or path() |
|
38 return new \Twig_Node_Expression_Conditional( |
|
39 new \Twig_Node_Expression_GetAttr( |
|
40 new \Twig_Node_Expression_Name('assetic', $line), |
|
41 new \Twig_Node_Expression_Constant('use_controller', $line), |
|
42 new \Twig_Node(), |
|
43 \Twig_TemplateInterface::ARRAY_CALL, |
|
44 $line |
|
45 ), |
|
46 new \Twig_Node_Expression_Function( |
|
47 new \Twig_Node_Expression_Name('path', $line), |
|
48 new \Twig_Node(array( |
|
49 new \Twig_Node_Expression_Constant('_assetic_'.$options['name'], $line), |
|
50 )), |
|
51 $line |
|
52 ), |
|
53 new \Twig_Node_Expression_Function( |
|
54 new \Twig_Node_Expression_Name('asset', $line), |
|
55 new \Twig_Node(array($node, new \Twig_Node_Expression_Constant(isset($options['package']) ? $options['package'] : null, $line))), |
|
56 $line |
|
57 ), |
|
58 $line |
|
59 ); |
|
60 } |
|
61 |
|
62 /** |
|
63 * Extracts formulae from filter function nodes. |
|
64 * |
|
65 * @return array|null The formula |
|
66 */ |
|
67 private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env) |
|
68 { |
|
69 if ($node instanceof \Twig_Node_Expression_Function) { |
|
70 $name = $node->getNode('name')->getAttribute('name'); |
|
71 if ($env->getFunction($name) instanceof AsseticFilterFunction) { |
|
72 $arguments = array(); |
|
73 foreach ($node->getNode('arguments') as $argument) { |
|
74 $arguments[] = eval('return '.$env->compile($argument).';'); |
|
75 } |
|
76 |
|
77 $invoker = $env->getExtension('assetic')->getFilterInvoker($name); |
|
78 $factory = $invoker->getFactory(); |
|
79 |
|
80 $inputs = isset($arguments[0]) ? (array) $arguments[0] : array(); |
|
81 $filters = $invoker->getFilters(); |
|
82 $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array()); |
|
83 |
|
84 if (!isset($options['name'])) { |
|
85 $options['name'] = $factory->generateAssetName($inputs, $filters); |
|
86 } |
|
87 |
|
88 return array($inputs, $filters, $options); |
|
89 } |
|
90 } |
|
91 } |
|
92 |
|
93 public function getPriority() |
|
94 { |
|
95 return 0; |
|
96 } |
|
97 } |