Removed autoload filters

This commit is contained in:
Simon Wisselink
2022-11-30 10:06:09 +01:00
parent 8b5540f4f3
commit e54a53332f
21 changed files with 17 additions and 278 deletions

View File

@@ -10,8 +10,7 @@ output filters operate on the template output when it is executed.
Output filters can be either [registered](#api.register.filter) or
loaded from the [plugins directory](#variable.plugins.dir) by using the
[`loadFilter()`](#api.load.filter) method or by setting the
[`$autoload_filters`](#variable.autoload.filters) variable. Smarty will
[`loadFilter()`](#api.load.filter) method. Smarty will
pass the template output as the first argument, and expect the function
to return the result of the processing.
@@ -38,6 +37,5 @@ to return the result of the processing.
See also [`registerFilter()`](#api.register.filter),
[`loadFilter()`](#api.load.filter),
[`$autoload_filters`](#variable.autoload.filters),
[postfilters](#advanced.features.postfilters) and
[`$plugins_dir`](#variable.plugins.dir).

View File

@@ -5,8 +5,7 @@ Template postfilters are PHP functions that your templates are ran
through *after they are compiled*. Postfilters can be either
[registered](#api.register.filter) or loaded from the [plugins
directory](#variable.plugins.dir) by using the
[`loadFilter()`](#api.load.filter) function or by setting the
[`$autoload_filters`](#variable.autoload.filters) variable. Smarty will
[`loadFilter()`](#api.load.filter) function. Smarty will
pass the compiled template code as the first argument, and expect the
function to return the result of the processing.

View File

@@ -8,8 +8,7 @@ putting in their templates, etc.
Prefilters can be either [registered](#api.register.filter) or loaded
from the [plugins directory](#variable.plugins.dir) by using
[`loadFilter()`](#api.load.filter) function or by setting the
[`$autoload_filters`](#variable.autoload.filters) variable.
[`loadFilter()`](#api.load.filter) function.
Smarty will pass the template source code as the first argument, and
expect the function to return the resulting template source code.

View File

@@ -37,6 +37,5 @@ specifies the `name` of the filter plugin.
See also [`registerFilter()`](#api.register.filter),
[`$autoload_filters`](#variable.autoload.filters) and [advanced
See also [`registerFilter()`](#api.register.filter) and [advanced
features](#advanced.features).

View File

@@ -38,8 +38,7 @@ filters](#advanced.features.outputfilters) for more information on how
to set up an output filter function.
See also [`unregisterFilter()`](#api.unregister.filter),
[`loadFilter()`](#api.load.filter),
[`$autoload_filters`](#variable.autoload.filters), [template pre
[`loadFilter()`](#api.load.filter), [template pre
filters](#advanced.features.prefilters) [template post
filters](#advanced.features.postfilters) [template output
filters](#advanced.features.outputfilters) section.

View File

@@ -6,7 +6,6 @@ them directly, or use the corresponding setter/getter methods.
- [$allow_php_templates](./api-variables/variable-allow-php-templates.md)
- [$auto_literal](./api-variables/variable-auto-literal.md)
- [$autoload_filters](./api-variables/variable-autoload-filters.md)
- [$cache_dir](./api-variables/variable-cache-dir.md)
- [$cache_id](./api-variables/variable-cache-id.md)
- [$cache_lifetime](./api-variables/variable-cache-lifetime.md)

View File

@@ -1,21 +0,0 @@
\$autoload\_filters {#variable.autoload.filters}
===================
If there are some filters that you wish to load on every template
invocation, you can specify them using this variable and Smarty will
automatically load them for you. The variable is an associative array
where keys are filter types and values are arrays of the filter names.
For example:
::: {.informalexample}
<?php
$smarty->autoload_filters = array('pre' => array('trim', 'stamp'),
'output' => array('convert'));
?>
:::
See also [`registerFilter()`](#api.register.filter) and
[`loadFilter()`](#api.load.filter)

View File

@@ -177,9 +177,7 @@ class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_Com
$_content = $nextCompiler->template->source->getContent();
if ($_content !== '') {
// run pre filter if required
if ((isset($nextCompiler->smarty->autoload_filters[ 'pre' ]) ||
isset($nextCompiler->smarty->registered_filters[ 'pre' ]))
) {
if (isset($nextCompiler->smarty->registered_filters[ 'pre' ])) {
$_content = $nextCompiler->smarty->ext->_filterHandler->runFilter(
'pre',
$_content,

View File

@@ -109,18 +109,6 @@ class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_C
}
}
}
// auto loaded filters
if (isset($compiler->smarty->autoload_filters[ \Smarty\Smarty::FILTER_VARIABLE ])) {
foreach ((array)$compiler->template->smarty->autoload_filters[ \Smarty\Smarty::FILTER_VARIABLE ] as $name) {
$result = $this->compile_variable_filter($compiler, $name, $output);
if ($result !== false) {
$output = $result;
} else {
// not found, throw exception
throw new SmartyException("Unable to load variable filter '{$name}'");
}
}
}
foreach ($compiler->variable_filters as $filter) {
if (count($filter) === 1
&& ($result = $this->compile_variable_filter($compiler, $filter[ 0 ], $output)) !== false

View File

@@ -472,9 +472,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
public function postFilter($code)
{
// run post filter if on code
if (!empty($code)
&& (isset($this->smarty->autoload_filters[ 'post' ]) || isset($this->smarty->registered_filters[ 'post' ]))
) {
if (!empty($code) && isset($this->smarty->registered_filters[ 'post' ])) {
return $this->smarty->ext->_filterHandler->runFilter('post', $code, $this->template);
} else {
return $code;
@@ -492,9 +490,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
public function preFilter($_content)
{
// run pre filter if required
if ($_content !== ''
&& ((isset($this->smarty->autoload_filters[ 'pre' ]) || isset($this->smarty->registered_filters[ 'pre' ])))
) {
if ($_content !== '' && isset($this->smarty->registered_filters[ 'pre' ])) {
return $this->smarty->ext->_filterHandler->runFilter('pre', $_content, $this->template);
} else {
return $_content;

View File

@@ -216,7 +216,6 @@ class Debug extends Data
$debObj->registered_plugins = array();
$debObj->registered_resources = array();
$debObj->registered_filters = array();
$debObj->autoload_filters = array();
$debObj->default_modifiers = array();
$debObj->escape_html = true;
$debObj->caching = \Smarty::CACHING_OFF;

View File

@@ -1,53 +0,0 @@
<?php
/**
* Smarty Method AddAutoloadFilters
*
* Smarty::addAutoloadFilters() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_AddAutoloadFilters extends Smarty_Internal_Method_SetAutoloadFilters
{
/**
* Add autoload filters
*
* @api Smarty::setAutoloadFilters()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array $filters filters to load automatically
* @param string $type "pre", "output", … specify
* the filter type to set.
* Defaults to none treating
* $filters' keys as the
* appropriate types
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function addAutoloadFilters(Smarty_Internal_TemplateBase $obj, $filters, $type = null)
{
$smarty = $obj->_getSmartyObj();
if ($type !== null) {
$this->_checkFilterType($type);
if (!empty($smarty->autoload_filters[ $type ])) {
$smarty->autoload_filters[ $type ] = array_merge($smarty->autoload_filters[ $type ], (array)$filters);
} else {
$smarty->autoload_filters[ $type ] = (array)$filters;
}
} else {
foreach ((array)$filters as $type => $value) {
$this->_checkFilterType($type);
if (!empty($smarty->autoload_filters[ $type ])) {
$smarty->autoload_filters[ $type ] =
array_merge($smarty->autoload_filters[ $type ], (array)$value);
} else {
$smarty->autoload_filters[ $type ] = (array)$value;
}
}
}
return $obj;
}
}

View File

@@ -1,37 +0,0 @@
<?php
/**
* Smarty Method GetAutoloadFilters
*
* Smarty::getAutoloadFilters() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetAutoloadFilters extends Smarty_Internal_Method_SetAutoloadFilters
{
/**
* Get autoload filters
*
* @api Smarty::getAutoloadFilters()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type type of filter to get auto loads
* for. Defaults to all autoload
* filters
*
* @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type
* was specified
* @throws \SmartyException
*/
public function getAutoloadFilters(Smarty_Internal_TemplateBase $obj, $type = null)
{
$smarty = $obj->_getSmartyObj();
if ($type !== null) {
$this->_checkFilterType($type);
return isset($smarty->autoload_filters[ $type ]) ? $smarty->autoload_filters[ $type ] : array();
}
return $smarty->autoload_filters;
}
}

View File

@@ -1,72 +0,0 @@
<?php
/**
* Smarty Method SetAutoloadFilters
*
* Smarty::setAutoloadFilters() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_SetAutoloadFilters
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Valid filter types
*
* @var array
*/
private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
/**
* Set autoload filters
*
* @api Smarty::setAutoloadFilters()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array $filters filters to load automatically
* @param string $type "pre", "output", … specify
* the filter type to set.
* Defaults to none treating
* $filters' keys as the
* appropriate types
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function setAutoloadFilters(Smarty_Internal_TemplateBase $obj, $filters, $type = null)
{
$smarty = $obj->_getSmartyObj();
if ($type !== null) {
$this->_checkFilterType($type);
$smarty->autoload_filters[ $type ] = (array)$filters;
} else {
foreach ((array)$filters as $type => $value) {
$this->_checkFilterType($type);
}
$smarty->autoload_filters = (array)$filters;
}
return $obj;
}
/**
* Check if filter type is valid
*
* @param string $type
*
* @throws \SmartyException
*/
public function _checkFilterType($type)
{
if (!isset($this->filterTypes[ $type ])) {
throw new SmartyException("Illegal filter type '{$type}'");
}
}
}

View File

@@ -32,28 +32,6 @@ class Smarty_Internal_Runtime_FilterHandler
*/
public function runFilter($type, $content, Smarty_Internal_Template $template)
{
// loop over autoload filters of specified type
if (!empty($template->smarty->autoload_filters[ $type ])) {
foreach ((array)$template->smarty->autoload_filters[ $type ] as $name) {
$plugin_name = "Smarty_{$type}filter_{$name}";
if (function_exists($plugin_name)) {
$callback = $plugin_name;
} elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) {
$callback = array($plugin_name, 'execute');
} else {
if (function_exists($plugin_name)) {
// use loaded Smarty2 style plugin
$callback = $plugin_name;
} elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) {
// loaded class of filter plugin
$callback = array($plugin_name, 'execute');
} else {
throw new SmartyException("Auto load {$type}-filter plugin method '{$plugin_name}::execute' not callable");
}
}
$content = call_user_func($callback, $content, $template);
}
}
// loop over registered filters of specified type
if (!empty($template->smarty->registered_filters[ $type ])) {
foreach ($template->smarty->registered_filters[ $type ] as $key => $name) {

View File

@@ -116,9 +116,10 @@ class Smarty_Internal_Runtime_UpdateCache
$content .= $cache_parts[ 2 ][ $curr_idx ];
}
}
if (!$no_output_filter && !$_template->cached->has_nocache_code
&& (isset($_template->smarty->autoload_filters[ 'output' ])
|| isset($_template->smarty->registered_filters[ 'output' ]))
if (
!$no_output_filter
&& !$_template->cached->has_nocache_code
&& isset($_template->smarty->registered_filters[ 'output' ])
) {
$content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
}

View File

@@ -465,13 +465,6 @@ class Smarty extends \Smarty_Internal_TemplateBase
*/
public $registered_cache_resources = array();
/**
* autoload filter
*
* @var array
*/
public $autoload_filters = array();
/**
* default modifier
*

View File

@@ -46,7 +46,7 @@ class Smarty_Internal_Extension_Handler
* @var array
*/
private $_property_info = array(
'AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
'DefaultModifiers' => 0, 'ConfigVars' => 0,
'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
'TemplateVars' => 0, 'Literals' => 'Literals',
);//

View File

@@ -226,8 +226,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
);
} else {
if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled)
&& !$no_output_filter && (isset($this->smarty->autoload_filters[ 'output' ])
|| isset($this->smarty->registered_filters[ 'output' ]))
&& !$no_output_filter && isset($this->smarty->registered_filters[ 'output' ])
) {
echo $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
} else {
@@ -247,10 +246,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$this->smarty->_debug->display_debug($this, true);
}
}
if (!$no_output_filter
if (
!$no_output_filter
&& (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled)
&& (isset($this->smarty->autoload_filters[ 'output' ])
|| isset($this->smarty->registered_filters[ 'output' ]))
&& isset($this->smarty->registered_filters[ 'output' ])
) {
return $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
}

View File

@@ -19,11 +19,9 @@
* The following methods will be dynamically loaded by the extension handler when they are called.
* They are located in a corresponding Smarty_Internal_Method_xxxx class
*
* @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
* @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
* @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
* @method Smarty_Internal_TemplateBase createData(\Smarty\Data $parent = null, string $name = null)
* @method array getAutoloadFilters(string $type = null)
* @method string getDebugTemplate()
* @method array getDefaultModifier()
* @method array getLiterals()
@@ -34,7 +32,6 @@
* @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
* @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
* @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
* @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
* @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
* @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
* @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)

View File

@@ -26,26 +26,6 @@ class FilterTest extends PHPUnit_Smarty
$this->cleanDirs();
}
/**
* test autoload output filter
*/
public function testAutoloadOutputFilter()
{
$this->smarty->autoload_filters[ 'output' ] = 'trimwhitespace';
$tpl = $this->smarty->createTemplate('eval:{" <br>hello world"}');
$this->assertEquals("<br>hello world", $this->smarty->fetch($tpl));
}
/**
* test autoload variable filter
*/
public function testAutoloadVariableFilter()
{
$this->smarty->autoload_filters[ 'variable' ] = 'htmlspecialchars';
$tpl = $this->smarty->createTemplate('eval:{"<test>"}');
$this->assertEquals("&lt;test&gt;", $this->smarty->fetch($tpl));
}
/**
* test loaded filter
*/