Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Twig to 1.13.1 #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/parsers/Twig/Autoloader.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@
/**
* Autoloads Twig classes.
*
* @package twig
* @author Fabien Potencier <[email protected]>
* @author Fabien Potencier <[email protected]>
*/
class Twig_Autoloader
{
/**
* Registers Twig_Autoloader as an SPL autoloader.
*
* @param Boolean $prepend Whether to prepend the autoloader or not.
*/
static public function register()
public static function register($prepend = false)
{
ini_set('unserialize_callback_func', 'spl_autoload_call');
spl_autoload_register(array(new self, 'autoload'));
if (version_compare(phpversion(), '5.3.0', '>=')) {
spl_autoload_register(array(new self, 'autoload'), true, $prepend);
} else {
spl_autoload_register(array(new self, 'autoload'));
}
}

/**
* Handles autoloading of classes.
*
* @param string $class A class name.
*
* @return boolean Returns true if the class has been loaded
* @param string $class A class name.
*/
static public function autoload($class)
public static function autoload($class)
{
if (0 !== strpos($class, 'Twig')) {
return;
Expand Down
82 changes: 65 additions & 17 deletions app/parsers/Twig/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
/**
* Compiles a node to PHP code.
*
* @package twig
* @author Fabien Potencier <[email protected]>
* @author Fabien Potencier <[email protected]>
*/
class Twig_Compiler implements Twig_CompilerInterface
{
protected $lastLine;
protected $source;
protected $indentation;
protected $env;
protected $debugInfo;
protected $sourceOffset;
protected $sourceLine;
protected $filename;

/**
* Constructor.
Expand All @@ -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;
}

/**
Expand All @@ -56,17 +65,24 @@ public function getSource()
/**
* 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
*/
public function compile(Twig_NodeInterface $node, $indentation = 0)
{
$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;
Expand All @@ -86,7 +102,7 @@ public function subcompile(Twig_NodeInterface $node, $raw = true)
/**
* 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
*/
Expand All @@ -113,6 +129,11 @@ public function write()
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);
Expand All @@ -123,7 +144,7 @@ public function addIndentation()
/**
* 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
*/
Expand All @@ -137,19 +158,27 @@ public function string($value)
/**
* 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) {
Expand Down Expand Up @@ -178,17 +207,35 @@ public function repr($value)
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
*/
Expand All @@ -202,18 +249,19 @@ public function indent($step = 1)
/**
* 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;
}
}
10 changes: 5 additions & 5 deletions app/parsers/Twig/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
/**
* Interface implemented by compiler classes.
*
* @package twig
* @author Fabien Potencier <[email protected]>
* @author Fabien Potencier <[email protected]>
* @deprecated since 1.12 (to be removed in 2.0)
*/
interface Twig_CompilerInterface
{
/**
* Compiles a node.
*
* @param Twig_NodeInterface $node The node to compile
* @param Twig_NodeInterface $node The node to compile
*
* @return Twig_CompilerInterface The current compiler instance
*/
function compile(Twig_NodeInterface $node);
public function compile(Twig_NodeInterface $node);

/**
* Gets the current PHP code after compilation.
*
* @return string The PHP code
*/
function getSource();
public function getSource();
}
Loading