mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
added a way to catch undefined filters (like functions)
This commit is contained in:
+31
-12
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a filter by name.
|
||||
*
|
||||
* 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 getFilter($name)
|
||||
{
|
||||
if (null === $this->filters) {
|
||||
$this->loadFilters();
|
||||
}
|
||||
|
||||
if (isset($this->filters[$name])) {
|
||||
return $this->filters[$name];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the registered Filters.
|
||||
*
|
||||
* @return Twig_FilterInterface[] An array of Twig_FilterInterface instances
|
||||
*/
|
||||
public function getFilters()
|
||||
protected function loadFilters()
|
||||
{
|
||||
if (null === $this->filters) {
|
||||
$this->filters = array();
|
||||
foreach ($this->getExtensions() as $extension) {
|
||||
$this->filters = array_merge($this->filters, $extension->getFilters());
|
||||
}
|
||||
$this->filters = array();
|
||||
foreach ($this->getExtensions() as $extension) {
|
||||
$this->filters = array_merge($this->filters, $extension->getFilters());
|
||||
}
|
||||
|
||||
return $this->filters;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user