initial import

This commit is contained in:
Alchemy
2011-02-16 16:09:48 +01:00
parent 399a584b6f
commit 339d23c06d
5539 changed files with 2028637 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Array extends Twig_Node_Expression
{
public function __construct(array $elements, $lineno)
{
parent::__construct($elements, array(), $lineno);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile($compiler)
{
$compiler->raw('array(');
$first = true;
foreach ($this->nodes as $name => $node) {
if (!$first) {
$compiler->raw(', ');
}
$first = false;
$compiler
->repr($name)
->raw(' => ')
->subcompile($node)
;
}
$compiler->raw(')');
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_AssignName extends Twig_Node_Expression_Name
{
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile($compiler)
{
$compiler->raw(sprintf('$context[\'%s\']', $this->getAttribute('name')));
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class Twig_Node_Expression_Binary extends Twig_Node_Expression
{
public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)
{
parent::__construct(array('left' => $left, 'right' => $right), array(), $lineno);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile($compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('left'))
->raw(' ')
;
$this->operator($compiler);
$compiler
->raw(' ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
abstract public function operator($compiler);
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Add extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('+');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_And extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('&&');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Concat extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('.');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Div extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('/');
}
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_FloorDiv extends Twig_Node_Expression_Binary
{
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile($compiler)
{
$compiler->raw('floor(');
parent::compile($compiler);
$compiler->raw(')');
}
public function operator($compiler)
{
return $compiler->raw('/');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Mod extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('%');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Mul extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('*');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Or extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('||');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Binary_Sub extends Twig_Node_Expression_Binary
{
public function operator($compiler)
{
return $compiler->raw('-');
}
}

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Compare extends Twig_Node_Expression
{
public function __construct(Twig_Node_Expression $expr, Twig_NodeInterface $ops, $lineno)
{
parent::__construct(array('expr' => $expr, 'ops' => $ops), array(), $lineno);
}
public function compile($compiler)
{
if ('in' === $this->getNode('ops')->getNode('0')->getAttribute('value')) {
return $this->compileIn($compiler);
}
$this->getNode('expr')->compile($compiler);
$nbOps = count($this->getNode('ops'));
for ($i = 0; $i < $nbOps; $i += 2) {
if ($i > 0) {
$compiler->raw(' && ($tmp'.($i / 2));
}
$compiler->raw(' '.$this->getNode('ops')->getNode($i)->getAttribute('value').' ');
if ($i != $nbOps - 2) {
$compiler
->raw('($tmp'.(($i / 2) + 1).' = ')
->subcompile($this->getNode('ops')->getNode($i + 1))
->raw(')')
;
} else {
$compiler->subcompile($this->getNode('ops')->getNode($i + 1));
}
}
for ($j = 1; $j < $i / 2; $j++) {
$compiler->raw(')');
}
}
protected function compileIn($compiler)
{
$compiler
->raw('twig_in_filter(')
->subcompile($this->getNode('expr'))
->raw(', ')
->subcompile($this->getNode('ops')->getNode(1))
->raw(')')
;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Conditional extends Twig_Node_Expression
{
public function __construct(Twig_Node_Expression $expr1, Twig_Node_Expression $expr2, Twig_Node_Expression $expr3, $lineno)
{
parent::__construct(array('expr1' => $expr1, 'expr2' => $expr2, 'expr3' => $expr3), array(), $lineno);
}
public function compile($compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('expr1'))
->raw(') ? (')
->subcompile($this->getNode('expr2'))
->raw(') : (')
->subcompile($this->getNode('expr3'))
->raw(')')
;
}
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Constant extends Twig_Node_Expression
{
public function __construct($value, $lineno)
{
parent::__construct(array(), array('value' => $value), $lineno);
}
public function compile($compiler)
{
$compiler->repr($this->getAttribute('value'));
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents an extension call node.
*
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Twig_Node_Expression_ExtensionReference extends Twig_Node_Expression
{
public function __construct($name, $lineno, $tag = null)
{
parent::__construct(array(), array('name' => $name), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile($compiler)
{
$compiler->raw(sprintf("\$this->env->getExtension('%s')", $this->getAttribute('name')));
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Filter extends Twig_Node_Expression
{
public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filter_name, Twig_NodeInterface $arguments, $lineno, $tag = null)
{
parent::__construct(array('node' => $node, 'filter' => $filter_name, 'arguments' => $arguments), array(), $lineno, $tag);
}
public function compile($compiler)
{
$filterMap = $compiler->getEnvironment()->getFilters();
$name = $this->getNode('filter')->getAttribute('value');
$attrs = $this->getNode('arguments');
if (!isset($filterMap[$name])) {
throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
} else {
$compiler->raw($filterMap[$name]->compile().($filterMap[$name]->needsEnvironment() ? '($this->env, ' : '('));
}
$this->getNode('node')->compile($compiler);
foreach ($attrs as $node) {
$compiler
->raw(', ')
->subcompile($node)
;
}
$compiler->raw(')');
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
{
const TYPE_ANY = 'any';
const TYPE_ARRAY = 'array';
const TYPE_METHOD = 'method';
public function __construct(Twig_Node_Expression $node, Twig_Node_Expression $attribute, Twig_NodeInterface $arguments, $type, $lineno)
{
parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type), $lineno);
}
public function compile($compiler)
{
$compiler
->raw('$this->getAttribute(')
->subcompile($this->getNode('node'))
->raw(', ')
->subcompile($this->getNode('attribute'))
->raw(', array(')
;
foreach ($this->getNode('arguments') as $node) {
$compiler
->subcompile($node)
->raw(', ')
;
}
$compiler
->raw('), ')
->repr($this->getAttribute('type'))
->raw(')');
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Name extends Twig_Node_Expression
{
public function __construct($name, $lineno)
{
parent::__construct(array(), array('name' => $name), $lineno);
}
public function compile($compiler)
{
if ('_self' === $this->getAttribute('name')) {
$compiler->raw('$this');
} elseif ('_context' === $this->getAttribute('name')) {
$compiler->raw('$context');
} elseif ('_charset' === $this->getAttribute('name')) {
$compiler->raw('$this->getEnvironment()->getCharset()');
} elseif ($compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this->getAttribute('name'), $this->getAttribute('name')));
} else {
$compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->getAttribute('name'), $this->getAttribute('name')));
}
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2010 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Test extends Twig_Node_Expression
{
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
{
parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno);
}
public function compile($compiler)
{
$testMap = $compiler->getEnvironment()->getTests();
if (!isset($testMap[$this->getAttribute('name')])) {
throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
// defined is a special case
if ('defined' === $this->getAttribute('name')) {
if (!$this->getNode('node') instanceof Twig_Node_Expression_Name){
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
$compiler
->raw($testMap[$this->getAttribute('name')]->compile().'(')
->repr($this->getNode('node')->getAttribute('name'))
->raw(', $context)')
;
return;
}
$compiler
->raw($testMap[$this->getAttribute('name')]->compile().'(')
->subcompile($this->getNode('node'))
;
if (null !== $this->getNode('arguments')) {
$compiler->raw(', ');
$max = count($this->getNode('arguments')) - 1;
foreach ($this->getNode('arguments') as $i => $node) {
$compiler->subcompile($node);
if ($i != $max) {
$compiler->raw(', ');
}
}
}
$compiler->raw(')');
}
}

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class Twig_Node_Expression_Unary extends Twig_Node_Expression
{
public function __construct(Twig_NodeInterface $node, $lineno)
{
parent::__construct(array('node' => $node), array(), $lineno);
}
public function compile($compiler)
{
$compiler->raw('(');
$this->operator($compiler);
$compiler
->subcompile($this->getNode('node'))
->raw(')')
;
}
abstract public function operator($compiler);
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Unary_Neg extends Twig_Node_Expression_Unary
{
public function operator($compiler)
{
$compiler->raw('-');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Unary_Not extends Twig_Node_Expression_Unary
{
public function operator($compiler)
{
$compiler->raw('!');
}
}

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_Unary_Pos extends Twig_Node_Expression_Unary
{
public function operator($compiler)
{
$compiler->raw('+');
}
}