* the odd and even filters are now tests:
{{ foo|odd }} must now be written {{ foo is odd }}
* the "safe" filter has been renamed to "raw"
+ * in Node classes,
+ sub-nodes are now accessed via getNode() (instead of property access)
+ attributes via getAttribute() (instead of array access)
* the implementation of template inheritance has been rewritten
(blocks can now be called individually and still work with inheritance)
{
$compiler
->addDebugInfo($this)
- ->write('$context[\''.$this['name'].'\'] = ')
- ->subcompile($this->value)
+ ->write('$context[\''.$this->getAttribute('name').'\'] = ')
+ ->subcompile($this->getNode('value'))
->raw(";\n")
;
}
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
-class Twig_Node implements Twig_NodeInterface, ArrayAccess, Countable, IteratorAggregate
+class Twig_Node implements Twig_NodeInterface, Countable, IteratorAggregate
{
protected $nodes;
protected $attributes;
*/
public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
{
- $this->nodes = array();
- foreach ($nodes as $name => $node) {
- $this->$name = $node;
- }
+ $this->nodes = $nodes;
$this->attributes = $attributes;
$this->lineno = $lineno;
$this->tag = $tag;
*
* @return Boolean true if the attribute is defined, false otherwise
*/
- public function offsetExists($name)
+ public function hasAttribute($name)
{
return array_key_exists($name, $this->attributes);
}
*
* @return mixed The attribute value
*/
- public function offsetGet($name)
+ public function getAttribute($name)
{
if (!array_key_exists($name, $this->attributes)) {
throw new InvalidArgumentException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
* @param string The attribute name
* @param mixed The attribute value
*/
- public function offsetSet($name, $value)
+ public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
*
* @param string The attribute name
*/
- public function offsetUnset($name)
+ public function removeAttribute($name)
{
unset($this->attributes[$name]);
}
*
* @return Boolean true if the node with the given name exists, false otherwise
*/
- public function __isset($name)
+ public function hasNode($name)
{
return array_key_exists($name, $this->nodes);
}
*
* @return Twig_Node A Twig_Node instance
*/
- public function __get($name)
+ public function getNode($name)
{
if (!array_key_exists($name, $this->nodes)) {
throw new InvalidArgumentException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
* @param string The node name
* @param Twig_Node A Twig_Node instance
*/
- public function __set($name, $node = null)
+ public function setNode($name, $node = null)
{
$this->nodes[$name] = $node;
}
*
* @param string The node name
*/
- public function __unset($name)
+ public function removeNode($name)
{
unset($this->nodes[$name]);
}
*/
public function compile($compiler)
{
- $compiler->subcompile($this->body);
+ $compiler->subcompile($this->getNode('body'));
}
}
{
$compiler
->addDebugInfo($this)
- ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this['name']), "{\n")
+ ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")
->indent()
;
$compiler
- ->subcompile($this->body)
+ ->subcompile($this->getNode('body'))
->outdent()
->write("}\n\n")
;
{
$compiler
->addDebugInfo($this)
- ->write(sprintf("\$this->getBlock('%s', \$context, \$blocks);\n", $this['name']))
+ ->write(sprintf("\$this->getBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')))
;
}
}
*/
public function compile($compiler)
{
- $compiler->raw(sprintf('$context[\'%s\']', $this['name']));
+ $compiler->raw(sprintf('$context[\'%s\']', $this->getAttribute('name')));
}
}
{
$compiler
->raw('(')
- ->subcompile($this->left)
+ ->subcompile($this->getNode('left'))
->raw(') ')
;
$this->operator($compiler);
$compiler
->raw(' (')
- ->subcompile($this->right)
+ ->subcompile($this->getNode('right'))
->raw(')')
;
}
public function compile($compiler)
{
- if ('in' === $this->ops->{0}['value']) {
+ if ('in' === $this->getNode('ops')->getNode('0')->getAttribute('value')) {
return $this->compileIn($compiler);
}
- $this->expr->compile($compiler);
+ $this->getNode('expr')->compile($compiler);
- $nbOps = count($this->ops);
+ $nbOps = count($this->getNode('ops'));
for ($i = 0; $i < $nbOps; $i += 2) {
if ($i > 0) {
$compiler->raw(' && ($tmp'.($i / 2));
}
- $compiler->raw(' '.$this->ops->{$i}['value'].' ');
+ $compiler->raw(' '.$this->getNode('ops')->getNode($i)->getAttribute('value').' ');
if ($i != $nbOps - 2) {
$compiler
->raw('($tmp'.(($i / 2) + 1).' = ')
- ->subcompile($this->ops->{($i + 1)})
+ ->subcompile($this->getNode('ops')->getNode($i + 1))
->raw(')')
;
} else {
- $compiler->subcompile($this->ops->{($i + 1)});
+ $compiler->subcompile($this->getNode('ops')->getNode($i + 1));
}
}
{
$compiler
->raw('twig_in_filter(')
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(', ')
- ->subcompile($this->ops->{1})
+ ->subcompile($this->getNode('ops')->getNode(1))
->raw(')')
;
}
{
$compiler
->raw('(')
- ->subcompile($this->expr1)
+ ->subcompile($this->getNode('expr1'))
->raw(') ? (')
- ->subcompile($this->expr2)
+ ->subcompile($this->getNode('expr2'))
->raw(') : (')
- ->subcompile($this->expr3)
+ ->subcompile($this->getNode('expr3'))
->raw(')')
;
}
public function compile($compiler)
{
- $compiler->repr($this['value']);
+ $compiler->repr($this->getAttribute('value'));
}
}
*/
public function compile($compiler)
{
- $compiler->raw(sprintf("\$this->env->getExtension('%s')", $this['name']));
+ $compiler->raw(sprintf("\$this->env->getExtension('%s')", $this->getAttribute('name')));
}
}
$filterMap = $compiler->getEnvironment()->getFilters();
$postponed = array();
- for ($i = count($this->filters) - 1; $i >= 0; $i -= 2) {
- $name = $this->filters->{$i - 1}['value'];
- $attrs = $this->filters->{$i};
+ for ($i = count($this->getNode('filters')) - 1; $i >= 0; $i -= 2) {
+ $name = $this->getNode('filters')->getNode($i - 1)->getAttribute('value');
+ $attrs = $this->getNode('filters')->getNode($i);
if (!isset($filterMap[$name])) {
throw new Twig_SyntaxError(sprintf('The filter "%s" does not exist', $name), $this->getLine());
} else {
$postponed[] = $attrs;
}
- $this->node->compile($compiler);
+ $this->getNode('node')->compile($compiler);
foreach (array_reverse($postponed) as $attributes) {
foreach ($attributes as $node) {
public function prependFilter(Twig_Node_Expression_Constant $name, Twig_Node $end)
{
$filters = array($name, $end);
- foreach ($this->filters as $node) {
+ foreach ($this->getNode('filters') as $node) {
$filters[] = $node;
}
- $this->filters = new Twig_Node($filters, array(), $this->filters->getLine());
+ $this->setNode('filters', new Twig_Node($filters, array(), $this->getNode('filters')->getLine()));
}
public function appendFilter(Twig_Node_Expression_Constant $name, Twig_Node $end)
{
$filters = array();
- foreach ($this->filters as $node) {
+ foreach ($this->getNode('filters') as $node) {
$filters[] = $node;
}
$filters[] = $name;
$filters[] = $end;
- $this->filters = new Twig_Node($filters, array(), $this->filters->getLine());
+ $this->setNode('filters', new Twig_Node($filters, array(), $this->getNode('filters')->getLine()));
}
public function appendFilters(Twig_NodeInterface $filters)
{
for ($i = 0; $i < count($filters); $i += 2) {
- $this->appendFilter($filters->{$i}, $filters->{$i + 1});
+ $this->appendFilter($filters->getNode($i), $filters->getNode($i + 1));
}
}
public function hasFilter($name)
{
- for ($i = 0; $i < count($this->filters); $i += 2) {
- if ($name == $this->filters->{$i}['value']) {
+ for ($i = 0; $i < count($this->getNode('filters')); $i += 2) {
+ if ($name == $this->getNode('filters')->getNode($i)->getAttribute('value')) {
return true;
}
}
{
$compiler
->raw('$this->getAttribute(')
- ->subcompile($this->node)
+ ->subcompile($this->getNode('node'))
->raw(', ')
- ->subcompile($this->attribute)
+ ->subcompile($this->getNode('attribute'))
->raw(', array(')
;
- foreach ($this->arguments as $node) {
+ foreach ($this->getNode('arguments') as $node) {
$compiler
->subcompile($node)
->raw(', ')
$compiler
->raw('), ')
- ->repr($this['type'])
+ ->repr($this->getAttribute('type'))
->raw(')');
}
}
public function compile($compiler)
{
- if ('_self' === $this['name']) {
+ if ('_self' === $this->getAttribute('name')) {
$compiler->raw('$this');
- } elseif ('_context' === $this['name']) {
+ } elseif ('_context' === $this->getAttribute('name')) {
$compiler->raw('$context');
} elseif ($compiler->getEnvironment()->isStrictVariables()) {
- $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this['name'], $this['name']));
+ $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this->getAttribute('name'), $this->getAttribute('name')));
} else {
- $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this['name'], $this['name']));
+ $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->getAttribute('name'), $this->getAttribute('name')));
}
}
}
public function compile($compiler)
{
$testMap = $compiler->getEnvironment()->getTests();
- if (!isset($testMap[$this['name']])) {
- throw new Twig_SyntaxError(sprintf('The test "%s" does not exist', $this['name']), $this->getLine());
+ if (!isset($testMap[$this->getAttribute('name')])) {
+ throw new Twig_SyntaxError(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
$compiler
- ->raw($testMap[$this['name']]->compile().'(')
- ->subcompile($this->node)
+ ->raw($testMap[$this->getAttribute('name')]->compile().'(')
+ ->subcompile($this->getNode('node'))
;
- if (null !== $this->arguments) {
+ if (null !== $this->getNode('arguments')) {
$compiler->raw(', ');
- $max = count($this->arguments) - 1;
- foreach ($this->arguments as $i => $node) {
+ $max = count($this->getNode('arguments')) - 1;
+ foreach ($this->getNode('arguments') as $i => $node) {
$compiler->subcompile($node);
if ($i != $max) {
$compiler->raw('(');
$this->operator($compiler);
$compiler
- ->subcompile($this->node)
+ ->subcompile($this->getNode('node'))
->raw(')')
;
}
->write('$context[\'_parent\'] = (array) $context;'."\n")
;
- if (!is_null($this->else)) {
+ if (!is_null($this->getNode('else'))) {
$compiler->write("\$context['_iterated'] = false;\n");
}
$compiler
->write("\$context['_seq'] = twig_iterator_to_array(")
- ->subcompile($this->seq)
+ ->subcompile($this->getNode('seq'))
->raw(");\n")
;
- if ($this['with_loop']) {
+ if ($this->getAttribute('with_loop')) {
$compiler
->write("\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);\n")
->write("\$length = \$countable ? count(\$context['_seq']) : null;\n")
$compiler
->write("foreach (\$context['_seq'] as ")
- ->subcompile($this->key_target)
+ ->subcompile($this->getNode('key_target'))
->raw(" => ")
- ->subcompile($this->value_target)
+ ->subcompile($this->getNode('value_target'))
->raw(") {\n")
->indent()
;
- if (!is_null($this->else)) {
+ if (!is_null($this->getNode('else'))) {
$compiler->write("\$context['_iterated'] = true;\n");
}
- $compiler->subcompile($this->body);
+ $compiler->subcompile($this->getNode('body'));
- if ($this['with_loop']) {
+ if ($this->getAttribute('with_loop')) {
$compiler
->write("++\$context['loop']['index0'];\n")
->write("++\$context['loop']['index'];\n")
->write("}\n")
;
- if (!is_null($this->else)) {
+ if (!is_null($this->getNode('else'))) {
$compiler
->write("if (!\$context['_iterated']) {\n")
->indent()
- ->subcompile($this->else)
+ ->subcompile($this->getNode('else'))
->outdent()
->write("}\n")
;
$compiler->write('$_parent = $context[\'_parent\'];'."\n");
// remove some "private" loop variables (needed for nested loops)
- $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->key_target['name'].'\'], $context[\''.$this->value_target['name'].'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
+ $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
/// keep the values set in the inner context for variables defined in the outer context
$compiler->write('$context = array_merge($_parent, array_intersect_key($context, $_parent));'."\n");
public function compile($compiler)
{
$compiler->addDebugInfo($this);
- for ($i = 0; $i < count($this->tests); $i += 2) {
+ for ($i = 0; $i < count($this->getNode('tests')); $i += 2) {
if ($i > 0) {
$compiler
->outdent()
}
$compiler
- ->subcompile($this->tests->{$i})
+ ->subcompile($this->getNode('tests')->getNode($i))
->raw(") {\n")
->indent()
- ->subcompile($this->tests->{($i + 1)})
+ ->subcompile($this->getNode('tests')->getNode($i + 1))
;
}
- if (isset($this->else) && null !== $this->else) {
+ if ($this->hasNode('else') && null !== $this->getNode('else')) {
$compiler
->outdent()
->write("} else {\n")
->indent()
- ->subcompile($this->else)
+ ->subcompile($this->getNode('else'))
;
}
$compiler
->addDebugInfo($this)
->write('')
- ->subcompile($this->var)
+ ->subcompile($this->getNode('var'))
->raw(' = ')
;
- if ($this->expr instanceof Twig_Node_Expression_Name && '_self' === $this->expr['name']) {
+ if ($this->getNode('expr') instanceof Twig_Node_Expression_Name && '_self' === $this->getNode('expr')->getAttribute('name')) {
$compiler->raw("\$this");
} else {
$compiler
->raw('$this->env->loadTemplate(')
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(", true)")
;
}
{
$compiler->addDebugInfo($this);
- if ($this->expr instanceof Twig_Node_Expression_Constant) {
+ if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
$compiler
->write("\$this->env->loadTemplate(")
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(")->display(")
;
} else {
$compiler
->write("\$template = ")
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(";\n")
->write("if (!\$template")
->raw(" instanceof Twig_Template) {\n")
;
}
- if (null === $this->variables) {
+ if (null === $this->getNode('variables')) {
$compiler->raw('$context');
} else {
- $compiler->subcompile($this->variables);
+ $compiler->subcompile($this->getNode('variables'));
}
$compiler->raw(");\n");
public function compile($compiler)
{
$arguments = array();
- foreach ($this->arguments as $argument) {
- $arguments[] = '$'.$argument['name'].' = null';
+ foreach ($this->getNode('arguments') as $argument) {
+ $arguments[] = '$'.$argument->getAttribute('name').' = null';
}
$compiler
->addDebugInfo($this)
- ->write(sprintf("public function get%s(%s)\n", $this['name'], implode(', ', $arguments)), "{\n")
+ ->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
->indent()
->write("\$context = array(\n")
->indent()
;
- foreach ($this->arguments as $argument) {
+ foreach ($this->getNode('arguments') as $argument) {
$compiler
->write('')
- ->string($argument['name'])
- ->raw(' => $'.$argument['name'])
+ ->string($argument->getAttribute('name'))
+ ->raw(' => $'.$argument->getAttribute('name'))
->raw(",\n")
;
}
$compiler
->outdent()
->write(");\n\n")
- ->subcompile($this->body)
+ ->subcompile($this->getNode('body'))
->outdent()
->write("}\n\n")
;
{
$this->compileClassHeader($compiler);
- if (count($this->blocks)) {
+ if (count($this->getNode('blocks'))) {
$this->compileConstructor($compiler);
}
$this->compileDisplayFooter($compiler);
- $compiler->subcompile($this->blocks);
+ $compiler->subcompile($this->getNode('blocks'));
$this->compileMacros($compiler);
protected function compileGetParent($compiler)
{
- if (null === $this->parent) {
+ if (null === $this->getNode('parent')) {
return;
}
->indent();
;
- if ($this->parent instanceof Twig_Node_Expression_Constant) {
+ if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
$compiler
->write("\$this->parent = \$this->env->loadTemplate(")
- ->subcompile($this->parent)
+ ->subcompile($this->getNode('parent'))
->raw(");\n")
;
} else {
$compiler
->write("\$this->parent = ")
- ->subcompile($this->parent)
+ ->subcompile($this->getNode('parent'))
->raw(";\n")
->write("if (!\$this->parent")
->raw(" instanceof Twig_Template) {\n")
protected function compileDisplayBody($compiler)
{
- if (null !== $this->parent) {
+ if (null !== $this->getNode('parent')) {
// remove all but import nodes
- foreach ($this->body as $node) {
+ foreach ($this->getNode('body') as $node) {
if ($node instanceof Twig_Node_Import) {
$compiler->subcompile($node);
}
->write("\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));\n")
;
} else {
- $compiler->subcompile($this->body);
+ $compiler->subcompile($this->getNode('body'));
}
}
$compiler
->write("<?php\n\n")
// if the filename contains */, add a blank to avoid a PHP parse error
- ->write("/* ".str_replace('*/', '* /', $this['filename'])." */\n")
- ->write('class '.$compiler->getEnvironment()->getTemplateClass($this['filename']))
+ ->write("/* ".str_replace('*/', '* /', $this->getAttribute('filename'))." */\n")
+ ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename')))
->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
->write("{\n")
->indent()
;
- if (null !== $this->parent) {
+ if (null !== $this->getNode('parent')) {
$compiler->write("protected \$parent;\n\n");
}
}
->indent()
;
- foreach ($this->blocks as $name => $node) {
+ foreach ($this->getNode('blocks') as $name => $node) {
$compiler
->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name))
;
protected function compileMacros($compiler)
{
- $compiler->subcompile($this->macros);
+ $compiler->subcompile($this->getNode('macros'));
}
}
$compiler
->addDebugInfo($this)
->write("\$this->getParentBlock(")
- ->string($this['name'])
+ ->string($this->getAttribute('name'))
->raw(", \$context, \$blocks);\n")
;
}
$compiler
->addDebugInfo($this)
->write('echo ')
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(";\n")
;
}
->write("\$sandbox->enableSandbox();\n")
->outdent()
->write("}\n")
- ->subcompile($this->body)
+ ->subcompile($this->getNode('body'))
->write("if (!\$alreadySandboxed) {\n")
->indent()
->write("\$sandbox->disableSandbox();\n")
public function __construct(Twig_Node_Module $node, array $usedFilters, array $usedTags)
{
- parent::__construct($node->body, $node->parent, $node->blocks, $node->macros, $node['filename'], $node->getLine(), $node->getNodeTag());
+ parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag());
$this->usedFilters = $usedFilters;
$this->usedTags = $usedTags;
protected function compileDisplayBody($compiler)
{
- if (null === $this->parent) {
+ if (null === $this->getNode('parent')) {
$compiler->write("\$this->checkSecurity();\n");
}
->write(");\n")
;
- if (null !== $this->parent) {
+ if (null !== $this->getNode('parent')) {
$compiler
->raw("\n")
->write("\$this->parent->checkSecurity();\n")
{
public function __construct(Twig_Node_Print $node)
{
- parent::__construct($node->expr, $node->getLine(), $node->getNodeTag());
+ parent::__construct($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
}
/**
$compiler
->addDebugInfo($this)
->write('if ($this->env->hasExtension(\'sandbox\') && is_object(')
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(')) {'."\n")
->indent()
->write('$this->env->getExtension(\'sandbox\')->checkMethodAllowed(')
- ->subcompile($this->expr)
+ ->subcompile($this->getNode('expr'))
->raw(', \'__toString\');'."\n")
->outdent()
->write('}'."\n")
{
$compiler->addDebugInfo($this);
- if (count($this->names) > 1) {
+ if (count($this->getNode('names')) > 1) {
$compiler->write('list(');
- foreach ($this->names as $idx => $node) {
+ foreach ($this->getNode('names') as $idx => $node) {
if ($idx) {
$compiler->raw(', ');
}
}
$compiler->raw(')');
} else {
- if ($this['capture']) {
+ if ($this->getAttribute('capture')) {
$compiler
->write("ob_start();\n")
- ->subcompile($this->values)
+ ->subcompile($this->getNode('values'))
;
}
- $compiler->subcompile($this->names, false);
+ $compiler->subcompile($this->getNode('names'), false);
- if ($this['capture']) {
+ if ($this->getAttribute('capture')) {
$compiler->raw(" = ob_get_clean()");
}
}
- if (!$this['capture']) {
+ if (!$this->getAttribute('capture')) {
$compiler->raw(' = ');
- if (count($this->names) > 1) {
+ if (count($this->getNode('names')) > 1) {
$compiler->write('array(');
- foreach ($this->values as $idx => $value) {
+ foreach ($this->getNode('values') as $idx => $value) {
if ($idx) {
$compiler->raw(', ');
}
}
$compiler->raw(')');
} else {
- $compiler->subcompile($this->values);
+ $compiler->subcompile($this->getNode('values'));
}
}
$compiler
->addDebugInfo($this)
->write('echo ')
- ->string($this['data'])
+ ->string($this->getAttribute('data'))
->raw(";\n")
;
}
{
$compiler->addDebugInfo($this);
- list($msg, $vars) = $this->compileString($this->body);
+ list($msg, $vars) = $this->compileString($this->getNode('body'));
- if (null !== $this->plural) {
- list($msg1, $vars1) = $this->compileString($this->plural);
+ if (null !== $this->getNode('plural')) {
+ list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
$vars = array_merge($vars, $vars1);
}
- $function = null === $this->plural ? 'gettext' : 'ngettext';
+ $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
if ($vars) {
$compiler
->subcompile($msg)
;
- if (null !== $this->plural) {
+ if (null !== $this->getNode('plural')) {
$compiler
->raw(', ')
->subcompile($msg1)
->raw(', abs(')
- ->subcompile($this->count)
+ ->subcompile($this->getNode('count'))
->raw(')')
;
}
$compiler->raw('), array(');
foreach ($vars as $var) {
- if ('count' === $var['name']) {
+ if ('count' === $var->getAttribute('name')) {
$compiler
->string('%count%')
->raw(' => abs(')
- ->subcompile($this->count)
+ ->subcompile($this->getNode('count'))
->raw('), ')
;
} else {
$compiler
- ->string('%'.$var['name'].'%')
+ ->string('%'.$var->getAttribute('name').'%')
->raw(' => ')
->subcompile($var)
->raw(', ')
->subcompile($msg)
;
- if (null !== $this->plural) {
+ if (null !== $this->getNode('plural')) {
$compiler
->raw(', ')
->subcompile($msg1)
->raw(', abs(')
- ->subcompile($this->count)
+ ->subcompile($this->getNode('count'))
->raw(')')
;
}
$vars = array();
foreach ($body as $node) {
if ($node instanceof Twig_Node_Print) {
- $n = $node->expr;
+ $n = $node->getNode('expr');
while ($n instanceof Twig_Node_Expression_Filter) {
- $n = $n->node;
+ $n = $n->getNode('node');
}
- $msg .= sprintf('%%%s%%', $n['name']);
- $vars[] = new Twig_Node_Expression_Name($n['name'], $n->getLine());
+ $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
+ $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
} else {
- $msg .= $node['data'];
+ $msg .= $node->getAttribute('data');
}
}
foreach ($node as $k => $n) {
if (false !== $n = $this->traverse($n)) {
- $node->$k = $n;
+ $node->setNode($k, $n);
} else {
- unset($node->$k);
+ $node->removeNode($k);
}
}
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_AutoEscape) {
- $this->statusStack[] = $node['value'];
+ $this->statusStack[] = $node->getAttribute('value');
} elseif ($node instanceof Twig_Node_Print) {
return $this->escapeNode($node, $env, $this->needEscaping($env));
} elseif ($node instanceof Twig_Node_Block) {
- $this->statusStack[] = isset($this->blocks[$node['name']]) ? $this->blocks[$node['name']] : $this->needEscaping($env);
+ $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
}
return $node;
if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) {
array_pop($this->statusStack);
} elseif ($node instanceof Twig_Node_BlockReference) {
- $this->blocks[$node['name']] = $this->needEscaping($env);
+ $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
}
return $node;
return $node;
}
- $expression = $node instanceof Twig_Node_Print ? $node->expr : $node;
+ $expression = $node instanceof Twig_Node_Print ? $node->getNode('expr') : $node;
if ($expression instanceof Twig_Node_Expression_Filter) {
// don't escape if the primary node of the filter is not a variable
- if (!$expression->node instanceof Twig_Node_Expression_GetAttr && !$expression->node instanceof Twig_Node_Expression_Name) {
+ if (!$expression->getNode('node') instanceof Twig_Node_Expression_GetAttr && !$expression->getNode('node') instanceof Twig_Node_Expression_Name) {
return $node;
}
// don't escape if there is already an "escaper" in the filter chain
$filterMap = $env->getFilters();
- for ($i = 0; $i < count($expression->filters); $i += 2) {
- $name = $expression->filters->{$i}['value'];
+ for ($i = 0; $i < count($expression->getNode('filters')); $i += 2) {
+ $name = $expression->getNode('filters')->getNode($i)->getAttribute('value');
if (isset($filterMap[$name]) && $filterMap[$name]->isEscaper()) {
return $node;
}
// escape
if ($expression instanceof Twig_Node_Expression_Filter) {
// escape all variables in filters arguments
- for ($i = 0; $i < count($expression->filters); $i += 2) {
- foreach ($expression->filters->{$i + 1} as $j => $n) {
- $expression->filters->{$i + 1}->{$j} = $this->escapeNode($n, $env, $type);
+ for ($i = 0; $i < count($expression->getNode('filters')); $i += 2) {
+ foreach ($expression->getNode('filters')->getNode($i + 1) as $j => $n) {
+ $expression->getNode('filters')->getNode($i + 1)->setNode($j, $this->escapeNode($n, $env, $type));
}
}
// look for filters
if ($node instanceof Twig_Node_Expression_Filter) {
- for ($i = 0; $i < count($node->filters); $i += 2) {
- $this->filters[] = $node->filters->{$i}['value'];
+ for ($i = 0; $i < count($node->getNode('filters')); $i += 2) {
+ $this->filters[] = $node->getNode('filters')->getNode($i)->getAttribute('value');
}
}
// look for simple print statements ({{ article }})
- if ($node instanceof Twig_Node_Print && $node->expr instanceof Twig_Node_Expression_Name) {
+ if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name) {
return new Twig_Node_SandboxedPrint($node);
}
}
foreach ($body as $node)
{
if (
- ($node instanceof Twig_Node_Text && !preg_match('/^\s*$/s', $node['data']))
+ ($node instanceof Twig_Node_Text && !preg_match('/^\s*$/s', $node->getAttribute('data')))
||
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference)
) {
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
if (count($targets) > 1) {
- $keyTarget = $targets->{0};
- $valueTarget = $targets->{1};
+ $keyTarget = $targets->getNode(0);
+ $valueTarget = $targets->getNode(1);
} else {
$keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
- $valueTarget = $targets->{0};
+ $valueTarget = $targets->getNode(0);
}
return new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, $lineno, $this->getTag());
$body = new Twig_Node(array(new Twig_Node_Text('foo', 0)));
$node = new Twig_Node_AutoEscape(true, $body, 0);
- $this->assertEquals($body, $node->body);
- $this->assertEquals(true, $node['value']);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals(true, $node->getAttribute('value'));
}
/**
{
$node = new Twig_Node_BlockReference('foo', 0);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$body = new Twig_Node_Text('foo', 0);
$node = new Twig_Node_Block('foo', $body, 0);
- $this->assertEquals($body, $node->body);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$elements = array('foo' => $foo = new Twig_Node_Expression_Constant('bar', 0));
$node = new Twig_Node_Expression_Array($elements, 0);
- $this->assertEquals($foo, $node->foo);
+ $this->assertEquals($foo, $node->getNode('foo'));
}
/**
{
$node = new Twig_Node_Expression_AssignName('foo', 0);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Add($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_And($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Concat($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Div($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_FloorDiv($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Mod($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Mul($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Or($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
$right = new Twig_Node_Expression_Constant(2, 0);
$node = new Twig_Node_Expression_Binary_Sub($left, $right, 0);
- $this->assertEquals($left, $node->left);
- $this->assertEquals($right, $node->right);
+ $this->assertEquals($left, $node->getNode('left'));
+ $this->assertEquals($right, $node->getNode('right'));
}
/**
), array(), 0);
$node = new Twig_Node_Expression_Compare($expr, $ops, 0);
- $this->assertEquals($expr, $node->expr);
- $this->assertEquals($ops, $node->ops);
+ $this->assertEquals($expr, $node->getNode('expr'));
+ $this->assertEquals($ops, $node->getNode('ops'));
}
/**
$expr3 = new Twig_Node_Expression_Constant(3, 0);
$node = new Twig_Node_Expression_Conditional($expr1, $expr2, $expr3, 0);
- $this->assertEquals($expr1, $node->expr1);
- $this->assertEquals($expr2, $node->expr2);
- $this->assertEquals($expr3, $node->expr3);
+ $this->assertEquals($expr1, $node->getNode('expr1'));
+ $this->assertEquals($expr2, $node->getNode('expr2'));
+ $this->assertEquals($expr3, $node->getNode('expr3'));
}
/**
{
$node = new Twig_Node_Expression_Constant('foo', 0);
- $this->assertEquals('foo', $node['value']);
+ $this->assertEquals('foo', $node->getAttribute('value'));
}
/**
), array(), 0);
$node = new Twig_Node_Expression_Filter($expr, $filters, 0);
- $this->assertEquals($expr, $node->node);
- $this->assertEquals($filters, $node->filters);
+ $this->assertEquals($expr, $node->getNode('node'));
+ $this->assertEquals($filters, $node->getNode('filters'));
}
/**
new Twig_Node(),
), array(), 0);
- $this->assertEquals($filters, $node->filters);
+ $this->assertEquals($filters, $node->getNode('filters'));
}
/**
$b,
), array(), 0);
- $this->assertEquals($filters, $node->filters);
+ $this->assertEquals($filters, $node->getNode('filters'));
}
/**
$b,
), array(), 0);
- $this->assertEquals($filters, $node->filters);
+ $this->assertEquals($filters, $node->getNode('filters'));
}
/**
));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ARRAY, 0);
- $this->assertEquals($expr, $node->node);
- $this->assertEquals($attr, $node->attribute);
- $this->assertEquals($args, $node->arguments);
- $this->assertEquals(Twig_Node_Expression_GetAttr::TYPE_ARRAY, $node['type']);
+ $this->assertEquals($expr, $node->getNode('node'));
+ $this->assertEquals($attr, $node->getNode('attribute'));
+ $this->assertEquals($args, $node->getNode('arguments'));
+ $this->assertEquals(Twig_Node_Expression_GetAttr::TYPE_ARRAY, $node->getAttribute('type'));
}
/**
{
$node = new Twig_Node_Expression_Name('foo', 0);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$expr = new Twig_Node_Expression_Constant(1, 0);
$node = new Twig_Node_Expression_Unary_Neg($expr, 0);
- $this->assertEquals($expr, $node->node);
+ $this->assertEquals($expr, $node->getNode('node'));
}
/**
$expr = new Twig_Node_Expression_Constant(1, 0);
$node = new Twig_Node_Expression_Unary_Not($expr, 0);
- $this->assertEquals($expr, $node->node);
+ $this->assertEquals($expr, $node->getNode('node'));
}
/**
$expr = new Twig_Node_Expression_Constant(1, 0);
$node = new Twig_Node_Expression_Unary_Pos($expr, 0);
- $this->assertEquals($expr, $node->node);
+ $this->assertEquals($expr, $node->getNode('node'));
}
/**
$withLoop = false;
$node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
- $this->assertEquals($keyTarget, $node->key_target);
- $this->assertEquals($valueTarget, $node->value_target);
- $this->assertEquals($seq, $node->seq);
- $this->assertEquals($body, $node->body);
- $this->assertEquals(null, $node->else);
+ $this->assertEquals($keyTarget, $node->getNode('key_target'));
+ $this->assertEquals($valueTarget, $node->getNode('value_target'));
+ $this->assertEquals($seq, $node->getNode('seq'));
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals(null, $node->getNode('else'));
- $this->assertEquals($withLoop, $node['with_loop']);
+ $this->assertEquals($withLoop, $node->getAttribute('with_loop'));
$else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
- $this->assertEquals($else, $node->else);
+ $this->assertEquals($else, $node->getNode('else'));
}
/**
$else = null;
$node = new Twig_Node_If($t, $else, 0);
- $this->assertEquals($t, $node->tests);
- $this->assertEquals(null, $node->else);
+ $this->assertEquals($t, $node->getNode('tests'));
+ $this->assertEquals(null, $node->getNode('else'));
$else = new Twig_Node_Print(new Twig_Node_Expression_Name('bar', 0), 0);
$node = new Twig_Node_If($t, $else, 0);
- $this->assertEquals($else, $node->else);
+ $this->assertEquals($else, $node->getNode('else'));
}
/**
$var = new Twig_Node_Expression_AssignName('macro', 0);
$node = new Twig_Node_Import($macro, $var, 0);
- $this->assertEquals($macro, $node->expr);
- $this->assertEquals($var, $node->var);
+ $this->assertEquals($macro, $node->getNode('expr'));
+ $this->assertEquals($var, $node->getNode('var'));
}
/**
$expr = new Twig_Node_Expression_Constant('foo.twig', 0);
$node = new Twig_Node_Include($expr, null, 0);
- $this->assertEquals(null, $node->variables);
- $this->assertEquals($expr, $node->expr);
+ $this->assertEquals(null, $node->getNode('variables'));
+ $this->assertEquals($expr, $node->getNode('expr'));
$vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
$node = new Twig_Node_Include($expr, $vars, 0);
- $this->assertEquals($vars, $node->variables);
+ $this->assertEquals($vars, $node->getNode('variables'));
}
/**
$arguments = new Twig_Node(array(new Twig_Node_Expression_Name('foo', 0)), array(), 0);
$node = new Twig_Node_Macro('foo', $body, $arguments, 0);
- $this->assertEquals($body, $node->body);
- $this->assertEquals($arguments, $node->arguments);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals($arguments, $node->getNode('arguments'));
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$filename = 'foo.twig';
$node = new Twig_Node_Module($body, $parent, $blocks, $macros, $filename);
- $this->assertEquals($body, $node->body);
- $this->assertEquals($blocks, $node->blocks);
- $this->assertEquals($macros, $node->macros);
- $this->assertEquals($parent, $node->parent);
- $this->assertEquals($filename, $node['filename']);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals($blocks, $node->getNode('blocks'));
+ $this->assertEquals($macros, $node->getNode('macros'));
+ $this->assertEquals($parent, $node->getNode('parent'));
+ $this->assertEquals($filename, $node->getAttribute('filename'));
}
/**
{
$node = new Twig_Node_Parent('foo', 0);
- $this->assertEquals('foo', $node['name']);
+ $this->assertEquals('foo', $node->getAttribute('name'));
}
/**
$expr = new Twig_Node_Expression_Constant('foo', 0);
$node = new Twig_Node_Print($expr, 0);
- $this->assertEquals($expr, $node->expr);
+ $this->assertEquals($expr, $node->getNode('expr'));
}
/**
$body = new Twig_Node_Text('foo', 0);
$node = new Twig_Node_Sandbox($body, 0);
- $this->assertEquals($body, $node->body);
+ $this->assertEquals($body, $node->getNode('body'));
}
/**
$node = new Twig_Node_Module($body, $parent, $blocks, $macros, $filename);
$node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'));
- $this->assertEquals($body, $node->body);
- $this->assertEquals($blocks, $node->blocks);
- $this->assertEquals($macros, $node->macros);
- $this->assertEquals($parent, $node->parent);
- $this->assertEquals($filename, $node['filename']);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals($blocks, $node->getNode('blocks'));
+ $this->assertEquals($macros, $node->getNode('macros'));
+ $this->assertEquals($parent, $node->getNode('parent'));
+ $this->assertEquals($filename, $node->getAttribute('filename'));
}
/**
$node = new Twig_Node_Print($expr, 0);
$node = new Twig_Node_SandboxedPrint($node);
- $this->assertEquals($expr, $node->expr);
+ $this->assertEquals($expr, $node->getNode('expr'));
}
/**
$values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0)), array(), 0);
$node = new Twig_Node_Set(false, $names, $values, 0);
- $this->assertEquals($names, $node->names);
- $this->assertEquals($values, $node->values);
- $this->assertEquals(false, $node['capture']);
+ $this->assertEquals($names, $node->getNode('names'));
+ $this->assertEquals($values, $node->getNode('values'));
+ $this->assertEquals(false, $node->getAttribute('capture'));
}
/**
{
$node = new Twig_Node_Text('foo', 0);
- $this->assertEquals('foo', $node['data']);
+ $this->assertEquals('foo', $node->getAttribute('data'));
}
/**
), array(), 0);
$node = new Twig_Node_Trans($body, $plural, $count, 0);
- $this->assertEquals($body, $node->body);
- $this->assertEquals($count, $node->count);
- $this->assertEquals($plural, $node->plural);
+ $this->assertEquals($body, $node->getNode('body'));
+ $this->assertEquals($count, $node->getNode('count'));
+ $this->assertEquals($plural, $node->getNode('plural'));
}
public function getTests()