{
if ('self' === $this['name']) {
$compiler->raw('$this');
- } else {
+ } elseif ($compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this['name'], $this['name']));
+ } else {
+ $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this['name'], $this['name']));
}
}
}
protected function getContext($context, $item)
{
- if (array_key_exists($item, $context)) {
- return $context[$item];
+ if (!array_key_exists($item, $context)) {
+ throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item));
}
- if (!$this->env->isStrictVariables()) {
- return null;
- }
-
- throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item));
+ return $context[$item];
}
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY)
$tests[] = array($node, <<<EOF
if (\$this->env->isDebug()) {
- var_export(\$this->getContext(\$context, 'foo'));
+ var_export((isset(\$context['foo']) ? \$context['foo'] : null));
}
EOF
);
$attr = new Twig_Node_Expression_Constant('bar', 0);
$args = new Twig_Node();
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ANY, 0);
- $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "any")');
+ $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "any")');
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ARRAY, 0);
- $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "array")');
+ $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "array")');
$args = new Twig_Node(array(
new Twig_Node_Expression_Constant('bar', 0),
));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_METHOD, 0);
- $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array($this->getContext($context, \'foo\'), "bar", ), "method")');
+ $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", ), "method")');
return $tests;
}
{
$node = new Twig_Node_Expression_Name('foo', 0);
+ $env = new Twig_Environment(null, array('strict_variables' => true));
+
return array(
- array($node, '$this->getContext($context, \'foo\')'),
+ array($node, '$this->getContext($context, \'foo\')', $env),
+ array($node, '(isset($context[\'foo\']) ? $context[\'foo\'] : null)'),
);
}
}
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'items'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['items']) ? \$context['items'] : null));
foreach (\$context['_seq'] as \$context['key'] => \$context['item']) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['values']) ? \$context['values'] : null));
\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
\$length = \$countable ? count(\$context['_seq']) : null;
\$context['loop'] = array(
\$context['loop']['last'] = 1 === \$length;
}
foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
++\$context['loop']['index0'];
++\$context['loop']['index'];
\$context['loop']['first'] = false;
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
\$context['_iterated'] = false;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['values']) ? \$context['values'] : null));
\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
\$length = \$countable ? count(\$context['_seq']) : null;
\$context['loop'] = array(
}
foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
\$context['_iterated'] = true;
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
++\$context['loop']['index0'];
++\$context['loop']['index'];
\$context['loop']['first'] = false;
}
}
if (!\$context['_iterated']) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
}
EOF
);
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
} elseif (false) {
- echo \$this->getContext(\$context, 'bar');
+ echo (isset(\$context['bar']) ? \$context['bar'] : null);
}
EOF
);
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->getContext(\$context, 'foo');
+ echo (isset(\$context['foo']) ? \$context['foo'] : null);
} else {
- echo \$this->getContext(\$context, 'bar');
+ echo (isset(\$context['bar']) ? \$context['bar'] : null);
}
EOF
);
$values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Name('bar', 0)), array(), 0);
$node = new Twig_Node_Set(false, $names, $values, 0);
$tests[] = array($node, <<<EOF
-list(\$context['foo'], \$context['bar']) = array("foo", \$this->getContext(\$context, 'bar'));
+list(\$context['foo'], \$context['bar']) = array("foo", (isset(\$context['bar']) ? \$context['bar'] : null));
EOF
);
new Twig_Node_Text(' pommes', 0),
), array(), 0);
$node = new Twig_Node_Trans(null, $body, null, 0);
- $tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => $this->getContext($context, \'foo\'), ));');
+ $tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => (isset($context[\'foo\']) ? $context[\'foo\'] : null), ));');
$count = new Twig_Node_Expression_Constant(12, 0);
$body = new Twig_Node(array(
new Twig_Node_Text(' apples', 0),
), array(), 0);
$node = new Twig_Node_Trans($count, $body, $plural, 0);
- $tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => $this->getContext($context, \'name\'), "%name%" => $this->getContext($context, \'name\'), "%count%" => abs(12), ));');
+ $tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%count%" => abs(12), ));');
return $tests;
}