Skip to content

Commit

Permalink
- improvement allow closures as callback at $smarty->registerFilter() #…
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Jun 19, 2015
1 parent 4537d8a commit afe79aa
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 74 deletions.
14 changes: 14 additions & 0 deletions NEW_FEATURES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

This file contains a brief description of new features which have been added to Smarty 3.1

Smarty 3.1.28

Filter support
==============
Optional filter names
An optional filter name was added to $smarty->registerFilter(). It can be used to unregister a filter by name.
- $smarty->registerFilter('output', $callback, 'name');
$smarty->unregister('output', 'name');

Closures
$smarty->registerFilter() does now accept closures.
- $smarty->registerFilter('pre', function($source) {return $source;});
If no optional filter name was specified it gets the default name 'closure'.

Smarty 3.1.22

Namespace support within templates
Expand Down
18 changes: 11 additions & 7 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
 ===== 3.1.27===== (18.06.2015)
18.06.2015
- bugfix another update on file path normalization failed on path containing something like "/.foo/" https://github.com/smarty-php/smarty/issues/56
 ===== 3.1.28-dev===== (xx.xx.2015)
19.06.2015
- improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59

===== 3.1.27===== (18.06.2015)
18.06.2015
- bugfix another update on file path normalization failed on path containing something like "/.foo/" https://github.com/smarty-php/smarty/issues/56

===== 3.1.26===== (18.06.2015)
18.06.2015
- bugfix file path normalization failed on path containing something like "/.foo/" https://github.com/smarty-php/smarty/issues/56
18.06.2015
- bugfix file path normalization failed on path containing something like "/.foo/" https://github.com/smarty-php/smarty/issues/56

17.06.2015
- bugfix calling a plugin with nocache option but no other attributes like {foo nocache} caused call to undefined function https://github.com/smarty-php/smarty/issues/55
17.06.2015
- bugfix calling a plugin with nocache option but no other attributes like {foo nocache} caused call to undefined function https://github.com/smarty-php/smarty/issues/55

===== 3.1.25===== (15.06.2015)
15.06.2015
Expand Down
4 changes: 2 additions & 2 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Uwe Tews
* @author Rodney Rehm
* @package Smarty
* @version 3.1.27
* @version 3.1.28-dev
*/

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.27';
const SMARTY_VERSION = '3.1.28-dev/1';

/**
* define variable scopes
Expand Down
150 changes: 150 additions & 0 deletions libs/sysplugins/smarty_internal_extension_filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* Smarty Extension Filter
*
* Register filter methods
* Load filter method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Extension_Filter
{
/**
* Valid filter types
*
* @var array
*/
static $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);

/**
* Registers a filter function
*
* @param \Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param callback $callback
* @param string|null $name optional filter name
*
* @throws \SmartyException
*/
static function registerFilter($obj, $type, $callback, $name)
{
self::_checkFilterType($type);
$name = isset($name) ? $name : self::_getFilterName($callback);
if (!is_callable($callback)) {
throw new SmartyException("{$type}filter \"{$name}\" not callable");
}
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
$smarty->registered_filters[$type][$name] = $callback;
}

/**
* Unregisters a filter function
*
* @param \Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param callback|string $callback
*
*/
static function unregisterFilter($obj, $type, $callback)
{
self::_checkFilterType($type);
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
if (isset($smarty->registered_filters[$type])) {
$name = is_string($callback) ? $callback : self::_getFilterName($callback);
if (isset($smarty->registered_filters[$type][$name])) {
unset($smarty->registered_filters[$type][$name]);
if (empty($smarty->registered_filters[$type])) {
unset($smarty->registered_filters[$type]);
}
}
}
}

/**
* load a filter of specified type and name
*
* @param \Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param string $name filter name
*
* @return bool
* @throws SmartyException if filter could not be loaded
*/
static function loadFilter($obj, $type, $name)
{
self::_checkFilterType($type);
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
$_plugin = "smarty_{$type}filter_{$name}";
$_filter_name = $_plugin;
if ($smarty->loadPlugin($_plugin)) {
if (class_exists($_plugin, false)) {
$_plugin = array($_plugin, 'execute');
}
if (is_callable($_plugin)) {
$smarty->registered_filters[$type][$_filter_name] = $_plugin;
return true;
}
}
throw new SmartyException("{$type}filter \"{$name}\" not callable");
}

/**
* unload a filter of specified type and name
*
* @param \Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param string $name filter name
*
*/
static function unloadFilter($obj, $type, $name)
{
self::_checkFilterType($type);
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
if (isset($smarty->registered_filters[$type])) {
$_filter_name = "smarty_{$type}filter_{$name}";
if (isset($smarty->registered_filters[$type][$_filter_name])) {
unset ($smarty->registered_filters[$type][$_filter_name]);
if (empty($smarty->registered_filters[$type])) {
unset($smarty->registered_filters[$type]);
}
}
}
}

/**
* Return internal filter name
*
* @param callback $function_name
*
* @return string internal filter name
*/
static function _getFilterName($function_name)
{
if (is_array($function_name)) {
$_class_name = (is_object($function_name[0]) ? get_class($function_name[0]) : $function_name[0]);

return $_class_name . '_' . $function_name[1];
} elseif (is_string($function_name)) {
return $function_name;
} else {
return 'closure';
}
}

/**
* Check if filter type is valid
*
* @param string $type
*
* @throws \SmartyException
*/
static function _checkFilterType($type)
{
if (!isset(self::$filterTypes[$type])) {
throw new SmartyException("Illegal filter type \"{$type}\"");
}
}
}
9 changes: 4 additions & 5 deletions libs/sysplugins/smarty_internal_filter_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ public static function runFilter($type, $content, Smarty_Internal_Template $temp
$output = $plugin_name($output, $template);
} elseif (class_exists($plugin_name, false)) {
// loaded class of filter plugin
if (!is_callable(array($plugin_name, 'execute'))) {
throw new SmartyException("Auto load {$type}-filter plugin method \"{$plugin_name}::execute\" not callable");
}
$output = call_user_func(array($plugin_name, 'execute'), $output, $template);
}
} else {
// nothing found, throw exception
throw new SmartyException("Unable to load filter {$plugin_name}");
throw new SmartyException("Unable to auto load {$type}-filter plugin \"{$plugin_name}\"");
}
}
}
// loop over registerd filters of specified type
if (!empty($template->smarty->registered_filters[$type])) {
foreach ($template->smarty->registered_filters[$type] as $key => $name) {
if (is_array($template->smarty->registered_filters[$type][$key])) {
$output = call_user_func($template->smarty->registered_filters[$type][$key], $output, $template);
} else {
$output = $template->smarty->registered_filters[$type][$key]($output, $template);
}
}
}
// return filtered output
Expand Down
Loading

0 comments on commit afe79aa

Please sign in to comment.