1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\TwigJackBundle\Assetic; |
3
|
|
|
|
4
|
|
|
use Assetic\Extension\Twig\AsseticFilterFunction; |
5
|
|
|
use Assetic\Extension\Twig\AsseticNode; |
6
|
|
|
use Assetic\Factory\Loader\FormulaLoaderInterface; |
7
|
|
|
use Assetic\Factory\Resource\ResourceInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Warnar Boekkooi <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class TwigFormulaLoader implements FormulaLoaderInterface |
14
|
|
|
{ |
15
|
|
|
private $twig; |
16
|
|
|
private $logger; |
17
|
|
|
|
18
|
|
|
public function __construct(\Twig_Environment $twig, LoggerInterface $logger = null) |
19
|
|
|
{ |
20
|
|
|
$this->twig = $twig; |
21
|
|
|
$this->logger = $logger; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function load(ResourceInterface $resource) |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
$tokens = $this->twig->tokenize($resource->getContent(), (string) $resource); |
28
|
|
|
$nodes = $this->twig->parse($tokens); |
29
|
|
|
} catch (\Exception $e) { |
30
|
|
|
if ($this->logger) { |
31
|
|
|
$this->logger->error(sprintf('The template "%s" contains an error: %s', $resource, $e->getMessage())); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return array(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->loadNode($nodes); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Loads assets from the supplied node. |
42
|
|
|
* |
43
|
|
|
* @param \Twig_Node $node |
44
|
|
|
* |
45
|
|
|
* @return array An array of asset formulae indexed by name |
46
|
|
|
*/ |
47
|
|
|
private function loadNode(\Twig_Node $node) |
48
|
|
|
{ |
49
|
|
|
$formulae = array(); |
50
|
|
|
|
51
|
|
|
if ($node instanceof AsseticNode) { |
52
|
|
|
$formulae[$node->getAttribute('name')] = array( |
53
|
|
|
$node->getAttribute('inputs'), |
54
|
|
|
$node->getAttribute('filters'), |
55
|
|
|
array( |
56
|
|
|
'output' => $node->getAttribute('asset')->getTargetPath(), |
57
|
|
|
'name' => $node->getAttribute('name'), |
58
|
|
|
'debug' => $node->getAttribute('debug'), |
59
|
|
|
'combine' => $node->getAttribute('combine'), |
60
|
|
|
'vars' => $node->getAttribute('vars'), |
61
|
|
|
), |
62
|
|
|
); |
63
|
|
|
} elseif ($node instanceof \Twig_Node_Expression_Function) { |
64
|
|
|
$name = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<') |
65
|
|
|
? $node->getNode('name')->getAttribute('name') |
66
|
|
|
: $node->getAttribute('name'); |
67
|
|
|
|
68
|
|
|
if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) { |
69
|
|
|
$arguments = array(); |
70
|
|
|
foreach ($node->getNode('arguments') as $argument) { |
71
|
|
|
$arguments[] = eval('return '.$this->twig->compile($argument).';'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var \Assetic\Extension\Twig\AsseticFilterInvoker $invoker */ |
75
|
|
|
$invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$inputs = isset($arguments[0]) ? (array) $arguments[0] : array(); |
78
|
|
|
$filters = $invoker->getFilters(); |
79
|
|
|
$options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array()); |
80
|
|
|
|
81
|
|
|
if (!isset($options['name'])) { |
82
|
|
|
$options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$formulae[$options['name']] = array($inputs, $filters, $options); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($node as $child) { |
90
|
|
|
if ($child instanceof \Twig_Node) { |
91
|
|
|
$formulae += $this->loadNode($child); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($node->hasAttribute('embedded_templates')) { |
96
|
|
|
foreach ($node->getAttribute('embedded_templates') as $child) { |
97
|
|
|
$formulae += $this->loadNode($child); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// We need to also check the parent since else it won't get parsed |
102
|
|
|
if ($node->hasNode('parent') && $node->getNode('parent') instanceof \Twig_Node_Expression_Constant) { |
103
|
|
|
$parent = $node->getNode('parent')->getAttribute('value'); |
104
|
|
|
if ($parent[0] === '!') { |
105
|
|
|
$source = $this->twig->getLoader()->getSource($parent); |
106
|
|
|
$tokens = $this->twig->tokenize($source, $parent); |
107
|
|
|
$nodes = $this->twig->parse($tokens); |
108
|
|
|
|
109
|
|
|
$formulae += $this->loadNode($nodes); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $formulae; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: