From a2c6db7ea7d2c90923e6708019ec1442526d1c30 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 6 Dec 2013 11:06:49 +0100 Subject: [PATCH] added an error message when trying to import an undefined block from a trait --- CHANGELOG | 2 +- lib/Twig/Node/Module.php | 12 ++++++++++++ .../Tests/Fixtures/exceptions/undefined_trait.test | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/exceptions/undefined_trait.test diff --git a/CHANGELOG b/CHANGELOG index 51a25a6..6d0a147 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.15.1 (2013-XX-XX) - * n/a + * added an error message when trying to import an undefined block from a trait * 1.15.0 (2013-12-06) diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 585048b..3f80175 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -170,6 +170,18 @@ class Twig_Node_Module extends Twig_Node foreach ($trait->getNode('targets') as $key => $value) { $compiler + ->write(sprintf("if (!isset(\$_trait_%s_blocks[", $i)) + ->string($key) + ->raw("])) {\n") + ->indent() + ->write("throw new Twig_Error_Runtime(sprintf('Block ") + ->string($key) + ->raw(" is not defined in trait ") + ->subcompile($trait->getNode('template')) + ->raw(".'));\n") + ->outdent() + ->write("}\n\n") + ->write(sprintf("\$_trait_%s_blocks[", $i)) ->subcompile($value) ->raw(sprintf("] = \$_trait_%s_blocks[", $i)) diff --git a/test/Twig/Tests/Fixtures/exceptions/undefined_trait.test b/test/Twig/Tests/Fixtures/exceptions/undefined_trait.test new file mode 100644 index 0000000..026bd52 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/undefined_trait.test @@ -0,0 +1,9 @@ +--TEST-- +Exception for an undefined trait +--TEMPLATE-- +{% use 'foo' with foobar as bar %} +--TEMPLATE(foo)-- +{% block bar %} +{% endblock %} +--EXCEPTION-- +Twig_Error_Runtime: Block "foobar" is not defined in trait "foo" in "index.twig". -- 1.7.2.5