- remove exception_handler property from Smarty class

- added Smarty's own exceptions SmartyException and SmartyCompilerException
This commit is contained in:
Uwe.Tews
2010-08-13 10:39:51 +00:00
parent cbc4205669
commit ad921936b0
17 changed files with 68 additions and 66 deletions

View File

@@ -1,3 +1,7 @@
13/08/2010
- remove exception_handler property from Smarty class
- added Smarty's own exceptions SmartyException and SmartyCompilerException
09/08/2010
- bugfix on modifier with doublequoted strings as parameter containing embedded tags

View File

@@ -193,8 +193,6 @@ class Smarty extends Smarty_Internal_Data {
public $properties = array();
// config type
public $default_config_type = 'file';
// exception handler: array('ExceptionClass','ExceptionMethod');
public $exception_handler = null;
// cached template objects
public $template_objects = null;
// check If-Modified-Since headers
@@ -247,9 +245,6 @@ class Smarty extends Smarty_Internal_Data {
mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
}
$this->start_time = microtime(true);
// set exception handler
if (!empty($this->exception_handler))
set_exception_handler($this->exception_handler);
// set default dirs
$this->template_dir = array('.' . DS . 'templates' . DS);
$this->compile_dir = '.' . DS . 'templates_c' . DS;
@@ -292,9 +287,6 @@ class Smarty extends Smarty_Internal_Data {
*/
public function __destruct()
{
// restore to previous exception handler, if any
if (!empty($this->exception_handler))
restore_exception_handler();
}
/**
@@ -467,7 +459,7 @@ class Smarty extends Smarty_Internal_Data {
$this->security_handler = new Smarty_Internal_Security_Handler($this);
$this->security = true;
} else {
throw new Exception('Property security_class is not defined');
throw new SmartyException('Property security_class is not defined');
}
}
@@ -538,7 +530,7 @@ class Smarty extends Smarty_Internal_Data {
$_name_parts = explode('_', $_plugin_name, 3);
// class name must have three parts to be valid plugin
if (count($_name_parts) < 3 || $_name_parts[0] !== 'smarty') {
throw new Exception("plugin {$plugin_name} is not a valid name format");
throw new SmartyException("plugin {$plugin_name} is not a valid name format");
return false;
}
// if type is "internal", get plugin from sysplugins
@@ -587,7 +579,7 @@ class Smarty extends Smarty_Internal_Data {
return $this->registered_filters[$type][$_filter_name] = $_plugin;
}
}
throw new Exception("{$type}filter \"{$name}\" not callable");
throw new SmartyException("{$type}filter \"{$name}\" not callable");
return false;
}
@@ -611,7 +603,7 @@ class Smarty extends Smarty_Internal_Data {
*/
public function trigger_error($error_msg, $error_type = E_USER_WARNING)
{
throw new Exception("Smarty error: $error_msg");
throw new SmartyException("Smarty error: $error_msg");
}
/**
@@ -677,10 +669,10 @@ class Smarty extends Smarty_Internal_Data {
function getRegisteredObject($name)
{
if (!isset($this->registered_objects[$name]))
throw new Exception("'$name' is not a registered object");
throw new SmartyException("'$name' is not a registered object");
if (!is_object($this->registered_objects[$name][0]))
throw new Exception("registered '$name' is not an object");
throw new SmartyException("registered '$name' is not an object");
return $this->registered_objects[$name][0];
}
@@ -740,7 +732,7 @@ class Smarty extends Smarty_Internal_Data {
$camel_func = create_function('$c', 'return "_" . strtolower($c[1]);');
// PHP4 call to constructor?
if (strtolower($name) == 'smarty') {
throw new Exception('Please use parent::__construct() to call parent constuctor');
throw new SmartyException('Please use parent::__construct() to call parent constuctor');
return false;
}
// see if this is a set/get for a property
@@ -752,7 +744,7 @@ class Smarty extends Smarty_Internal_Data {
// convert camel case to underscored name
$property_name = preg_replace_callback('/([A-Z])/', $camel_func, $property_name);
if (!property_exists($this, $property_name)) {
throw new Exception("property '$property_name' does not exist.");
throw new SmartyException("property '$property_name' does not exist.");
return false;
}
if ($first3 == 'get')
@@ -776,4 +768,11 @@ function smartyAutoload($class)
}
}
Class SmartyException extends Exception {
}
Class SmartyCompilerException extends SmartyException {
}
?>

View File

@@ -19,9 +19,9 @@
function smarty_block_php($params, $content, $smarty, &$repeat, $template)
{
if (!$smarty->allow_php_tag) {
throw new Exception("{php} is deprecated, set allow_php_tag = true to enable");
throw new SmartyException("{php} is deprecated, set allow_php_tag = true to enable");
}
eval($content);
return '';
}
?>
?>

View File

@@ -64,7 +64,7 @@ function smarty_function_html_image($params, $smarty, $template)
if (!is_array($_val)) {
$$_key = smarty_function_escape_special_chars($_val);
} else {
throw new Exception ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
@@ -78,7 +78,7 @@ function smarty_function_html_image($params, $smarty, $template)
if (!is_array($_val)) {
$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
} else {
throw new Exception ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
}
@@ -136,4 +136,4 @@ function smarty_function_html_image($params, $smarty, $template)
return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
}
?>
?>

View File

@@ -64,7 +64,7 @@ class Smarty_Internal_Cache {
return $this->smarty->cache_resource_objects[$type] = new $cache_resource_class($this->smarty);
}
else {
throw new Exception("Unable to load cache resource '{$type}'");
throw new SmartyException("Unable to load cache resource '{$type}'");
}
}
}

View File

@@ -29,7 +29,7 @@ class Smarty_Internal_Config {
$this->compiler_object = null;
// parse config resource name
if (!$this->parseConfigResourceName ($config_resource)) {
throw new Exception ("Unable to parse config resource '{$config_resource}'");
throw new SmartyException ("Unable to parse config resource '{$config_resource}'");
}
}
@@ -87,7 +87,7 @@ class Smarty_Internal_Config {
if (file_exists($this->config_resource_name))
return $this->config_resource_name;
// no tpl file found
throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
return false;
}
/**
@@ -106,7 +106,7 @@ class Smarty_Internal_Config {
{
if ($this->config_source === null) {
if ($this->readConfigSource($this) === false) {
throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
}
}
return $this->config_source;

View File

@@ -98,7 +98,7 @@ class Smarty_Internal_Config_File_Compiler {
// output parser error message
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
}
throw new Exception($error_text);
throw new SmartyCompilerException($error_text);
}
}

View File

@@ -287,7 +287,7 @@ class Smarty_Internal_Data {
return $this->smarty->global_tpl_vars[$variable];
}
if ($this->smarty->error_unassigned && $error_enable) {
throw new Exception('Undefined Smarty variable "' . $variable . '"');
throw new SmartyException('Undefined Smarty variable "' . $variable . '"');
} else {
return new Undefined_Smarty_Variable;
}
@@ -310,7 +310,7 @@ class Smarty_Internal_Data {
$_ptr = $_ptr->parent;
}
if ($this->smarty->error_unassigned) {
throw new Exception('Undefined config variable "' . $variable . '"');
throw new SmartyException('Undefined config variable "' . $variable . '"');
} else {
return '';
}
@@ -333,7 +333,7 @@ class Smarty_Internal_Data {
}
if ($this->smarty->$error_unassigned) {
throw new Exception('Undefined stream variable "' . $variable . '"');
throw new SmartyException('Undefined stream variable "' . $variable . '"');
} else {
return '';
}
@@ -407,7 +407,7 @@ class Smarty_Data extends Smarty_Internal_Data {
$this->tpl_vars[$_key] = new Smarty_variable($_val);
}
} elseif ($_parent != null) {
throw new Exception("Wrong type for template variables");
throw new SmartyException("Wrong type for template variables");
}
}
}

View File

@@ -44,7 +44,7 @@ class Smarty_Internal_Filter_Handler {
}
} else {
// nothing found, throw exception
throw new Exception("Unable to load filter {$plugin_name}");
throw new SmartyException("Unable to load filter {$plugin_name}");
}
}
}

View File

@@ -51,9 +51,9 @@ class Smarty_Internal_Register {
function block($block_tag, $block_impl, $cacheable = true, $cache_attr = array())
{
if (isset($this->smarty->registered_plugins['block'][$block_tag])) {
throw new Exception("Plugin tag \"{$block_tag}\" already registered");
throw new SmartyException("Plugin tag \"{$block_tag}\" already registered");
} elseif (!is_callable($block_impl)) {
throw new Exception("Plugin \"{$block_tag}\" not callable");
throw new SmartyException("Plugin \"{$block_tag}\" not callable");
} else {
$this->smarty->registered_plugins['block'][$block_tag] =
array($block_impl, $cacheable, $cache_attr);
@@ -70,9 +70,9 @@ class Smarty_Internal_Register {
function compilerFunction($compiler_tag, $compiler_impl, $cacheable = true)
{
if (isset($this->smarty->registered_plugins['compiler'][$compiler_tag])) {
throw new Exception("Plugin tag \"{$compiler_tag}\" already registered");
throw new SmartyException("Plugin tag \"{$compiler_tag}\" already registered");
} elseif (!is_callable($compiler_impl)) {
throw new Exception("Plugin \"{$compiler_tag}\" not callable");
throw new SmartyException("Plugin \"{$compiler_tag}\" not callable");
} else {
$this->smarty->registered_plugins['compiler'][$compiler_tag] =
array($compiler_impl, $cacheable);
@@ -90,9 +90,9 @@ class Smarty_Internal_Register {
function templateFunction($function_tag, $function_impl, $cacheable = true, $cache_attr = array())
{
if (isset($this->smarty->registered_plugins['function'][$function_tag])) {
throw new Exception("Plugin tag \"{$function_tag}\" already registered");
throw new SmartyException("Plugin tag \"{$function_tag}\" already registered");
} elseif (!is_callable($function_impl)) {
throw new Exception("Plugin \"{$function_tag}\" not callable");
throw new SmartyException("Plugin \"{$function_tag}\" not callable");
} else {
$this->smarty->registered_plugins['function'][$function_tag] =
array($function_impl, $cacheable, $cache_attr);
@@ -108,9 +108,9 @@ class Smarty_Internal_Register {
function modifier($modifier_name, $modifier_impl)
{
if (isset($this->smarty->registered_plugins['modifier'][$modifier_name])) {
throw new Exception("Plugin \"{$modifier_name}\" already registered");
throw new SmartyException("Plugin \"{$modifier_name}\" already registered");
} elseif (!is_callable($modifier_impl)) {
throw new Exception("Plugin \"{$modifier_name}\" not callable");
throw new SmartyException("Plugin \"{$modifier_name}\" not callable");
} else {
$this->smarty->registered_plugins['modifier'][$modifier_name] =
array($modifier_impl);
@@ -132,7 +132,7 @@ class Smarty_Internal_Register {
if (!empty($allowed)) {
foreach ((array)$allowed as $method) {
if (!is_callable(array($object_impl, $method))) {
throw new Exception("Undefined method '$method' in registered object");
throw new SmartyException("Undefined method '$method' in registered object");
}
}
}
@@ -140,7 +140,7 @@ class Smarty_Internal_Register {
if (!empty($block_methods)) {
foreach ((array)$block_methods as $method) {
if (!is_callable(array($object_impl, $method))) {
throw new Exception("Undefined method '$method' in registered object");
throw new SmartyException("Undefined method '$method' in registered object");
}
}
}
@@ -159,7 +159,7 @@ class Smarty_Internal_Register {
{
// test if exists
if (!class_exists($class_impl)) {
throw new Exception("Undefined class '$class_impl' in register template class");
throw new SmartyException("Undefined class '$class_impl' in register template class");
}
// register the class
$this->smarty->registered_classes[$class_name] = $class_impl;
@@ -217,7 +217,7 @@ class Smarty_Internal_Register {
array(&$function_names[0], $function_names[4])),
false);
} else {
throw new Exception("malformed function-list for '$resource_type' in register_resource");
throw new SmartyException("malformed function-list for '$resource_type' in register_resource");
}
}
@@ -242,7 +242,7 @@ class Smarty_Internal_Register {
if (is_callable($function_name)) {
$this->smarty->default_plugin_handler_func = $function_name;
} else {
throw new Exception("Default plugin handler '$function_name' not callable");
throw new SmartyException("Default plugin handler '$function_name' not callable");
}
}
@@ -256,7 +256,7 @@ class Smarty_Internal_Register {
if (is_callable($function_name)) {
$this->smarty->default_template_handler_func = $function_name;
} else {
throw new Exception("Default template handler '$function_name' not callable");
throw new SmartyException("Default template handler '$function_name' not callable");
}
}
}

View File

@@ -90,7 +90,7 @@ class Smarty_Internal_Resource_Extends {
foreach ($_files as $_filepath) {
// read template file
if ($_filepath === false) {
throw new Exception("Unable to load template 'file : {$_file}'");
throw new SmartyException("Unable to load template 'file : {$_file}'");
}
if ($_filepath != $_files[0]) {
$_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));

View File

@@ -102,10 +102,10 @@ class Smarty_Internal_Resource_PHP {
public function renderUncompiled($_smarty_template)
{
if (!$this->smarty->allow_php_templates) {
throw new Exception("PHP templates are disabled");
throw new SmartyException("PHP templates are disabled");
}
if ($this->getTemplateFilepath($_smarty_template) === false) {
throw new Exception("Unable to load template \"{$_smarty_template->resource_type} : {$_smarty_template->resource_name}\"");
throw new SmartyException("Unable to load template \"{$_smarty_template->resource_type} : {$_smarty_template->resource_name}\"");
}
// prepare variables
$_smarty_ptr = $_smarty_template;
@@ -124,4 +124,4 @@ class Smarty_Internal_Resource_PHP {
}
}
?>
?>

View File

@@ -75,7 +75,7 @@ class Smarty_Internal_Security_Handler {
if (empty($this->smarty->security_policy->streams) || in_array($stream_name, $this->smarty->security_policy->streams)) {
return true;
} else {
throw new Exception ("stream '{$stream_name}' not allowed by security setting");
throw new SmartyException ("stream '{$stream_name}' not allowed by security setting");
return false;
}
}
@@ -112,7 +112,7 @@ class Smarty_Internal_Security_Handler {
}
}
throw new Exception ("directory '{$_rp}' not allowed by security setting");
throw new SmartyException ("directory '{$_rp}' not allowed by security setting");
return false;
}
/**
@@ -138,7 +138,7 @@ class Smarty_Internal_Security_Handler {
}
}
throw new Exception ("directory '{$_rp}' not allowed by security setting");
throw new SmartyException ("directory '{$_rp}' not allowed by security setting");
return false;
}
}

View File

@@ -98,7 +98,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
$this->template_resource = $template_resource;
// parse resource name
if (!$this->parseResourceName ($template_resource, $this->resource_type, $this->resource_name, $this->resource_object)) {
throw new Exception ("Unable to parse resource name \"{$template_resource}\"");
throw new SmartyException ("Unable to parse resource name \"{$template_resource}\"");
}
// load cache resource
if (!$this->resource_object->isEvaluated && ($this->caching == SMARTY_CACHING_LIFETIME_CURRENT || $this->caching == SMARTY_CACHING_LIFETIME_SAVED)) {
@@ -145,7 +145,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
{
if ($this->template_source === null) {
if (!$this->resource_object->getTemplateSource($this)) {
throw new Exception("Unable to read template {$this->resource_type} '{$this->resource_name}'");
throw new SmartyException("Unable to read template {$this->resource_type} '{$this->resource_name}'");
}
}
return $this->template_source;
@@ -164,7 +164,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
$this->isExisting = $this->resource_object->isExisting($this);
}
if (!$this->isExisting && $error) {
throw new Exception("Unable to load template {$this->resource_type} '{$this->resource_name}'");
throw new SmartyException("Unable to load template {$this->resource_type} '{$this->resource_name}'");
}
return $this->isExisting;
}
@@ -461,7 +461,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
ob_start();
$this->resource_object->renderUncompiled($this);
} else {
throw new Exception("Resource '$this->resource_type' must have 'renderUncompiled' methode");
throw new SmartyException("Resource '$this->resource_type' must have 'renderUncompiled' methode");
}
}
$this->rendered_content = ob_get_clean();
@@ -593,7 +593,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
// no tpl file found
if (!empty($this->smarty->default_template_handler_func)) {
if (!is_callable($this->smarty->default_template_handler_func)) {
throw new Exception("Default template handler not callable");
throw new SmartyException("Default template handler not callable");
} else {
$_return = call_user_func_array($this->smarty->default_template_handler_func,
array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, $this));
@@ -604,7 +604,6 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
}
}
}
// throw new Exception("Unable to load template \"{$file}\"");
return false;
}
@@ -729,7 +728,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
}
return new Smarty_Internal_Resource_Stream($this->smarty);
} else {
throw new Exception('Unkown resource type \'' . $resource_type . '\'');
throw new SmartyException('Unkown resource type \'' . $resource_type . '\'');
}
}
}
@@ -883,7 +882,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
// convert camel case to underscored name
$property_name = preg_replace_callback('/([A-Z])/', $camel_func, $property_name);
if (!property_exists($this, $property_name)) {
throw new Exception("property '$property_name' does not exist.");
throw new SmartyException("property '$property_name' does not exist.");
return false;
}
if ($first3 == 'get')

View File

@@ -183,7 +183,7 @@ class Smarty_Internal_TemplateCompilerBase {
return $plugin_object->compile($args, $this);
}
}
throw new Exception("Plugin \"{$tag}\" not callable");
throw new SmartyException("Plugin \"{$tag}\" not callable");
} else {
if ($function = $this->getPlugin($tag, $plugin_type)) {
return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $tag, $function);
@@ -222,7 +222,7 @@ class Smarty_Internal_TemplateCompilerBase {
return $plugin_object->compile($args, $this);
}
}
throw new Exception("Plugin \"{$tag}\" not callable");
throw new SmartyException("Plugin \"{$tag}\" not callable");
}
}
$this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
@@ -400,7 +400,7 @@ class Smarty_Internal_TemplateCompilerBase {
// output parser error message
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
}
throw new Exception($error_text);
throw new SmartyCompilerException($error_text);
}
}

View File

@@ -114,7 +114,7 @@ class Smarty_Internal_Wrapper {
}
$func_name = implode('',$name_parts);
if(!method_exists($this->smarty,$func_name)) {
throw new Exception("unknown method '$name'");
throw new SmartyException("unknown method '$name'");
return false;
}
return call_user_func_array(array($this->smarty,$func_name),$args);

View File

@@ -31,7 +31,7 @@ class Smarty_Internal_Write_File {
if (!file_put_contents($_tmp_file, $_contents)) {
umask($old_umask);
throw new Exception("unable to write file {$_tmp_file}");
throw new SmartyException("unable to write file {$_tmp_file}");
return false;
}
// remove original file
@@ -46,4 +46,4 @@ class Smarty_Internal_Write_File {
}
}
?>
?>