summaryrefslogtreecommitdiff
path: root/inc/lib/Twig/Compiler.php
diff options
context:
space:
mode:
authorczaks <marcin@6irc.net>2013-08-05 06:03:40 -0400
committerczaks <marcin@6irc.net>2013-08-05 06:03:40 -0400
commita84c4510fccfdc0cfb6f187633daea10dc70f326 (patch)
tree6077f5211196ae40cac0184bb7cc67b81d320b0b /inc/lib/Twig/Compiler.php
parent33c7abf11f3a9b5c0964a2d7a25533c2849dbc78 (diff)
parenta8327c3a37fd22d2aa5d63061c9d3eaa30ae425a (diff)
Merge branch 'master' of github.com:vichan-devel/Tinyboard
Conflicts: js/post-hider.js
Diffstat (limited to 'inc/lib/Twig/Compiler.php')
-rw-r--r--inc/lib/Twig/Compiler.php82
1 files changed, 65 insertions, 17 deletions
diff --git a/inc/lib/Twig/Compiler.php b/inc/lib/Twig/Compiler.php
index db2e8de4..99aecbcc 100644
--- a/inc/lib/Twig/Compiler.php
+++ b/inc/lib/Twig/Compiler.php
@@ -13,8 +13,7 @@
/**
* Compiles a node to PHP code.
*
- * @package twig
- * @author Fabien Potencier <fabien@symfony.com>
+ * @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Compiler implements Twig_CompilerInterface
{
@@ -22,6 +21,10 @@ class Twig_Compiler implements Twig_CompilerInterface
protected $source;
protected $indentation;
protected $env;
+ protected $debugInfo;
+ protected $sourceOffset;
+ protected $sourceLine;
+ protected $filename;
/**
* Constructor.
@@ -31,6 +34,12 @@ class Twig_Compiler implements Twig_CompilerInterface
public function __construct(Twig_Environment $env)
{
$this->env = $env;
+ $this->debugInfo = array();
+ }
+
+ public function getFilename()
+ {
+ return $this->filename;
}
/**
@@ -56,8 +65,8 @@ class Twig_Compiler implements Twig_CompilerInterface
/**
* Compiles a node.
*
- * @param Twig_NodeInterface $node The node to compile
- * @param integer $indent The current indentation
+ * @param Twig_NodeInterface $node The node to compile
+ * @param integer $indentation The current indentation
*
* @return Twig_Compiler The current compiler instance
*/
@@ -65,8 +74,15 @@ class Twig_Compiler implements Twig_CompilerInterface
{
$this->lastLine = null;
$this->source = '';
+ $this->sourceOffset = 0;
+ // source code starts at 1 (as we then increment it when we encounter new lines)
+ $this->sourceLine = 1;
$this->indentation = $indentation;
+ if ($node instanceof Twig_Node_Module) {
+ $this->filename = $node->getAttribute('filename');
+ }
+
$node->compile($this);
return $this;
@@ -86,7 +102,7 @@ class Twig_Compiler implements Twig_CompilerInterface
/**
* Adds a raw string to the compiled code.
*
- * @param string $string The string
+ * @param string $string The string
*
* @return Twig_Compiler The current compiler instance
*/
@@ -113,6 +129,11 @@ class Twig_Compiler implements Twig_CompilerInterface
return $this;
}
+ /**
+ * Appends an indentation to the current PHP code after compilation.
+ *
+ * @return Twig_Compiler The current compiler instance
+ */
public function addIndentation()
{
$this->source .= str_repeat(' ', $this->indentation * 4);
@@ -123,7 +144,7 @@ class Twig_Compiler implements Twig_CompilerInterface
/**
* Adds a quoted string to the compiled code.
*
- * @param string $string The string
+ * @param string $value The string
*
* @return Twig_Compiler The current compiler instance
*/
@@ -137,19 +158,27 @@ class Twig_Compiler implements Twig_CompilerInterface
/**
* Returns a PHP representation of a given value.
*
- * @param mixed $value The value to convert
+ * @param mixed $value The value to convert
*
* @return Twig_Compiler The current compiler instance
*/
public function repr($value)
{
if (is_int($value) || is_float($value)) {
+ if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
+ setlocale(LC_NUMERIC, 'C');
+ }
+
$this->raw($value);
- } else if (null === $value) {
+
+ if (false !== $locale) {
+ setlocale(LC_NUMERIC, $locale);
+ }
+ } elseif (null === $value) {
$this->raw('null');
- } else if (is_bool($value)) {
+ } elseif (is_bool($value)) {
$this->raw($value ? 'true' : 'false');
- } else if (is_array($value)) {
+ } elseif (is_array($value)) {
$this->raw('array(');
$i = 0;
foreach ($value as $key => $value) {
@@ -178,17 +207,35 @@ class Twig_Compiler implements Twig_CompilerInterface
public function addDebugInfo(Twig_NodeInterface $node)
{
if ($node->getLine() != $this->lastLine) {
- $this->lastLine = $node->getLine();
$this->write("// line {$node->getLine()}\n");
+
+ // when mbstring.func_overload is set to 2
+ // mb_substr_count() replaces substr_count()
+ // but they have different signatures!
+ if (((int) ini_get('mbstring.func_overload')) & 2) {
+ // this is much slower than the "right" version
+ $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
+ } else {
+ $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
+ }
+ $this->sourceOffset = strlen($this->source);
+ $this->debugInfo[$this->sourceLine] = $node->getLine();
+
+ $this->lastLine = $node->getLine();
}
return $this;
}
+ public function getDebugInfo()
+ {
+ return $this->debugInfo;
+ }
+
/**
* Indents the generated code.
*
- * @param integer $indent The number of indentation to add
+ * @param integer $step The number of indentation to add
*
* @return Twig_Compiler The current compiler instance
*/
@@ -202,18 +249,19 @@ class Twig_Compiler implements Twig_CompilerInterface
/**
* Outdents the generated code.
*
- * @param integer $indent The number of indentation to remove
+ * @param integer $step The number of indentation to remove
*
* @return Twig_Compiler The current compiler instance
*/
public function outdent($step = 1)
{
- $this->indentation -= $step;
-
- if ($this->indentation < 0) {
- throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
+ // can't outdent by more steps than the current indentation level
+ if ($this->indentation < $step) {
+ throw new LogicException('Unable to call outdent() as the indentation would become negative');
}
+ $this->indentation -= $step;
+
return $this;
}
}