added a better error message for unknown macros

This commit is contained in:
Fabien Potencier
2019-05-18 09:01:55 +02:00
parent 60ab3e7134
commit 1153534aac
3 changed files with 30 additions and 5 deletions
+12
View File
@@ -1311,6 +1311,18 @@ function twig_capitalize_string_filter(Environment $env, $string)
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, null, $charset), $charset);
}
/**
* @internal
*/
function twig_call_macro($object, string $method, array $args, int $lineno, Source $source)
{
if (!method_exists($object, $method)) {
throw new RuntimeError(sprintf('Macro "%s" is not defined in template "%s".', substr($method, strlen('macro_')), $object->getTemplateName()), $lineno, $source);
}
return $object->$method(...$args);
}
/**
* @internal
*/
+8 -5
View File
@@ -39,11 +39,11 @@ class MethodCallExpression extends AbstractExpression
}
$compiler
->raw('$macros[')
->raw('twig_call_macro($macros[')
->repr($this->getNode('node')->getAttribute('name'))
->raw(']->')
->raw($this->getAttribute('method'))
->raw('(')
->raw('], ')
->repr($this->getAttribute('method'))
->raw(', [')
;
$first = true;
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
@@ -54,7 +54,10 @@ class MethodCallExpression extends AbstractExpression
$compiler->subcompile($pair['value']);
}
$compiler->raw(')');
$compiler
->raw('], ')
->repr($this->getTemplateLine())
->raw(', $this->getSourceContext())');
}
}
@@ -0,0 +1,10 @@
--TEST--
macro
--TEMPLATE--
{% import _self as macros %}
{{ macros.unknown() }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Macro "unknown" is not defined in template "index.twig" in "index.twig" at line 4.