ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_Compiler Class Reference

Compiles a node to PHP code. More...

+ Inheritance diagram for Twig_Compiler:
+ Collaboration diagram for Twig_Compiler:

Public Member Functions

 __construct (Twig_Environment $env)
 
 getFilename ()
 
 getEnvironment ()
 Returns the environment instance related to this compiler. More...
 
 getSource ()
 Gets the current PHP code after compilation. More...
 
 compile (Twig_NodeInterface $node, $indentation=0)
 Compiles a node. More...
 
 subcompile (Twig_NodeInterface $node, $raw=true)
 
 raw ($string)
 Adds a raw string to the compiled code. More...
 
 write ()
 Writes a string to the compiled code by adding indentation. More...
 
 addIndentation ()
 Appends an indentation to the current PHP code after compilation. More...
 
 string ($value)
 Adds a quoted string to the compiled code. More...
 
 repr ($value)
 Returns a PHP representation of a given value. More...
 
 addDebugInfo (Twig_NodeInterface $node)
 Adds debugging information. More...
 
 getDebugInfo ()
 
 indent ($step=1)
 Indents the generated code. More...
 
 outdent ($step=1)
 Outdents the generated code. More...
 
 getVarName ()
 
- Public Member Functions inherited from Twig_CompilerInterface
 compile (Twig_NodeInterface $node)
 Compiles a node. More...
 

Protected Attributes

 $lastLine
 
 $source
 
 $indentation
 
 $env
 
 $debugInfo = array()
 
 $sourceOffset
 
 $sourceLine
 
 $filename
 

Private Attributes

 $varNameSalt = 0
 

Detailed Description

Compiles a node to PHP code.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 18 of file Compiler.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Compiler::__construct ( Twig_Environment  $env)

Definition at line 30 of file Compiler.php.

References $env.

31  {
32  $this->env = $env;
33  }

Member Function Documentation

◆ addDebugInfo()

Twig_Compiler::addDebugInfo ( Twig_NodeInterface  $node)

Adds debugging information.

Returns
$this

Definition at line 212 of file Compiler.php.

References $lastLine, $sourceLine, and write().

Referenced by Twig_Node_SetTemp\compile(), Twig_Node_Sandbox\compile(), Twig_Node_SandboxedPrint\compile(), Twig_Node_Do\compile(), Twig_Node_Flush\compile(), Twig_Node_Import\compile(), Twig_Node_BlockReference\compile(), Twig_Node_Print\compile(), Twig_Node_Expression_Name\compile(), Twig_Node_Expression_Parent\compile(), Twig_Node_Text\compile(), Twig_Node_Block\compile(), Twig_Node_Spaceless\compile(), Twig_Node_With\compile(), Twig_Node_Include\compile(), Twig_Node_If\compile(), Twig_Node_Macro\compile(), Twig_Node_For\compile(), Twig_Extensions_Node_Trans\compile(), Twig_Node_Set\compile(), Twig_Node_Expression_BlockReference\compile(), Twig_Node_Module\compileConstructor(), and Twig_Node_Module\compileDisplay().

213  {
214  if ($node->getTemplateLine() != $this->lastLine) {
215  $this->write(sprintf("// line %d\n", $node->getTemplateLine()));
216 
217  // when mbstring.func_overload is set to 2
218  // mb_substr_count() replaces substr_count()
219  // but they have different signatures!
220  if (((int) ini_get('mbstring.func_overload')) & 2) {
221  @trigger_error('Support for having "mbstring.func_overload" different from 0 is deprecated version 1.29 and will be removed in 2.0.', E_USER_DEPRECATED);
222 
223  // this is much slower than the "right" version
224  $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
225  } else {
226  $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
227  }
228  $this->sourceOffset = strlen($this->source);
229  $this->debugInfo[$this->sourceLine] = $node->getTemplateLine();
230 
231  $this->lastLine = $node->getTemplateLine();
232  }
233 
234  return $this;
235  }
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addIndentation()

