Skip to content

Commit

Permalink
removed code that people want to overide over and over again
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jul 3, 2015
1 parent bd8a07e commit a0261e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
33 changes: 23 additions & 10 deletions lib/Twig/Node/Expression/Name.php
Expand Up @@ -43,18 +43,31 @@ public function compile(Twig_Compiler $compiler)
->raw(']')
;
} else {
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : ')
;

if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : null)')
;
} else {
$compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
// When Twig will require PHP 7.0, the Template::notFound() method
// will be removed and the code inlined like this:
// (function () { throw new Exception(...); })();
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) || array_key_exists(')
->string($name)
->raw(', $context) ? $context[')
->string($name)
->raw('] : $this->notFound(')
->string($name)
->raw(', ')
->repr($this->lineno)
->raw('))')
;
}
}
}
Expand Down
27 changes: 5 additions & 22 deletions lib/Twig/Template.php
Expand Up @@ -347,35 +347,18 @@ protected function displayWithErrorHandling(array $context, array $blocks = arra
abstract protected function doDisplay(array $context, array $blocks = array());

/**
* Returns a variable from the context.
* Throws an exception for an unknown variable.
*
* This method is for internal use only and should never be called
* directly.
*
* This method should not be overridden in a sub-class as this is an
* implementation detail that has been introduced to optimize variable
* access for versions of PHP before 5.4. This is not a way to override
* the way to get a variable value.
* This is an implementation detail due to a PHP limitation before version 7.0.
*
* @param array $context The context
* @param string $item The variable to return from the context
* @param bool $ignoreStrictCheck Whether to ignore the strict variable check or not
*
* @return mixed The content of the context variable
*
* @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
* @internal
*/
final protected function getContext($context, $item, $ignoreStrictCheck = false)
final protected function notFound($name, $line)
{
if (!array_key_exists($item, $context)) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return;
}

throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), -1, $this->getTemplateName());
}

return $context[$item];
throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $name), $line, $this->getTemplateName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/Twig/Tests/Node/Expression/NameTest.php
Expand Up @@ -28,7 +28,7 @@ public function getTests()
$env1 = new Twig_Environment(null, array('strict_variables' => false));

return array(
array($node, "// line 1\n".'(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env),
array($node, "// line 1\n".'(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : $this->notFound("foo", 1))', $env),
array($node, $this->getVariableGetter('foo', 1), $env1),
array($self, "// line 1\n\$this"),
array($context, "// line 1\n\$context"),
Expand Down

0 comments on commit a0261e7

Please sign in to comment.