From 159bfec4c937c02f30a4784d8988abf5e0fbf34e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 17 Nov 2016 10:26:37 -0500 Subject: [PATCH 01/13] fixed render block with template wrapper --- lib/Twig/TemplateWrapper.php | 25 +++++++++++------------- test/Twig/Tests/TemplateWrapperTest.php | 26 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/Twig/TemplateWrapper.php b/lib/Twig/TemplateWrapper.php index 643a493d4..9589bbc85 100644 --- a/lib/Twig/TemplateWrapper.php +++ b/lib/Twig/TemplateWrapper.php @@ -87,20 +87,6 @@ final class Twig_TemplateWrapper * @return string The rendered block */ public function renderBlock($name, $context = array()) - { - ob_start(); - $this->displayBlock($name, $context); - - return ob_get_clean(); - } - - /** - * Displays a template block. - * - * @param string $name The block name to render - * @param array $context An array of parameters to pass to the template - */ - public function displayBlock($name, $context = array()) { $context = $this->env->mergeGlobals($context); $level = ob_get_level(); @@ -124,6 +110,17 @@ final class Twig_TemplateWrapper return ob_get_clean(); } + /** + * Displays a template block. + * + * @param string $name The block name to render + * @param array $context An array of parameters to pass to the template + */ + public function displayBlock($name, $context = array()) + { + $this->template->displayBlock($name, $this->env->mergeGlobals($context)); + } + /** * @return Twig_Source */ diff --git a/test/Twig/Tests/TemplateWrapperTest.php b/test/Twig/Tests/TemplateWrapperTest.php index fbc4b4a20..9cd1ced9a 100644 --- a/test/Twig/Tests/TemplateWrapperTest.php +++ b/test/Twig/Tests/TemplateWrapperTest.php @@ -35,4 +35,30 @@ class Twig_Tests_TemplateWrapperTest extends PHPUnit_Framework_TestCase $this->assertTrue($wrapper->hasBlock('extended')); $this->assertEquals(array('foo', 'extended'), $wrapper->getBlockNames()); } + + public function testRenderBlock() + { + $twig = new Twig_Environment(new Twig_Loader_Array(array( + 'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}', + ))); + $twig->addGlobal('bar', 'BAR'); + + $wrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); + $this->assertEquals('FOOBAR', $wrapper->renderBlock('foo', array('foo' => 'FOO'))); + } + + public function testDisplayBlock() + { + $twig = new Twig_Environment(new Twig_Loader_Array(array( + 'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}', + ))); + $twig->addGlobal('bar', 'BAR'); + + $wrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); + + ob_start(); + $wrapper->displayBlock('foo', array('foo' => 'FOO')); + + $this->assertEquals('FOOBAR', ob_get_clean()); + } } From 60ae30368f7ac50a95de032f16c1e882b0f69813 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 17 Nov 2016 12:46:53 -0500 Subject: [PATCH 02/13] prepared the 1.28.0 release --- CHANGELOG | 2 +- ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index cb3f937dd..fac90746c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -* 1.28.0 (2016-XX-XX) +* 1.28.0 (2016-11-17) * added support for the PHP 7 null coalescing operator for the ?? Twig implementation * exposed a way to access template data and methods in a portable way diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index 3ac50ff76..cbbb7bc35 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.28.0-DEV" +#define PHP_TWIG_VERSION "1.28.0" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 369b18dcc..5d4a7f302 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,12 +16,12 @@ */ class Twig_Environment { - const VERSION = '1.28.0-DEV'; + const VERSION = '1.28.0'; const VERSION_ID = 12800; const MAJOR_VERSION = 1; const MINOR_VERSION = 28; const RELEASE_VERSION = 0; - const EXTRA_VERSION = 'DEV'; + const EXTRA_VERSION = ''; protected $charset; protected $loader; From e55069863dc5408f7323b2789a23de781c0c192e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 17 Nov 2016 12:51:52 -0500 Subject: [PATCH 03/13] bumped version to 1.28.1-DEV --- CHANGELOG | 4 ++++ ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fac90746c..d68025ce0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +* 1.28.1 (2016-XX-XX) + + * n/a + * 1.28.0 (2016-11-17) * added support for the PHP 7 null coalescing operator for the ?? Twig implementation diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index cbbb7bc35..d020dc06a 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.28.0" +#define PHP_TWIG_VERSION "1.28.1-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 5d4a7f302..09f7038c3 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,12 +16,12 @@ */ class Twig_Environment { - const VERSION = '1.28.0'; - const VERSION_ID = 12800; + const VERSION = '1.28.1-DEV'; + const VERSION_ID = 12801; const MAJOR_VERSION = 1; const MINOR_VERSION = 28; - const RELEASE_VERSION = 0; - const EXTRA_VERSION = ''; + const RELEASE_VERSION = 1; + const EXTRA_VERSION = 'DEV'; protected $charset; protected $loader; From d816d46c423a2949598d3ac9cf9b9a07c379a01f Mon Sep 17 00:00:00 2001 From: Marco Dania Date: Fri, 18 Nov 2016 11:13:19 +0100 Subject: [PATCH 04/13] fix wrong __CLASS__ constant --- lib/Twig/Template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 8eff8ae7e..6717420de 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -675,7 +675,7 @@ abstract class Twig_Template implements Twig_TemplateInterface // @deprecated in 1.28 if ($object instanceof Twig_TemplateInterface) { - @trigger_error('Using the dot notation on an instance of '.__CLASS.' is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', E_USER_DEPRECATED); + @trigger_error('Using the dot notation on an instance of '.__CLASS__.' is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', E_USER_DEPRECATED); return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset()); } From 9c9816874d2160a2b68d6d4e4f11543426a5b7eb Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 09:49:07 -0500 Subject: [PATCH 05/13] fixed typo in the docs --- doc/tags/with.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/tags/with.rst b/doc/tags/with.rst index 3b3ce25c4..815b06992 100644 --- a/doc/tags/with.rst +++ b/doc/tags/with.rst @@ -35,6 +35,8 @@ is equivalent to the following one: By default, the inner scope has access to the outer scope context; you can disable this behavior by appending the ``only`` keyword: +.. code-block:: jinja + {% set bar = 'bar' %} {% with { foo: 42 } only %} {# only foo is defined #} From 629810c91edec389fc27df53bdb539c3f5b32220 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 10:06:55 -0500 Subject: [PATCH 06/13] fixed doc issues --- doc/recipes.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/recipes.rst b/doc/recipes.rst index 0f60f21e8..9523fff9b 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -551,18 +551,18 @@ include in your templates: ``interpolateProvider`` service, for instance at the module initialization time: - ```js - angular.module('myApp', []).config(function($interpolateProvider) { - $interpolateProvider.startSymbol('{[').endSymbol(']}'); - }); - ``` + ..code-block:: javascript + + angular.module('myApp', []).config(function($interpolateProvider) { + $interpolateProvider.startSymbol('{[').endSymbol(']}'); + }); * For Twig, change the delimiters via the ``tag_variable`` Lexer option: - ```php - $env->setLexer(new Twig_Lexer($env, array( - 'tag_variable' => array('{[', ']}'), - ))); - ``` + ..code-block:: php + + $env->setLexer(new Twig_Lexer($env, array( + 'tag_variable' => array('{[', ']}'), + ))); .. _callback: http://www.php.net/manual/en/function.is-callable.php From c04d077e4ff950c2f73040ec29f1cc802dab7ae5 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 18 Nov 2016 18:16:19 +0100 Subject: [PATCH 07/13] Bugfix: When rendering blocks of other templates, don't pass the local blocks array. --- lib/Twig/Node/Expression/BlockReference.php | 26 ++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/Twig/Node/Expression/BlockReference.php b/lib/Twig/Node/Expression/BlockReference.php index 0dc533202..8aeea2808 100644 --- a/lib/Twig/Node/Expression/BlockReference.php +++ b/lib/Twig/Node/Expression/BlockReference.php @@ -50,17 +50,17 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression $this ->compileTemplateCall($compiler) - ->raw('->displayBlock(') - ->subcompile($this->getNode('name')) - ->raw(", \$context, \$blocks);\n") + ->raw('->displayBlock') ; + $this + ->compileBlockArguments($compiler) + ->raw(";\n"); } else { $this ->compileTemplateCall($compiler) - ->raw('->renderBlock(') - ->subcompile($this->getNode('name')) - ->raw(', $context, $blocks)') + ->raw('->renderBlock') ; + $this->compileBlockArguments($compiler); } } } @@ -81,4 +81,18 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression ->raw(')') ; } + + private function compileBlockArguments(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('name')) + ->raw(', $context'); + + if (!$this->hasNode('template')) { + $compiler->raw(', $blocks'); + } + + return $compiler->raw(')'); + } } From a5526f32fbaac71fcc39beeed008e7d12f1cfb12 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 15:44:11 -0500 Subject: [PATCH 08/13] fixed block() is defined call when using a template argument --- lib/Twig/Node/Expression/BlockReference.php | 43 ++++++++++----------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/Twig/Node/Expression/BlockReference.php b/lib/Twig/Node/Expression/BlockReference.php index 8aeea2808..dcb645a14 100644 --- a/lib/Twig/Node/Expression/BlockReference.php +++ b/lib/Twig/Node/Expression/BlockReference.php @@ -39,47 +39,46 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { if ($this->getAttribute('is_defined_test')) { - $compiler - ->raw('$this->hasBlock(') - ->subcompile($this->getNode('name')) - ->raw(', $context, $blocks)') + $this + ->compileTemplateCall($compiler, 'hasBlock') + ->compileBlockArguments($compiler) ; } else { if ($this->getAttribute('output')) { $compiler->addDebugInfo($this); $this - ->compileTemplateCall($compiler) - ->raw('->displayBlock') - ; - $this + ->compileTemplateCall($compiler, 'displayBlock') ->compileBlockArguments($compiler) ->raw(";\n"); } else { $this - ->compileTemplateCall($compiler) - ->raw('->renderBlock') + ->compileTemplateCall($compiler, 'renderBlock') + ->compileBlockArguments($compiler) ; - $this->compileBlockArguments($compiler); } } } - private function compileTemplateCall(Twig_Compiler $compiler) + private function compileTemplateCall(Twig_Compiler $compiler, $method) { if (!$this->hasNode('template')) { - return $compiler->write('$this'); + $compiler->write('$this'); + } else { + $compiler + ->write('$this->loadTemplate(') + ->subcompile($this->getNode('template')) + ->raw(', ') + ->repr($this->getTemplateName()) + ->raw(', ') + ->repr($this->getTemplateLine()) + ->raw(')') + ; } - return $compiler - ->write('$this->loadTemplate(') - ->subcompile($this->getNode('template')) - ->raw(', ') - ->repr($this->getTemplateName()) - ->raw(', ') - ->repr($this->getTemplateLine()) - ->raw(')') - ; + $compiler->raw(sprintf('->%s', $method)); + + return $this; } private function compileBlockArguments(Twig_Compiler $compiler) From 8c090a7cc4e43bf001b6f4364574843f1088d317 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 15:51:55 -0500 Subject: [PATCH 09/13] added tests for block() when using a template argument --- CHANGELOG | 2 +- lib/Twig/Node/Expression/BlockReference.php | 14 ++++-------- .../functions/block_with_template.test | 22 +++++++++++++++++++ .../defined_for_blocks_with_template.test | 17 ++++++++++++++ 4 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/functions/block_with_template.test create mode 100644 test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test diff --git a/CHANGELOG b/CHANGELOG index d68025ce0..9b9b4e67e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.28.1 (2016-XX-XX) - * n/a + * fixed block() function when used with a template argument * 1.28.0 (2016-11-17) diff --git a/lib/Twig/Node/Expression/BlockReference.php b/lib/Twig/Node/Expression/BlockReference.php index dcb645a14..adc63fcf0 100644 --- a/lib/Twig/Node/Expression/BlockReference.php +++ b/lib/Twig/Node/Expression/BlockReference.php @@ -39,23 +39,16 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { if ($this->getAttribute('is_defined_test')) { - $this - ->compileTemplateCall($compiler, 'hasBlock') - ->compileBlockArguments($compiler) - ; + $this->compileTemplateCall($compiler, 'hasBlock'); } else { if ($this->getAttribute('output')) { $compiler->addDebugInfo($this); $this ->compileTemplateCall($compiler, 'displayBlock') - ->compileBlockArguments($compiler) ->raw(";\n"); } else { - $this - ->compileTemplateCall($compiler, 'renderBlock') - ->compileBlockArguments($compiler) - ; + $this->compileTemplateCall($compiler, 'renderBlock'); } } } @@ -77,8 +70,9 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression } $compiler->raw(sprintf('->%s', $method)); + $this->compileBlockArguments($compiler); - return $this; + return $compiler; } private function compileBlockArguments(Twig_Compiler $compiler) diff --git a/test/Twig/Tests/Fixtures/functions/block_with_template.test b/test/Twig/Tests/Fixtures/functions/block_with_template.test new file mode 100644 index 000000000..8305eb67f --- /dev/null +++ b/test/Twig/Tests/Fixtures/functions/block_with_template.test @@ -0,0 +1,22 @@ +--TEST-- +"block" function with a template argument +--TEMPLATE-- +{{ block('foo', 'included.twig') }} +{{ block('foo', included_loaded) }} +{{ block('foo', included_loaded_internal) }} +{% set output = block('foo', 'included.twig') %} +{{ output }} +{% block foo %}NOT FOO{% endblock %} +--TEMPLATE(included.twig)-- +{% block foo %}FOO{% endblock %} +--DATA-- +return array( + 'included_loaded' => $twig->load('included.twig'), + 'included_loaded_internal' => $twig->loadTemplate('included.twig'), +) +--EXPECT-- +FOO +FOO +FOO +FOO +NOT FOO diff --git a/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test b/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test new file mode 100644 index 000000000..2c651657e --- /dev/null +++ b/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test @@ -0,0 +1,17 @@ +--TEST-- +"defined" support for blocks with a template argument +--TEMPLATE-- +{{ block('foo', 'included.twig') is defined ? 'ok' : 'ko' }} +{{ block('foo', included_loaded) is defined ? 'ok' : 'ko' }} +{{ block('foo', included_loaded_internal) is defined ? 'ok' : 'ko' }} +--TEMPLATE(included.twig)-- +{% block foo %}FOO{% endblock %} +--DATA-- +return array( + 'included_loaded' => $twig->load('included.twig'), + 'included_loaded_internal' => $twig->loadTemplate('included.twig'), +) +--EXPECT-- +ok +ok +ok From fff80c4a7ae1d47a81dfec10c76cbcb939170b45 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 21:52:49 -0800 Subject: [PATCH 10/13] prepared the 1.28.1 release --- CHANGELOG | 2 +- ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9b9b4e67e..c92c64bdf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -* 1.28.1 (2016-XX-XX) +* 1.28.1 (2016-11-18) * fixed block() function when used with a template argument diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index d020dc06a..4113fe412 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.28.1-DEV" +#define PHP_TWIG_VERSION "1.28.1" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 09f7038c3..9ef983f79 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,12 +16,12 @@ */ class Twig_Environment { - const VERSION = '1.28.1-DEV'; + const VERSION = '1.28.1'; const VERSION_ID = 12801; const MAJOR_VERSION = 1; const MINOR_VERSION = 28; const RELEASE_VERSION = 1; - const EXTRA_VERSION = 'DEV'; + const EXTRA_VERSION = ''; protected $charset; protected $loader; From 4c9603121d59a530c23305d2be7868bf5496700e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Nov 2016 21:55:27 -0800 Subject: [PATCH 11/13] bumped version to 1.28.2-DEV --- CHANGELOG | 4 ++++ ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c92c64bdf..0ae70d266 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +* 1.28.2 (2016-XX-XX) + + * n/a + * 1.28.1 (2016-11-18) * fixed block() function when used with a template argument diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index 4113fe412..b5eab0b5b 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.28.1" +#define PHP_TWIG_VERSION "1.28.2-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 9ef983f79..e55e7d6d3 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,12 +16,12 @@ */ class Twig_Environment { - const VERSION = '1.28.1'; - const VERSION_ID = 12801; + const VERSION = '1.28.2-DEV'; + const VERSION_ID = 12802; const MAJOR_VERSION = 1; const MINOR_VERSION = 28; - const RELEASE_VERSION = 1; - const EXTRA_VERSION = ''; + const RELEASE_VERSION = 2; + const EXTRA_VERSION = 'DEV'; protected $charset; protected $loader; From 0595c402d7b1969db8536fddf9f5c0d978060b70 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 19 Nov 2016 10:48:18 -0800 Subject: [PATCH 12/13] improved a deprecation notice --- composer.json | 2 +- lib/Twig/Template.php | 15 +++- phpunit.xml.dist | 8 +++ test/Twig/Tests/EnvironmentTest.php | 1 + test/Twig/Tests/NativeExtensionTest.php | 3 + test/Twig/Tests/TemplateTest.php | 91 ++++++++++++++++++++++--- 6 files changed, 105 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 7de84b5c8..c0ff2e284 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "php": ">=5.2.7" }, "require-dev": { - "symfony/phpunit-bridge": "~2.7", + "symfony/phpunit-bridge": "~3.2@dev", "symfony/debug": "~2.7" }, "autoload": { diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 6717420de..f8f25be62 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -314,8 +314,8 @@ abstract class Twig_Template implements Twig_TemplateInterface * This method checks blocks defined in the current template * or defined in "used" traits or defined in parent templates. * - * @param array $context The context - * @param array $blocks The current set of blocks + * @param array $context The context + * @param array $blocks The current set of blocks * * @return array An array of block names * @@ -675,7 +675,16 @@ abstract class Twig_Template implements Twig_TemplateInterface // @deprecated in 1.28 if ($object instanceof Twig_TemplateInterface) { - @trigger_error('Using the dot notation on an instance of '.__CLASS__.' is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', E_USER_DEPRECATED); + $self = $object->getTemplateName() === $this->getTemplateName(); + $message = sprintf('Calling "%s" on template "%s" from template "%s" is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', $method, $object->getTemplateName(), $this->getTemplateName()); + if ('renderBlock' === $method || 'displayBlock' === $method) { + $message .= sprintf(' Use block("%s"%s) instead).', $arguments[0], $self ? '' : ', template'); + } elseif ('hasBlock' === $method) { + $message .= sprintf(' Use block("%s"%s) is defined instead).', $arguments[0], $self ? '' : ', template'); + } elseif ('render' === $method || 'display' === $method) { + $message .= sprintf(' Use include("%s") instead).', $object->getTemplateName()); + } + @trigger_error($message, E_USER_DEPRECATED); return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset()); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6f6d1d25a..ce7732785 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,6 +17,14 @@ + + + + + + + + ./lib/Twig/ diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index fac0d5337..3dc6fb7f8 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -646,6 +646,7 @@ class Twig_Tests_EnvironmentTest_Runtime } } +// to be removed in 2.0 interface Twig_EnvironmentTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface { } diff --git a/test/Twig/Tests/NativeExtensionTest.php b/test/Twig/Tests/NativeExtensionTest.php index 942aff9e3..a692be1f3 100644 --- a/test/Twig/Tests/NativeExtensionTest.php +++ b/test/Twig/Tests/NativeExtensionTest.php @@ -11,6 +11,9 @@ class Twig_Tests_NativeExtensionTest extends PHPUnit_Framework_TestCase { + /** + * @requires PHP 5.3 + */ public function testGetProperties() { if (defined('HHVM_VERSION')) { diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 78f63a315..d583c87d1 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -134,8 +134,12 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeWithTemplateAsObject($useExt) { - $template = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), $useExt); - $template1 = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), false); + // to be removed in 2.0 + $twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock()); + //$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock()); + + $template = new Twig_TemplateTest($twig, $useExt, 'index.twig'); + $template1 = new Twig_TemplateTest($twig, false, 'index1.twig'); $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string')); $this->assertEquals('some_string', $template->getAttribute($template1, 'string')); @@ -168,6 +172,67 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase return $bools; } + /** + * @group legacy + * @expectedDeprecation Calling "getString" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getString" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getTrue" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getTrue" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getZero" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getZero" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getEmpty" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "getEmpty" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. + * @expectedDeprecation Calling "renderBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") instead). + * @expectedDeprecation Calling "displayBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") instead). + * @expectedDeprecation Calling "hasBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") is defined instead). + * @expectedDeprecation Calling "render" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index.twig") instead). + * @expectedDeprecation Calling "display" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index.twig") instead). + * @expectedDeprecation Calling "renderBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) instead). + * @expectedDeprecation Calling "displayBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) instead). + * @expectedDeprecation Calling "hasBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) is defined instead). + * @expectedDeprecation Calling "render" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index1.twig") instead). + * @expectedDeprecation Calling "display" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index1.twig") instead). + */ + public function testGetAttributeWithTemplateAsObjectForDeprecations() + { + // to be removed in 2.0 + $twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock()); + //$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock()); + + $template = new Twig_TemplateTest($twig, false, 'index.twig'); + $template1 = new Twig_TemplateTest($twig, false, 'index1.twig'); + + $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string')); + $this->assertEquals('some_string', $template->getAttribute($template1, 'string')); + + $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'true')); + $this->assertEquals('1', $template->getAttribute($template1, 'true')); + + $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'zero')); + $this->assertEquals('0', $template->getAttribute($template1, 'zero')); + + $this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty')); + $this->assertSame('', $template->getAttribute($template1, 'empty')); + + // trigger some deprecation notice messages to check them with @expectedDeprecation + $template->getAttribute($template, 'renderBlock', array('name', array())); + $template->getAttribute($template, 'displayBlock', array('name', array())); + $template->getAttribute($template, 'hasBlock', array('name', array())); + $template->getAttribute($template, 'render', array(array())); + $template->getAttribute($template, 'display', array(array())); + + $template->getAttribute($template1, 'renderBlock', array('name', array())); + $template->getAttribute($template1, 'displayBlock', array('name', array())); + $template->getAttribute($template1, 'hasBlock', array('name', array())); + $template->getAttribute($template1, 'render', array(array())); + $template->getAttribute($template1, 'display', array(array())); + + $this->assertFalse($template->getAttribute($template1, 'env', array(), Twig_Template::ANY_CALL, true)); + $this->assertFalse($template->getAttribute($template1, 'environment', array(), Twig_Template::ANY_CALL, true)); + $this->assertFalse($template->getAttribute($template1, 'getEnvironment', array(), Twig_Template::METHOD_CALL, true)); + $this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true)); + } + /** * @dataProvider getTestsDependingOnExtensionAvailability */ @@ -388,9 +453,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase // tests when input is not an array or object $tests = array_merge($tests, array( - array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42").'), - array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string").'), - array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty.'), + array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'), + array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string") in "index.twig".'), + array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty in "index.twig".'), )); // add twig_template_get_attributes tests @@ -410,12 +475,14 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase class Twig_TemplateTest extends Twig_Template { protected $useExtGetAttribute = false; + private $name; - public function __construct(Twig_Environment $env, $useExtGetAttribute = false) + public function __construct(Twig_Environment $env, $useExtGetAttribute = false, $name = 'index.twig') { parent::__construct($env); $this->useExtGetAttribute = $useExtGetAttribute; self::$cache = array(); + $this->name = $name; } public function getZero() @@ -440,6 +507,7 @@ class Twig_TemplateTest extends Twig_Template public function getTemplateName() { + return $this->name; } public function getDebugInfo() @@ -447,13 +515,9 @@ class Twig_TemplateTest extends Twig_Template return array(); } - public function getSource() - { - return ''; - } - protected function doGetParent(array $context) { + return false; } protected function doDisplay(array $context, array $blocks = array()) @@ -688,3 +752,8 @@ class CExtDisablingNodeVisitor implements Twig_NodeVisitorInterface return 0; } } + +// to be removed in 2.0 +interface Twig_TemplateTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface +{ +} From ed6e8972aca32f27d36f5d625b8f041723c9b94f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 19 Nov 2016 11:44:10 -0800 Subject: [PATCH 13/13] fixed message --- lib/Twig/Template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index f8f25be62..aa5e5b3b7 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -680,7 +680,7 @@ abstract class Twig_Template implements Twig_TemplateInterface if ('renderBlock' === $method || 'displayBlock' === $method) { $message .= sprintf(' Use block("%s"%s) instead).', $arguments[0], $self ? '' : ', template'); } elseif ('hasBlock' === $method) { - $message .= sprintf(' Use block("%s"%s) is defined instead).', $arguments[0], $self ? '' : ', template'); + $message .= sprintf(' Use "block("%s"%s) is defined" instead).', $arguments[0], $self ? '' : ', template'); } elseif ('render' === $method || 'display' === $method) { $message .= sprintf(' Use include("%s") instead).', $object->getTemplateName()); }