ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
AsseticNode.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 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
13
15
17{
36 public function __construct(AssetInterface $asset, \Twig_Node $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 if ($vars = $asset->getVars()) {
105 $compiler->write("// check variable conditions\n");
106
107 foreach ($vars as $var) {
108 $compiler
109 ->write("if (!isset(\$context['assetic']['vars']['$var'])) {\n")
110 ->indent()
111 ->write("throw new \RuntimeException(sprintf('The asset \"".$name."\" expected variable \"".$var."\" to be set, but got only these vars: %s. Did you set-up a value supplier?', isset(\$context['assetic']['vars']) && \$context['assetic']['vars'] ? implode(', ', \$context['assetic']['vars']) : '# none #'));\n")
112 ->outdent()
113 ->write("}\n")
114 ;
115 }
116
117 $compiler->raw("\n");
118 }
119
120 $compiler
121 ->write("// asset \"$name\"\n")
122 ->write('$context[')
123 ->repr($this->getAttribute('var_name'))
124 ->raw('] = ')
125 ;
126
127 $this->compileAssetUrl($compiler, $asset, $name);
128
129 $compiler
130 ->raw(";\n")
131 ->subcompile($this->getNode('body'))
132 ;
133 }
134
135 protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
136 {
137 if (!$vars = $asset->getVars()) {
138 $compiler->repr($asset->getTargetPath());
139
140 return;
141 }
142
143 $compiler
144 ->raw("strtr(")
145 ->string($asset->getTargetPath())
146 ->raw(", array(");
147
148 $first = true;
149 foreach ($vars as $var) {
150 if (!$first) {
151 $compiler->raw(", ");
152 }
153 $first = false;
154
155 $compiler
156 ->string("{".$var."}")
157 ->raw(" => \$context['assetic']['vars']['$var']")
158 ;
159 }
160
161 $compiler
162 ->raw("))")
163 ;
164 }
165}
compileAsset(\Twig_Compiler $compiler, AssetInterface $asset, $name)
compileDebug(\Twig_Compiler $compiler)
Definition: AsseticNode.php:93
compile(\Twig_Compiler $compiler)
Definition: AsseticNode.php:49
compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
__construct(AssetInterface $asset, \Twig_Node $body, array $inputs, array $filters, $name, array $attributes=array(), $lineno=0, $tag=null)
Constructor.
Definition: AsseticNode.php:36
An exception for terminatinating execution or to throw for unit testing.
An asset has a mutable URL and content and can be loaded and dumped.
getVars()
Returns an array of variable names for this asset.
getTargetPath()
Returns the URL for the current asset.