- move auto load filter methods into extension

This commit is contained in:
Uwe Tews
2015-06-28 04:05:07 +02:00
parent aee07f7bba
commit 85eb95d960
4 changed files with 145 additions and 64 deletions

View File

@@ -2,6 +2,7 @@
28.06.2015
- move $smarty->enableSecurity() into Smarty_Security class
- optimize security isTrustedResourceDir()
- move auto load filter methods into extension
27.06.2015
- bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64

View File

@@ -47,43 +47,44 @@ class Smarty_Autoloader
*/
public static $rootClasses = array('Smarty' => 'Smarty.class.php', 'SmartyBC' => 'SmartyBC.class.php',);
private static $syspluginsClasses = array('smarty_config_source' => true,
'smarty_security' => true,
'smarty_cacheresource' => true,
'smarty_compiledresource' => true,
'smarty_cacheresource_custom' => true,
'smarty_cacheresource_keyvaluestore' => true,
'smarty_resource' => true,
'smarty_resource_custom' => true,
'smarty_resource_uncompiled' => true,
'smarty_resource_recompiled' => true,
'smarty_template_source' => true,
'smarty_template_compiled' => true,
'smarty_template_cached' => true,
'smarty_template_config' => true,
'smarty_data' => true,
'smarty_variable' => true,
'smarty_undefined_variable' => true,
'smartyexception' => true,
'smartycompilerexception' => true,
'smarty_internal_data' => true,
'smarty_internal_template' => true,
'smarty_internal_templatebase' => true,
'smarty_internal_resource_file' => true,
'smarty_internal_resource_extends' => true,
'smarty_internal_resource_eval' => true,
'smarty_internal_resource_string' => true,
'smarty_internal_resource_registered' => true,
'smarty_internal_extension_codeframe' => true,
'smarty_internal_extension_config' => true,
'smarty_internal_extension_filter' => true,
'smarty_internal_extension_object' => true,
'smarty_internal_extension_loadplugin' => true,
'smarty_internal_extension_clearcompiled' => true,
'smarty_internal_filter_handler' => true,
'smarty_internal_function_call_handler' => true,
'smarty_internal_cacheresource_file' => true,
'smarty_internal_write_file' => true,);
private static $syspluginsClasses = array('smarty_config_source' => true,
'smarty_security' => true,
'smarty_cacheresource' => true,
'smarty_compiledresource' => true,
'smarty_cacheresource_custom' => true,
'smarty_cacheresource_keyvaluestore' => true,
'smarty_resource' => true,
'smarty_resource_custom' => true,
'smarty_resource_uncompiled' => true,
'smarty_resource_recompiled' => true,
'smarty_template_source' => true,
'smarty_template_compiled' => true,
'smarty_template_cached' => true,
'smarty_template_config' => true,
'smarty_data' => true,
'smarty_variable' => true,
'smarty_undefined_variable' => true,
'smartyexception' => true,
'smartycompilerexception' => true,
'smarty_internal_data' => true,
'smarty_internal_template' => true,
'smarty_internal_templatebase' => true,
'smarty_internal_resource_file' => true,
'smarty_internal_resource_extends' => true,
'smarty_internal_resource_eval' => true,
'smarty_internal_resource_string' => true,
'smarty_internal_resource_registered' => true,
'smarty_internal_extension_codeframe' => true,
'smarty_internal_extension_config' => true,
'smarty_internal_extension_filter' => true,
'smarty_internal_extension_autoloadfilter' => true,
'smarty_internal_extension_object' => true,
'smarty_internal_extension_loadplugin' => true,
'smarty_internal_extension_clearcompiled' => true,
'smarty_internal_filter_handler' => true,
'smarty_internal_function_call_handler' => true,
'smarty_internal_cacheresource_file' => true,
'smarty_internal_write_file' => true,);
/**
* Registers Smarty_Autoloader backward compatible to older installations.

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.28-dev/13';
const SMARTY_VERSION = '3.1.28-dev/14';
/**
* define variable scopes
@@ -1249,12 +1249,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public function setAutoloadFilters($filters, $type = null)
{
if ($type !== null) {
$this->autoload_filters[$type] = (array) $filters;
} else {
$this->autoload_filters = (array) $filters;
}
Smarty_Internal_Extension_AutoLoadFilter::setAutoloadFilters($this, $filters, $type);
return $this;
}
@@ -1269,22 +1264,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public function addAutoloadFilters($filters, $type = null)
{
if ($type !== null) {
if (!empty($this->autoload_filters[$type])) {
$this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
} else {
$this->autoload_filters[$type] = (array) $filters;
}
} else {
foreach ((array) $filters as $key => $value) {
if (!empty($this->autoload_filters[$key])) {
$this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
} else {
$this->autoload_filters[$key] = (array) $value;
}
}
}
Smarty_Internal_Extension_AutoLoadFilter::addAutoloadFilters($this, $filters, $type);
return $this;
}
@@ -1298,10 +1278,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public function getAutoloadFilters($type = null)
{
if ($type !== null) {
return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
}
return $this->autoload_filters;
return Smarty_Internal_Extension_AutoLoadFilter::getAutoloadFilters($this, $type);
}
/**

View File

@@ -0,0 +1,102 @@
<?php
/**
* Smarty Extension AutoLoadFilter
*
* Auto load filter methods
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Extension_AutoLoadFilter
{
/**
* Valid filter types
*
* @var array
*/
static $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
/**
* Set autoload filters
*
* @param \Smarty $smarty
* @param array $filters filters to load automatically
* @param string $type "pre", "output", <20> specify the filter type to set. Defaults to none treating $filters'
* keys as the appropriate types
*/
public static function setAutoloadFilters(Smarty $smarty, $filters, $type)
{
if ($type !== null) {
self::_checkFilterType($type);
$smarty->autoload_filters[$type] = (array) $filters;
} else {
foreach ((array) $filters as $type => $value) {
self::_checkFilterType($type);
}
$smarty->autoload_filters = (array) $filters;
}
}
/**
* Add autoload filters
*
* @param \Smarty $smarty
* @param array $filters filters to load automatically
* @param string $type "pre", "output", <20> specify the filter type to set. Defaults to none treating $filters'
* keys as the appropriate types
*/
public static function addAutoloadFilters(Smarty $smarty, $filters, $type)
{
if ($type !== null) {
self::_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) {
self::_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;
}
}
}
}
/**
* Get autoload filters
*
* @param \Smarty $smarty
* @param string $type type of filter to get auto loads for. Defaults to all autoload filters
*
* @return array array( 'type1' => array( 'filter1', 'filter2', <20> ) ) or array( 'filter1', 'filter2', <20>) if $type
* was specified
*/
public static function getAutoloadFilters(Smarty $smarty, $type)
{
if ($type !== null) {
self::_checkFilterType($type);
return isset($smarty->autoload_filters[$type]) ? $smarty->autoload_filters[$type] : array();
}
return $smarty->autoload_filters;
}
/**
* 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}\"");
}
}
}