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());
+ }
}
/**
/**
* 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)
{
return $this->functions[$name];
}
- return null;
+ return false;
}
protected function loadFunctions() {
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)
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());
}
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;
}
$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());
}
$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());