|
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\Asset\AssetInterface; |
|
|
15 |
|
|
|
16 |
class AsseticNode extends \Twig_Node |
|
|
17 |
{ |
|
|
18 |
/** |
|
|
19 |
* Constructor. |
|
|
20 |
* |
|
|
21 |
* Available attributes: |
|
|
22 |
* |
|
|
23 |
* * debug: The debug mode |
|
|
24 |
* * combine: Whether to combine assets |
|
|
25 |
* * var_name: The name of the variable to expose to the body node |
|
|
26 |
* |
|
|
27 |
* @param AssetInterface $asset The asset |
|
|
28 |
* @param Twig_NodeInterface $body The body node |
|
|
29 |
* @param array $inputs An array of input strings |
|
|
30 |
* @param array $filters An array of filter strings |
|
|
31 |
* @param string $name The name of the asset |
|
|
32 |
* @param array $attributes An array of attributes |
|
|
33 |
* @param integer $lineno The line number |
|
|
34 |
* @param string $tag The tag name |
|
|
35 |
*/ |
|
|
36 |
public function __construct(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null) |
|
|
37 |
{ |
|
|
38 |
$nodes = array('body' => $body); |
|
|
39 |
|
|
|
40 |
$attributes = array_replace( |
|
|
41 |
array('debug' => null, 'combine' => null, 'var_name' => 'asset_url'), |
|
|
42 |
$attributes, |
|
|
43 |
array('asset' => $asset, 'inputs' => $inputs, 'filters' => $filters, 'name' => $name) |
|
|
44 |
); |
|
|
45 |
|
|
|
46 |
parent::__construct($nodes, $attributes, $lineno, $tag); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
public function compile(\Twig_Compiler $compiler) |
|
|
50 |
{ |
|
|
51 |
$compiler->addDebugInfo($this); |
|
|
52 |
|
|
|
53 |
$combine = $this->getAttribute('combine'); |
|
|
54 |
$debug = $this->getAttribute('debug'); |
|
|
55 |
|
|
|
56 |
if (null === $combine && null !== $debug) { |
|
|
57 |
$combine = !$debug; |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
if (null === $combine) { |
|
|
61 |
$compiler |
|
|
62 |
->write("if (isset(\$context['assetic']['debug']) && \$context['assetic']['debug']) {\n") |
|
|
63 |
->indent() |
|
|
64 |
; |
|
|
65 |
|
|
|
66 |
$this->compileDebug($compiler); |
|
|
67 |
|
|
|
68 |
$compiler |
|
|
69 |
->outdent() |
|
|
70 |
->write("} else {\n") |
|
|
71 |
->indent() |
|
|
72 |
; |
|
|
73 |
|
|
|
74 |
$this->compileAsset($compiler, $this->getAttribute('asset'), $this->getAttribute('name')); |
|
|
75 |
|
|
|
76 |
$compiler |
|
|
77 |
->outdent() |
|
|
78 |
->write("}\n") |
|
|
79 |
; |
|
|
80 |
} elseif ($combine) { |
|
|
81 |
$this->compileAsset($compiler, $this->getAttribute('asset'), $this->getAttribute('name')); |
|
|
82 |
} else { |
|
|
83 |
$this->compileDebug($compiler); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
$compiler |
|
|
87 |
->write('unset($context[') |
|
|
88 |
->repr($this->getAttribute('var_name')) |
|
|
89 |
->raw("]);\n") |
|
|
90 |
; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
protected function compileDebug(\Twig_Compiler $compiler) |
|
|
94 |
{ |
|
|
95 |
$i = 0; |
|
|
96 |
foreach ($this->getAttribute('asset') as $leaf) { |
|
|
97 |
$leafName = $this->getAttribute('name').'_'.$i++; |
|
|
98 |
$this->compileAsset($compiler, $leaf, $leafName); |
|
|
99 |
} |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
protected function compileAsset(\Twig_Compiler $compiler, AssetInterface $asset, $name) |
|
|
103 |
{ |
|
|
104 |
$compiler |
|
|
105 |
->write("// asset \"$name\"\n") |
|
|
106 |
->write('$context[') |
|
|
107 |
->repr($this->getAttribute('var_name')) |
|
|
108 |
->raw('] = ') |
|
|
109 |
; |
|
|
110 |
|
|
|
111 |
$this->compileAssetUrl($compiler, $asset, $name); |
|
|
112 |
|
|
|
113 |
$compiler |
|
|
114 |
->raw(";\n") |
|
|
115 |
->subcompile($this->getNode('body')) |
|
|
116 |
; |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name) |
|
|
120 |
{ |
|
|
121 |
$compiler->repr($asset->getTargetPath()); |
|
|
122 |
} |
|
|
123 |
} |