Twig_Compiler::addIndentation ( )

Appends an indentation to the current PHP code after compilation.

Returns
$this
Deprecated:
since 1.27 (to be removed in 2.0).

Definition at line 141 of file Compiler.php.

142  {
143  @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use write(\'\') instead.', E_USER_DEPRECATED);
144 
145  $this->source .= str_repeat(' ', $this->indentation * 4);
146 
147  return $this;
148  }

◆ compile()

Twig_Compiler::compile ( Twig_NodeInterface  $node,
  $indentation = 0 
)

Compiles a node.

Parameters
Twig_NodeInterface$nodeThe node to compile
int$indentationThe current indentation
Returns
$this

Definition at line 73 of file Compiler.php.

References $indentation, array, and Twig_NodeInterface\compile().

74  {
75  $this->lastLine = null;
76  $this->source = '';
77  $this->debugInfo = array();
78  $this->sourceOffset = 0;
79  // source code starts at 1 (as we then increment it when we encounter new lines)
80  $this->sourceLine = 1;
81  $this->indentation = $indentation;
82  $this->varNameSalt = 0;
83 
84  if ($node instanceof Twig_Node_Module) {
85  // to be removed in 2.0
86  $this->filename = $node->getTemplateName();
87  }
88 
89  $node->compile($this);
90 
91  return $this;
92  }
Represents a module node.
Definition: Module.php:22
Create styles array
The data for the language used.
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
+ Here is the call graph for this function:

◆ getDebugInfo()

Twig_Compiler::getDebugInfo ( )

Definition at line 237 of file Compiler.php.

References $debugInfo.

Referenced by Twig_Node_Module\compileDebugInfo().

238  {
239  ksort($this->debugInfo);
240 
241  return $this->debugInfo;
242  }
+ Here is the caller graph for this function:

◆ getEnvironment()

Twig_Compiler::getEnvironment ( )

Returns the environment instance related to this compiler.

Returns
Twig_Environment

Definition at line 50 of file Compiler.php.

References $env.

Referenced by Twig_Node_Expression_Function\compile(), Twig_Node_Expression_Filter\compile(), Twig_Node_Expression_Test\compile(), Twig_Node_Expression_Name\compile(), Twig_Node_Module\compileClassHeader(), and Twig_Node_Module\compileGetSourceContext().

51  {
52  return $this->env;
53  }
+ Here is the caller graph for this function:

◆ getFilename()

Twig_Compiler::getFilename ( )
Deprecated:
since 1.25 (to be removed in 2.0)

Definition at line 38 of file Compiler.php.

References $filename.

39  {
40  @trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
41 
42  return $this->filename;
43  }

◆ getSource()

Twig_Compiler::getSource ( )

Gets the current PHP code after compilation.

Returns
string The PHP code

Implements Twig_CompilerInterface.

Definition at line 60 of file Compiler.php.

References $source.

61  {
62  return $this->source;
63  }

◆ getVarName()

Twig_Compiler::getVarName ( )

Definition at line 279 of file Compiler.php.

References GuzzleHttp\Psr7\hash().

Referenced by Twig_Node_Expression_Binary_EndsWith\compile(), Twig_Node_Expression_Binary_StartsWith\compile(), and Twig_Node_With\compile().

280  {
281  return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->varNameSalt++));
282  }
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ indent()

Twig_Compiler::indent (   $step = 1)

Indents the generated code.

Parameters
int$stepThe number of indentation to add
Returns
$this

Definition at line 251 of file Compiler.php.

Referenced by Twig_Node_Module\compileConstructor().

252  {
253  $this->indentation += $step;
254 
255  return $this;
256  }
+ Here is the caller graph for this function:

◆ outdent()

Twig_Compiler::outdent (   $step = 1)

Outdents the generated code.

