From 05a9c3beab4b04c6abce4f6bd68f4db88391c4d9 Mon Sep 17 00:00:00 2001 From: "monte.ohrt" Date: Mon, 8 Mar 2010 15:15:41 +0000 Subject: [PATCH] add setters/getters to created templates --- libs/sysplugins/smarty_internal_template.php | 34 +++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 60bb8a98..61603333 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -801,7 +801,39 @@ class Smarty_Internal_Template extends Smarty_Internal_Data { public function fetch () { return $this->smarty->fetch($this); - } + } + + /** + * Takes unknown class methods and lazy loads sysplugin files for them + * class name format: Smarty_Method_MethodName + * plugin filename format: method.methodname.php + * + * @param string $name unknown methode name + * @param array $args aurgument array + */ + public function __call($name, $args) + { + static $camel_func; + if (!isset($camel_func)) + $camel_func = create_function('$c', 'return "_" . strtolower($c[1]);'); + // see if this is a set/get for a property + $first3 = strtolower(substr($name, 0, 3)); + if (in_array($first3, array('set', 'get')) && substr($name, 3, 1) !== '_') { + // try to keep case correct for future PHP 6.0 case-sensitive class methods + // lcfirst() not available < PHP 5.3.0, so improvise + $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4); + // 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."); + return false; + } + if ($first3 == 'get') + return $this->$property_name; + else + return $this->$property_name = $args[0]; + } + } } /**