From 874b8cbf002e9d8bf0ff39671e1501165016320a Mon Sep 17 00:00:00 2001 From: Michael Save Date: Mon, 9 Apr 2012 20:52:26 +1000 Subject: inc/contrib -> inc/lib --- inc/lib/Twig/Node/Expression/Array.php | 41 ++++++++++++ inc/lib/Twig/Node/Expression/AssignName.php | 24 ++++++++ inc/lib/Twig/Node/Expression/Binary.php | 40 ++++++++++++ inc/lib/Twig/Node/Expression/Binary/Add.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/And.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/BitwiseAnd.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/BitwiseOr.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/BitwiseXor.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/Concat.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/Div.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/Equal.php | 17 +++++ inc/lib/Twig/Node/Expression/Binary/FloorDiv.php | 29 +++++++++ inc/lib/Twig/Node/Expression/Binary/Greater.php | 17 +++++ .../Twig/Node/Expression/Binary/GreaterEqual.php | 17 +++++ inc/lib/Twig/Node/Expression/Binary/In.php | 33 ++++++++++ inc/lib/Twig/Node/Expression/Binary/Less.php | 17 +++++ inc/lib/Twig/Node/Expression/Binary/LessEqual.php | 17 +++++ inc/lib/Twig/Node/Expression/Binary/Mod.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/Mul.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/NotEqual.php | 17 +++++ inc/lib/Twig/Node/Expression/Binary/NotIn.php | 33 ++++++++++ inc/lib/Twig/Node/Expression/Binary/Or.php | 18 ++++++ inc/lib/Twig/Node/Expression/Binary/Power.php | 33 ++++++++++ inc/lib/Twig/Node/Expression/Binary/Range.php | 33 ++++++++++ inc/lib/Twig/Node/Expression/Binary/Sub.php | 18 ++++++ inc/lib/Twig/Node/Expression/BlockReference.php | 52 ++++++++++++++++ inc/lib/Twig/Node/Expression/Conditional.php | 31 ++++++++++ inc/lib/Twig/Node/Expression/Constant.php | 23 +++++++ .../Twig/Node/Expression/ExtensionReference.php | 34 ++++++++++ inc/lib/Twig/Node/Expression/Filter.php | 72 ++++++++++++++++++++++ inc/lib/Twig/Node/Expression/Function.php | 49 +++++++++++++++ inc/lib/Twig/Node/Expression/GetAttr.php | 53 ++++++++++++++++ inc/lib/Twig/Node/Expression/Name.php | 41 ++++++++++++ inc/lib/Twig/Node/Expression/Parent.php | 39 ++++++++++++ inc/lib/Twig/Node/Expression/Test.php | 60 ++++++++++++++++++ inc/lib/Twig/Node/Expression/Unary.php | 30 +++++++++ inc/lib/Twig/Node/Expression/Unary/Neg.php | 18 ++++++ inc/lib/Twig/Node/Expression/Unary/Not.php | 18 ++++++ inc/lib/Twig/Node/Expression/Unary/Pos.php | 18 ++++++ 39 files changed, 1104 insertions(+) create mode 100644 inc/lib/Twig/Node/Expression/Array.php create mode 100644 inc/lib/Twig/Node/Expression/AssignName.php create mode 100644 inc/lib/Twig/Node/Expression/Binary.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Add.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/And.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/BitwiseAnd.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/BitwiseOr.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/BitwiseXor.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Concat.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Div.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Equal.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/FloorDiv.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Greater.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/GreaterEqual.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/In.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Less.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/LessEqual.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Mod.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Mul.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/NotEqual.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/NotIn.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Or.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Power.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Range.php create mode 100644 inc/lib/Twig/Node/Expression/Binary/Sub.php create mode 100644 inc/lib/Twig/Node/Expression/BlockReference.php create mode 100644 inc/lib/Twig/Node/Expression/Conditional.php create mode 100644 inc/lib/Twig/Node/Expression/Constant.php create mode 100644 inc/lib/Twig/Node/Expression/ExtensionReference.php create mode 100644 inc/lib/Twig/Node/Expression/Filter.php create mode 100644 inc/lib/Twig/Node/Expression/Function.php create mode 100644 inc/lib/Twig/Node/Expression/GetAttr.php create mode 100644 inc/lib/Twig/Node/Expression/Name.php create mode 100644 inc/lib/Twig/Node/Expression/Parent.php create mode 100644 inc/lib/Twig/Node/Expression/Test.php create mode 100644 inc/lib/Twig/Node/Expression/Unary.php create mode 100644 inc/lib/Twig/Node/Expression/Unary/Neg.php create mode 100644 inc/lib/Twig/Node/Expression/Unary/Not.php create mode 100644 inc/lib/Twig/Node/Expression/Unary/Pos.php (limited to 'inc/lib/Twig/Node/Expression') diff --git a/inc/lib/Twig/Node/Expression/Array.php b/inc/lib/Twig/Node/Expression/Array.php new file mode 100644 index 00000000..2d860823 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Array.php @@ -0,0 +1,41 @@ +raw('array('); + $first = true; + foreach ($this->nodes as $name => $node) { + if (!$first) { + $compiler->raw(', '); + } + $first = false; + + $compiler + ->repr($name) + ->raw(' => ') + ->subcompile($node) + ; + } + $compiler->raw(')'); + } +} diff --git a/inc/lib/Twig/Node/Expression/AssignName.php b/inc/lib/Twig/Node/Expression/AssignName.php new file mode 100644 index 00000000..67f12509 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/AssignName.php @@ -0,0 +1,24 @@ +raw(sprintf('$context[\'%s\']', $this->getAttribute('name'))); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary.php b/inc/lib/Twig/Node/Expression/Binary.php new file mode 100644 index 00000000..9dd5de2c --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary.php @@ -0,0 +1,40 @@ + $left, 'right' => $right), array(), $lineno); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('left')) + ->raw(' ') + ; + $this->operator($compiler); + $compiler + ->raw(' ') + ->subcompile($this->getNode('right')) + ->raw(')') + ; + } + + abstract public function operator(Twig_Compiler $compiler); +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Add.php b/inc/lib/Twig/Node/Expression/Binary/Add.php new file mode 100644 index 00000000..0ef8e117 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Add.php @@ -0,0 +1,18 @@ +raw('+'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/And.php b/inc/lib/Twig/Node/Expression/Binary/And.php new file mode 100644 index 00000000..d5752ebb --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/And.php @@ -0,0 +1,18 @@ +raw('&&'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/BitwiseAnd.php b/inc/lib/Twig/Node/Expression/Binary/BitwiseAnd.php new file mode 100644 index 00000000..9a46d845 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/BitwiseAnd.php @@ -0,0 +1,18 @@ +raw('&'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/BitwiseOr.php b/inc/lib/Twig/Node/Expression/Binary/BitwiseOr.php new file mode 100644 index 00000000..058a20bf --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/BitwiseOr.php @@ -0,0 +1,18 @@ +raw('|'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/BitwiseXor.php b/inc/lib/Twig/Node/Expression/Binary/BitwiseXor.php new file mode 100644 index 00000000..f4da73d4 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/BitwiseXor.php @@ -0,0 +1,18 @@ +raw('^'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Concat.php b/inc/lib/Twig/Node/Expression/Binary/Concat.php new file mode 100644 index 00000000..f9a64627 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Concat.php @@ -0,0 +1,18 @@ +raw('.'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Div.php b/inc/lib/Twig/Node/Expression/Binary/Div.php new file mode 100644 index 00000000..e0797a61 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Div.php @@ -0,0 +1,18 @@ +raw('/'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Equal.php b/inc/lib/Twig/Node/Expression/Binary/Equal.php new file mode 100644 index 00000000..7b1236d0 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Equal.php @@ -0,0 +1,17 @@ +raw('=='); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/FloorDiv.php b/inc/lib/Twig/Node/Expression/Binary/FloorDiv.php new file mode 100644 index 00000000..e86b1ea0 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/FloorDiv.php @@ -0,0 +1,29 @@ +raw('floor('); + parent::compile($compiler); + $compiler->raw(')'); + } + + public function operator(Twig_Compiler $compiler) + { + return $compiler->raw('/'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Greater.php b/inc/lib/Twig/Node/Expression/Binary/Greater.php new file mode 100644 index 00000000..a110bd92 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Greater.php @@ -0,0 +1,17 @@ +raw('>'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/GreaterEqual.php b/inc/lib/Twig/Node/Expression/Binary/GreaterEqual.php new file mode 100644 index 00000000..3754fed2 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/GreaterEqual.php @@ -0,0 +1,17 @@ +raw('>='); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/In.php b/inc/lib/Twig/Node/Expression/Binary/In.php new file mode 100644 index 00000000..788f9377 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/In.php @@ -0,0 +1,33 @@ +raw('twig_in_filter(') + ->subcompile($this->getNode('left')) + ->raw(', ') + ->subcompile($this->getNode('right')) + ->raw(')') + ; + } + + public function operator(Twig_Compiler $compiler) + { + return $compiler->raw('in'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Less.php b/inc/lib/Twig/Node/Expression/Binary/Less.php new file mode 100644 index 00000000..45fd3004 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Less.php @@ -0,0 +1,17 @@ +raw('<'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/LessEqual.php b/inc/lib/Twig/Node/Expression/Binary/LessEqual.php new file mode 100644 index 00000000..e38e257c --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/LessEqual.php @@ -0,0 +1,17 @@ +raw('<='); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Mod.php b/inc/lib/Twig/Node/Expression/Binary/Mod.php new file mode 100644 index 00000000..9924114f --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Mod.php @@ -0,0 +1,18 @@ +raw('%'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Mul.php b/inc/lib/Twig/Node/Expression/Binary/Mul.php new file mode 100644 index 00000000..c91529ca --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Mul.php @@ -0,0 +1,18 @@ +raw('*'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/NotEqual.php b/inc/lib/Twig/Node/Expression/Binary/NotEqual.php new file mode 100644 index 00000000..26867ba2 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/NotEqual.php @@ -0,0 +1,17 @@ +raw('!='); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/NotIn.php b/inc/lib/Twig/Node/Expression/Binary/NotIn.php new file mode 100644 index 00000000..f347b7b6 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/NotIn.php @@ -0,0 +1,33 @@ +raw('!twig_in_filter(') + ->subcompile($this->getNode('left')) + ->raw(', ') + ->subcompile($this->getNode('right')) + ->raw(')') + ; + } + + public function operator(Twig_Compiler $compiler) + { + return $compiler->raw('not in'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Or.php b/inc/lib/Twig/Node/Expression/Binary/Or.php new file mode 100644 index 00000000..adba49c6 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Or.php @@ -0,0 +1,18 @@ +raw('||'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Power.php b/inc/lib/Twig/Node/Expression/Binary/Power.php new file mode 100644 index 00000000..b2c59040 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Power.php @@ -0,0 +1,33 @@ +raw('pow(') + ->subcompile($this->getNode('left')) + ->raw(', ') + ->subcompile($this->getNode('right')) + ->raw(')') + ; + } + + public function operator(Twig_Compiler $compiler) + { + return $compiler->raw('**'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Range.php b/inc/lib/Twig/Node/Expression/Binary/Range.php new file mode 100644 index 00000000..bea4f2a6 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Range.php @@ -0,0 +1,33 @@ +raw('range(') + ->subcompile($this->getNode('left')) + ->raw(', ') + ->subcompile($this->getNode('right')) + ->raw(')') + ; + } + + public function operator(Twig_Compiler $compiler) + { + return $compiler->raw('..'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Binary/Sub.php b/inc/lib/Twig/Node/Expression/Binary/Sub.php new file mode 100644 index 00000000..d4463991 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Binary/Sub.php @@ -0,0 +1,18 @@ +raw('-'); + } +} diff --git a/inc/lib/Twig/Node/Expression/BlockReference.php b/inc/lib/Twig/Node/Expression/BlockReference.php new file mode 100644 index 00000000..174d9097 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/BlockReference.php @@ -0,0 +1,52 @@ + + */ +class Twig_Node_Expression_BlockReference extends Twig_Node_Expression +{ + public function __construct(Twig_NodeInterface $name, $asString = false, $lineno, $tag = null) + { + parent::__construct(array('name' => $name), array('as_string' => $asString, 'output' => false), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile(Twig_Compiler $compiler) + { + if ($this->getAttribute('as_string')) { + $compiler->raw('(string) '); + } + + if ($this->getAttribute('output')) { + $compiler + ->addDebugInfo($this) + ->write("\$this->displayBlock(") + ->subcompile($this->getNode('name')) + ->raw(", \$context, \$blocks);\n") + ; + } else { + $compiler + ->raw("\$this->renderBlock(") + ->subcompile($this->getNode('name')) + ->raw(", \$context, \$blocks)") + ; + } + } +} diff --git a/inc/lib/Twig/Node/Expression/Conditional.php b/inc/lib/Twig/Node/Expression/Conditional.php new file mode 100644 index 00000000..edcb1e2d --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Conditional.php @@ -0,0 +1,31 @@ + $expr1, 'expr2' => $expr2, 'expr3' => $expr3), array(), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('((') + ->subcompile($this->getNode('expr1')) + ->raw(') ? (') + ->subcompile($this->getNode('expr2')) + ->raw(') : (') + ->subcompile($this->getNode('expr3')) + ->raw('))') + ; + } +} diff --git a/inc/lib/Twig/Node/Expression/Constant.php b/inc/lib/Twig/Node/Expression/Constant.php new file mode 100644 index 00000000..a91dc698 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Constant.php @@ -0,0 +1,23 @@ + $value), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler->repr($this->getAttribute('value')); + } +} diff --git a/inc/lib/Twig/Node/Expression/ExtensionReference.php b/inc/lib/Twig/Node/Expression/ExtensionReference.php new file mode 100644 index 00000000..cb4efad0 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/ExtensionReference.php @@ -0,0 +1,34 @@ + + */ +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(Twig_Compiler $compiler) + { + $compiler->raw(sprintf("\$this->env->getExtension('%s')", $this->getAttribute('name'))); + } +} diff --git a/inc/lib/Twig/Node/Expression/Filter.php b/inc/lib/Twig/Node/Expression/Filter.php new file mode 100644 index 00000000..101e6df0 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Filter.php @@ -0,0 +1,72 @@ + $node, 'filter' => $filterName, 'arguments' => $arguments), array(), $lineno, $tag); + } + + public function compile(Twig_Compiler $compiler) + { + $name = $this->getNode('filter')->getAttribute('value'); + if (false === $filter = $compiler->getEnvironment()->getFilter($name)) { + throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine()); + } + + $node = $this->getNode('node'); + + // The default filter is intercepted when the filtered value + // is a name (like obj) or an attribute (like obj.attr) + // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} + if ('default' === $name && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { + $compiler + ->raw('((') + ->subcompile(new Twig_Node_Expression_Test($node, 'defined', new Twig_Node(), $this->getLine())) + ->raw(') ? (') + ; + + $this->compileFilter($compiler, $filter); + + $compiler->raw(') : ('); + + if ($this->getNode('arguments')->hasNode(0)) { + $compiler->subcompile($this->getNode('arguments')->getNode(0)); + } else { + $compiler->string(''); + } + + $compiler->raw('))'); + } else { + $this->compileFilter($compiler, $filter); + } + } + + protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter) + { + $compiler + ->raw($filter->compile().'(') + ->raw($filter->needsEnvironment() ? '$this->env, ' : '') + ->raw($filter->needsContext() ? '$context, ' : '') + ->subcompile($this->getNode('node')) + ; + + foreach ($this->getNode('arguments') as $node) { + $compiler + ->raw(', ') + ->subcompile($node) + ; + } + + $compiler->raw(')'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Function.php b/inc/lib/Twig/Node/Expression/Function.php new file mode 100644 index 00000000..3f457199 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Function.php @@ -0,0 +1,49 @@ + $arguments), array('name' => $name), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name')); + if (false === $function) { + throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getAttribute('name')), $this->getLine()); + } + + $compiler + ->raw($function->compile().'(') + ->raw($function->needsEnvironment() ? '$this->env' : '') + ; + + if ($function->needsContext()) { + $compiler->raw($function->needsEnvironment() ? ', $context' : '$context'); + } + + $first = true; + foreach ($this->getNode('arguments') as $node) { + if (!$first) { + $compiler->raw(', '); + } else { + if ($function->needsEnvironment() || $function->needsContext()) { + $compiler->raw(', '); + } + $first = false; + } + $compiler->subcompile($node); + } + + $compiler->raw(')'); + } +} diff --git a/inc/lib/Twig/Node/Expression/GetAttr.php b/inc/lib/Twig/Node/Expression/GetAttr.php new file mode 100644 index 00000000..eb9e6050 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/GetAttr.php @@ -0,0 +1,53 @@ + $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler->raw('$this->getAttribute('); + + if ($this->hasAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) { + $compiler->subcompile(new Twig_Node_Expression_Filter( + $this->getNode('node'), + new Twig_Node_Expression_Constant('default', $this->getLine()), + new Twig_Node(), + $this->getLine() + )); + } else { + $compiler->subcompile($this->getNode('node')); + } + + $compiler + ->raw(', ') + ->subcompile($this->getNode('attribute')) + ->raw(', array(') + ; + + foreach ($this->getNode('arguments') as $node) { + $compiler + ->subcompile($node) + ->raw(', ') + ; + } + + $compiler + ->raw('), ') + ->repr($this->getAttribute('type')) + ->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false') + ->raw(')'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Name.php b/inc/lib/Twig/Node/Expression/Name.php new file mode 100644 index 00000000..fceda2b7 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Name.php @@ -0,0 +1,41 @@ + $name), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + static $specialVars = array( + '_self' => '$this', + '_context' => '$context', + '_charset' => '$this->env->getCharset()', + ); + + $name = $this->getAttribute('name'); + + if ($this->hasAttribute('is_defined_test')) { + if (isset($specialVars[$name])) { + $compiler->repr(true); + } else { + $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); + } + } elseif (isset($specialVars[$name])) { + $compiler->raw($specialVars[$name]); + } else { + $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); + } + } +} diff --git a/inc/lib/Twig/Node/Expression/Parent.php b/inc/lib/Twig/Node/Expression/Parent.php new file mode 100644 index 00000000..5689fe45 --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Parent.php @@ -0,0 +1,39 @@ + + */ +class Twig_Node_Expression_Parent 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(Twig_Compiler $compiler) + { + $compiler + ->raw("\$this->renderParentBlock(") + ->string($this->getAttribute('name')) + ->raw(", \$context, \$blocks)") + ; + } +} diff --git a/inc/lib/Twig/Node/Expression/Test.php b/inc/lib/Twig/Node/Expression/Test.php new file mode 100644 index 00000000..ef35df6b --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Test.php @@ -0,0 +1,60 @@ + $node, 'arguments' => $arguments), array('name' => $name), $lineno); + } + + public function compile(Twig_Compiler $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()); + } + + $name = $this->getAttribute('name'); + $node = $this->getNode('node'); + + // defined is a special case + if ('defined' === $name) { + if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) { + $node->setAttribute('is_defined_test', true); + $compiler->subcompile($node); + $node->removeAttribute('is_defined_test'); + } else { + throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); + } + return; + } + + $compiler + ->raw($testMap[$name]->compile().'(') + ->subcompile($node) + ; + + if (null !== $this->getNode('arguments')) { + $compiler->raw(', '); + + $max = count($this->getNode('arguments')) - 1; + foreach ($this->getNode('arguments') as $i => $arg) { + $compiler->subcompile($arg); + + if ($i != $max) { + $compiler->raw(', '); + } + } + } + + $compiler->raw(')'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Unary.php b/inc/lib/Twig/Node/Expression/Unary.php new file mode 100644 index 00000000..c514388e --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Unary.php @@ -0,0 +1,30 @@ + $node), array(), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler->raw('('); + $this->operator($compiler); + $compiler + ->subcompile($this->getNode('node')) + ->raw(')') + ; + } + + abstract public function operator(Twig_Compiler $compiler); +} diff --git a/inc/lib/Twig/Node/Expression/Unary/Neg.php b/inc/lib/Twig/Node/Expression/Unary/Neg.php new file mode 100644 index 00000000..2a3937ec --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Unary/Neg.php @@ -0,0 +1,18 @@ +raw('-'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Unary/Not.php b/inc/lib/Twig/Node/Expression/Unary/Not.php new file mode 100644 index 00000000..f94073cf --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Unary/Not.php @@ -0,0 +1,18 @@ +raw('!'); + } +} diff --git a/inc/lib/Twig/Node/Expression/Unary/Pos.php b/inc/lib/Twig/Node/Expression/Unary/Pos.php new file mode 100644 index 00000000..04edb52a --- /dev/null +++ b/inc/lib/Twig/Node/Expression/Unary/Pos.php @@ -0,0 +1,18 @@ +raw('+'); + } +} -- cgit v1.3