Parameters
int$stepThe number of indentation to remove
Returns
$this
Exceptions
LogicExceptionWhen trying to outdent too much so the indentation would become negative

Definition at line 267 of file Compiler.php.

Referenced by Twig_Node_If\compile(), Twig_Node_Include\compile(), Twig_Node_Macro\compile(), and Twig_Node_Module\compileConstructor().

268  {
269  // can't outdent by more steps than the current indentation level
270  if ($this->indentation < $step) {
271  throw new LogicException('Unable to call outdent() as the indentation would become negative.');
272  }
273 
274  $this->indentation -= $step;
275 
276  return $this;
277  }
+ Here is the caller graph for this function:

◆ raw()

Twig_Compiler::raw (   $string)

Adds a raw string to the compiled code.

Parameters
string$stringThe string
Returns
$this

Definition at line 112 of file Compiler.php.

Referenced by Twig_Node_Include\addTemplateArguments(), Twig_Node_Expression_Binary_EndsWith\compile(), Twig_Node_Expression_Binary_Matches\compile(), Twig_Node_Expression_Binary_NotIn\compile(), Twig_Node_Expression_Binary_Power\compile(), Twig_Node_Expression_Binary_Range\compile(), Twig_Node_Expression_Binary_StartsWith\compile(), Twig_Node_Expression_Binary_FloorDiv\compile(), Twig_Node_Expression_Binary_In\compile(), Twig_Node_Expression_AssignName\compile(), Twig_Node_Expression_TempName\compile(), Twig_Node_Expression_Binary\compile(), Twig_Node_Expression_Conditional\compile(), Twig_Node_Expression_Test_Sameas\compile(), Twig_Node_Expression_Unary\compile(), Twig_Node_Expression_MethodCall\compile(), Twig_Node_Expression_Test_Null\compile(), Twig_Node_Expression_Test_Divisibleby\compile(), Twig_Node_Expression_Test_Odd\compile(), Twig_Node_Expression_Test_Even\compile(), Twig_Node_Expression_GetAttr\compile(), Twig_Node_Expression_NullCoalesce\compile(), Twig_Node_Import\compile(), Twig_Node_Expression_Name\compile(), Twig_Node_Expression_Test_Constant\compile(), Twig_Node_Expression_Parent\compile(), Twig_Node_Expression_ExtensionReference\compile(), Twig_Node_Include\compile(), Twig_Node_Macro\compile(), Twig_Extensions_Node_Trans\compile(), Twig_Node_Set\compile(), Twig_Node_Expression_Array\compile(), Twig_Node_Expression_Call\compileArguments(), Twig_Node_Expression_BlockReference\compileBlockArguments(), Twig_Node_Expression_Call\compileCallable(), Twig_Node_Module\compileDisplay(), Twig_Node_Module\compileGetParent(), Twig_Node_Expression_BlockReference\compileTemplateCall(), Twig_Node_Expression_Binary_Greater\operator(), Twig_Node_Expression_Binary_NotEqual\operator(), Twig_Node_Expression_Binary_Equal\operator(), Twig_Node_Expression_Binary_LessEqual\operator(), Twig_Node_Expression_Binary_GreaterEqual\operator(), Twig_Node_Expression_Binary_Less\operator(), Twig_Node_Expression_Binary_Add\operator(), Twig_Node_Expression_Binary_And\operator(), Twig_Node_Expression_Unary_Pos\operator(), Twig_Node_Expression_Binary_Div\operator(), Twig_Node_Expression_Binary_BitwiseOr\operator(), Twig_Node_Expression_Binary_BitwiseXor\operator(), Twig_Node_Expression_Binary_Concat\operator(), Twig_Node_Expression_Binary_Mod\operator(), Twig_Node_Expression_Unary_Not\operator(), Twig_Node_Expression_Binary_BitwiseAnd\operator(), Twig_Node_Expression_Binary_Mul\operator(), Twig_Node_Expression_Binary_Sub\operator(), Twig_Node_Expression_Binary_Or\operator(), Twig_Node_Expression_Unary_Neg\operator(), Twig_Node_Expression_Binary_FloorDiv\operator(), Twig_Node_Expression_Binary_NotIn\operator(), Twig_Node_Expression_Binary_In\operator(), Twig_Node_Expression_Binary_Matches\operator(), Twig_Node_Expression_Binary_Range\operator(), Twig_Node_Expression_Binary_StartsWith\operator(), Twig_Node_Expression_Binary_EndsWith\operator(), Twig_Node_Expression_Binary_Power\operator(), and repr().

