From: Fabien Potencier Date: Sat, 8 Jan 2011 13:56:11 +0000 (+0100) Subject: added a way to catch undefined filters (like functions) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=57d8bf97afee79e8d3753fe88d9206f8caa0adf3;p=web%2Fkonrad%2Ftwig.git added a way to catch undefined filters (like functions) --- diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 5118656..cfe92a1 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -647,27 +647,46 @@ class Twig_Environment public function addFilter($name, Twig_FilterInterface $filter) { if (null === $this->filters) { - $this->getFilters(); + $this->loadFilters(); } $this->filters[$name] = $filter; } /** - * Gets the registered Filters. + * Get a filter by name. * - * @return Twig_FilterInterface[] An array of Twig_FilterInterface instances + * Subclasses may override this method and load filters differently; + * so no list of filters is available. + * + * @param string $name The filter name + * + * @return Twig_Filter|false A Twig_Filter instance or false if the filter does not exists */ - public function getFilters() + public function getFilter($name) { if (null === $this->filters) { - $this->filters = array(); - foreach ($this->getExtensions() as $extension) { - $this->filters = array_merge($this->filters, $extension->getFilters()); - } + $this->loadFilters(); + } + + if (isset($this->filters[$name])) { + return $this->filters[$name]; } - return $this->filters; + return false; + } + + /** + * Gets the registered Filters. + * + * @return Twig_FilterInterface[] An array of Twig_FilterInterface instances + */ + protected function loadFilters() + { + $this->filters = array(); + foreach ($this->getExtensions() as $extension) { + $this->filters = array_merge($this->filters, $extension->getFilters()); + } } /** @@ -720,12 +739,12 @@ class Twig_Environment /** * Get a function by name. * - * Subclasses may override getFunction($name) and load functions differently; + * Subclasses may override this method and load functions differently; * so no list of functions is available. * * @param string $name function name * - * @return Twig_Function|null A Twig_Function instance or null if the function does not exists + * @return Twig_Function|false A Twig_Function instance or false if the function does not exists */ public function getFunction($name) { @@ -737,7 +756,7 @@ class Twig_Environment return $this->functions[$name]; } - return null; + return false; } protected function loadFunctions() { diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index d39c364..091606a 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -18,12 +18,10 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { - $filterMap = $compiler->getEnvironment()->getFilters(); $name = $this->getNode('filter')->getAttribute('value'); - if (!isset($filterMap[$name])) { + if (false === $filter = $compiler->getEnvironment()->getFilter($name)) { throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine()); } - $filter = $filterMap[$name]; // The default filter is intercepted when the filtered value // is a name (like obj) or an attribute (like obj.attr) diff --git a/lib/Twig/Node/Expression/Function.php b/lib/Twig/Node/Expression/Function.php index 92ba789..81d243b 100644 --- a/lib/Twig/Node/Expression/Function.php +++ b/lib/Twig/Node/Expression/Function.php @@ -18,7 +18,7 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { $function = $compiler->getEnvironment()->getFunction($this->getNode('name')->getAttribute('name')); - if (!$function) { + if (false === $function) { throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine()); } diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index 8f0cc14..468f97a 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -94,11 +94,10 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env) { - $filterMap = $env->getFilters(); $name = $filter->getNode('filter')->getAttribute('value'); - if (isset($filterMap[$name])) { - $type = $filterMap[$name]->getPreEscape(); + if (false !== $f = $env->getFilter($name)) { + $type = $f->getPreEscape(); if (null === $type) { return $filter; } diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index 2f2403b..4579f4a 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -50,11 +50,10 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface $this->setSafe($node, $safe); } elseif ($node instanceof Twig_Node_Expression_Filter) { // filter expression is safe when the filter is safe - $filterMap = $env->getFilters(); $name = $node->getNode('filter')->getAttribute('value'); $args = $node->getNode('arguments'); - if (isset($filterMap[$name])) { - $this->setSafe($node, $filterMap[$name]->getSafe($args)); + if (false !== $filter = $env->getFilter($name)) { + $this->setSafe($node, $filter->getSafe($args)); } else { $this->setSafe($node, array()); } @@ -63,7 +62,7 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface $name = $node->getNode('name')->getAttribute('name'); $args = $node->getNode('arguments'); $function = $env->getFunction($name); - if (null !== $function) { + if (false !== $function) { $this->setSafe($node, $function->getSafe($args)); } else { $this->setSafe($node, array());