mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 10:26:32 +00:00
Merge branch '1.x' into 2.x
* 1.x: changed context access to use the PHP 7 null coalescing operator when available renamed blockExists to hasBlock
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
* 1.28.0 (2016-XX-XX)
|
||||
|
||||
* changed context access to use the PHP 7 null coalescing operator when available
|
||||
* added the "with" tag
|
||||
* added support for a custom template on the block() function
|
||||
* added "is defined" support for block() and constant()
|
||||
|
||||
@@ -31,7 +31,7 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
|
||||
{
|
||||
if ($this->getAttribute('is_defined_test')) {
|
||||
$compiler
|
||||
->raw('$this->blockExists(')
|
||||
->raw('$this->hasBlock(')
|
||||
->subcompile($this->getNode('name'))
|
||||
->raw(', $context, $blocks)')
|
||||
;
|
||||
|
||||
@@ -44,13 +44,22 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
|
||||
;
|
||||
} else {
|
||||
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
|
||||
$compiler
|
||||
->raw('(isset($context[')
|
||||
->string($name)
|
||||
->raw(']) ? $context[')
|
||||
->string($name)
|
||||
->raw('] : null)')
|
||||
;
|
||||
if (PHP_VERSION_ID >= 70000) {
|
||||
// use PHP 7 null coalescing operator
|
||||
$compiler
|
||||
->raw('($context[')
|
||||
->string($name)
|
||||
->raw('] ?? null)')
|
||||
;
|
||||
} else {
|
||||
$compiler
|
||||
->raw('(isset($context[')
|
||||
->string($name)
|
||||
->raw(']) ? $context[')
|
||||
->string($name)
|
||||
->raw('] : null)')
|
||||
;
|
||||
}
|
||||
} else {
|
||||
// When Twig will require PHP 7.0, the Template::notFound() method
|
||||
// will be removed and the code inlined like this:
|
||||
|
||||
+26
-52
@@ -239,29 +239,40 @@ abstract class Twig_Template
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a block exists or not.
|
||||
* Returns whether a block exists or not in the current context of the template.
|
||||
*
|
||||
* This method is for internal use only and should never be called
|
||||
* directly.
|
||||
* This method checks blocks defined in the current template
|
||||
* or defined in "used" traits or defined in parent templates.
|
||||
*
|
||||
* This method does only return blocks defined in the current template
|
||||
* or defined in "used" traits.
|
||||
*
|
||||
* It does not return blocks from parent templates as the parent
|
||||
* template name can be dynamic, which is only known based on the
|
||||
* current context.
|
||||
*
|
||||
* @param string $name The block name
|
||||
* @param string $name The block name
|
||||
* @param array $context The context
|
||||
* @param array $blocks The current set of blocks
|
||||
*
|
||||
* @return bool true if the block exists, false otherwise
|
||||
*
|
||||
* @see blockExists
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function hasBlock($name)
|
||||
public function hasBlock($name, array $context = null, array $blocks = array())
|
||||
{
|
||||
return isset($this->blocks[$name]);
|
||||
if (null === $context) {
|
||||
@trigger_error('The '.__METHOD__.' method is internal and should never be called; calling it directly is deprecated since version 1.28 and won\'t be possible anymore in 2.0.', E_USER_DEPRECATED);
|
||||
|
||||
return isset($this->blocks[(string) $name]);
|
||||
}
|
||||
|
||||
if (isset($blocks[$name])) {
|
||||
return $blocks[$name][0] instanceof self;
|
||||
}
|
||||
|
||||
if (isset($this->blocks[$name])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (false !== $parent = $this->getParent($context)) {
|
||||
return $parent->hasBlock($name, $context);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,8 +283,6 @@ abstract class Twig_Template
|
||||
*
|
||||
* @return array An array of block names
|
||||
*
|
||||
* @see hasBlock
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getBlockNames()
|
||||
@@ -320,8 +329,6 @@ abstract class Twig_Template
|
||||
*
|
||||
* @return array An array of blocks
|
||||
*
|
||||
* @see hasBlock
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getBlocks()
|
||||
@@ -602,37 +609,4 @@ abstract class Twig_Template
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a block exists or not in the current context of the template.
|
||||
*
|
||||
* This method checks blocks defined in the current template
|
||||
* or defined in "used" traits or defined in parent templates.
|
||||
*
|
||||
* @param string $name The block name
|
||||
* @param array $context The context
|
||||
* @param array $blocks The current set of blocks
|
||||
*
|
||||
* @return bool true if the block exists, false otherwise
|
||||
*
|
||||
* @see hasBlock
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function blockExists($name, array $context, array $blocks = array())
|
||||
{
|
||||
if (isset($blocks[$name])) {
|
||||
return $blocks[$name][0] instanceof self;
|
||||
}
|
||||
|
||||
if (isset($this->blocks[$name])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (false !== $parent = $this->getParent($context)) {
|
||||
return $parent->blockExists($name, $context);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,10 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
$line = $line > 0 ? "// line {$line}\n" : '';
|
||||
|
||||
if (PHP_VERSION_ID >= 70000) {
|
||||
return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
|
||||
}
|
||||
|
||||
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,10 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
|
||||
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true));
|
||||
$env1 = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => false));
|
||||
|
||||
$output = '(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : $this->notFound("foo", 1))';
|
||||
|
||||
return array(
|
||||
array($node, "// line 1\n".'(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : $this->notFound("foo", 1))', $env),
|
||||
array($node, "// line 1\n".$output, $env),
|
||||
array($node, $this->getVariableGetter('foo', 1), $env1),
|
||||
array($self, "// line 1\n\$this->getTemplateName()"),
|
||||
array($context, "// line 1\n\$context"),
|
||||
|
||||
Reference in New Issue
Block a user