113  {
114  $this->source .= $string;
115 
116  return $this;
117  }
+ Here is the caller graph for this function:

◆ repr()

Twig_Compiler::repr (   $value)

Returns a PHP representation of a given value.

Parameters
mixed$valueThe value to convert
Returns
$this

Definition at line 171 of file Compiler.php.

References $key, raw(), and string().

Referenced by Twig_Node_Expression_Constant\compile(), and Twig_Node_Expression_Name\compile().

172  {
173  if (is_int($value) || is_float($value)) {
174  if (false !== $locale = setlocale(LC_NUMERIC, '0')) {
175  setlocale(LC_NUMERIC, 'C');
176  }
177 
178  $this->raw($value);
179 
180  if (false !== $locale) {
181  setlocale(LC_NUMERIC, $locale);
182  }
183  } elseif (null === $value) {
184  $this->raw('null');
185  } elseif (is_bool($value)) {
186  $this->raw($value ? 'true' : 'false');
187  } elseif (is_array($value)) {
188  $this->raw('array(');
189  $first = true;
190  foreach ($value as $key => $v) {
191  if (!$first) {
192  $this->raw(', ');
193  }
194  $first = false;
195  $this->repr($key);
196  $this->raw(' => ');
197  $this->repr($v);
198  }
199  $this->raw(')');
200  } else {
201  $this->string($value);
202  }
203 
204  return $this;
205  }
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
string($value)
Adds a quoted string to the compiled code.
Definition: Compiler.php:157
repr($value)
Returns a PHP representation of a given value.
Definition: Compiler.php:171
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ string()

Twig_Compiler::string (   $value)

Adds a quoted string to the compiled code.

Parameters
string$valueThe string
Returns
$this

Definition at line 157 of file Compiler.php.

Referenced by Twig_Extensions_Node_Trans\compile(), Twig_Node_Expression_Call\compileArguments(), and repr().

158  {
159  $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
160 
161  return $this;
162  }
+ Here is the caller graph for this function:

◆ subcompile()

◆ write()

Field Documentation

◆ $debugInfo

Twig_Compiler::$debugInfo = array()
protected

Definition at line 24 of file Compiler.php.

Referenced by getDebugInfo().

◆ $env

Twig_Compiler::$env
protected

Definition at line 23 of file Compiler.php.

Referenced by __construct(), and getEnvironment().

◆ $filename

Twig_Compiler::$filename
protected

Definition at line 27 of file Compiler.php.

Referenced by getFilename().

◆ $indentation

Twig_Compiler::$indentation
protected

Definition at line 22 of file Compiler.php.

Referenced by compile().

◆ $lastLine

Twig_Compiler::$lastLine
protected

Definition at line 20 of file Compiler.php.

Referenced by addDebugInfo().

◆ $source

Twig_Compiler::$source
protected

Definition at line 21 of file Compiler.php.

Referenced by getSource().

◆ $sourceLine

Twig_Compiler::$sourceLine
protected

Definition at line 26 of file Compiler.php.

Referenced by addDebugInfo().

◆ $sourceOffset

Twig_Compiler::$sourceOffset
protected

Definition at line 25 of file Compiler.php.

◆ $varNameSalt

Twig_Compiler::$varNameSalt = 0
private

Definition at line 28 of file Compiler.php.


The documentation for this class was generated from the following file: