mirror of
https://github.com/smarty-php/smarty.git
synced 2026-05-04 11:44:16 +02:00
Improve extension handler
This commit is contained in:
@@ -47,9 +47,9 @@ class Smarty_Internal_Extension_Handler
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
|
||||
'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
|
||||
'TemplateVars' => 0,);#
|
||||
private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
|
||||
'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
|
||||
'TemplateVars' => 0, 'Literals' => 'Literals',);#
|
||||
|
||||
private $resolvedProperties = array();
|
||||
|
||||
@@ -68,38 +68,62 @@ class Smarty_Internal_Extension_Handler
|
||||
/* @var Smarty $data ->smarty */
|
||||
$smarty = isset($data->smarty) ? $data->smarty : $data;
|
||||
if (!isset($smarty->ext->$name)) {
|
||||
$class = 'Smarty_Internal_Method_' . $this->upperCase($name);
|
||||
if (preg_match('/^(set|get)([A-Z].*)$/', $name, $match)) {
|
||||
$pn = '';
|
||||
if (!isset($this->_property_info[ $prop = $match[ 2 ] ])) {
|
||||
// convert camel case to underscored name
|
||||
$this->resolvedProperties[ $prop ] = $pn = strtolower(join('_',
|
||||
preg_split('/([A-Z][^A-Z]*)/', $prop,
|
||||
- 1, PREG_SPLIT_NO_EMPTY |
|
||||
PREG_SPLIT_DELIM_CAPTURE)));
|
||||
$this->_property_info[ $prop ] =
|
||||
property_exists($data, $pn) ? 1 : ($data->_isTplObj() && property_exists($smarty, $pn) ? 2 : 0);
|
||||
if (preg_match('/^((set|get)|(.*?))([A-Z].*)$/', $name, $match)) {
|
||||
$basename = $this->upperCase($match[4]);
|
||||
if (!isset($smarty->ext->$basename) && isset($this->_property_info[ $basename ]) &&
|
||||
is_string($this->_property_info[ $basename ])) {
|
||||
$class = 'Smarty_Internal_Method_' . $this->_property_info[ $basename ];
|
||||
if (class_exists($class)) {
|
||||
$classObj = new $class();
|
||||
$methodes = get_class_methods($classObj);
|
||||
foreach ($methodes as $method) {
|
||||
$smarty->ext->$method = $classObj;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->_property_info[ $prop ]) {
|
||||
$pn = $this->resolvedProperties[ $prop ];
|
||||
if ($match[ 1 ] == 'get') {
|
||||
return $this->_property_info[ $prop ] == 1 ? $data->$pn : $data->smarty->$pn;
|
||||
} else {
|
||||
return $this->_property_info[ $prop ] == 1 ? $data->$pn = $args[ 0 ] :
|
||||
$data->smarty->$pn = $args[ 0 ];
|
||||
if (!empty($match[2]) && !isset($smarty->ext->$name)) {
|
||||
$class = 'Smarty_Internal_Method_' . $this->upperCase($name);
|
||||
if (!class_exists($class)) {
|
||||
$objType = $data->_objType;
|
||||
$propertyType = false;
|
||||
if (!isset($this->resolvedProperties[ $match[0] ][ $objType ])) {
|
||||
$property = isset($this->resolvedProperties['property'][ $basename ]) ?
|
||||
$this->resolvedProperties['property'][ $basename ] :
|
||||
$property = $this->resolvedProperties['property'][ $basename ] = strtolower(join('_',
|
||||
preg_split('/([A-Z][^A-Z]*)/',
|
||||
$basename,
|
||||
-1,
|
||||
PREG_SPLIT_NO_EMPTY |
|
||||
PREG_SPLIT_DELIM_CAPTURE)));
|
||||
|
||||
if ($property !== false) {
|
||||
if (property_exists($data, $property)) {
|
||||
$propertyType = $this->resolvedProperties[ $match[0] ][ $objType ] = 1;
|
||||
} else if (property_exists($smarty, $property)) {
|
||||
$propertyType = $this->resolvedProperties[ $match[0] ][ $objType ] = 2;
|
||||
} else {
|
||||
$this->resolvedProperties['property'][ $basename ] = $property = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$propertyType = $this->resolvedProperties[ $match[0] ][ $objType ];
|
||||
$property = $this->resolvedProperties['property'][ $basename ];
|
||||
}
|
||||
if ($propertyType) {
|
||||
$obj = $propertyType === 1 ? $data : $smarty;
|
||||
if ($match[2] == 'get') {
|
||||
return $obj->$property;
|
||||
} else if ($match[2] == 'set') {
|
||||
return $obj->$property = $args[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif (!class_exists($class)) {
|
||||
throw new SmartyException("property '$pn' does not exist.");
|
||||
}
|
||||
}
|
||||
if (class_exists($class)) {
|
||||
$callback = array($smarty->ext->$name = new $class(), $name);
|
||||
}
|
||||
} else {
|
||||
$callback = array($smarty->ext->$name, $name);
|
||||
}
|
||||
$callback = array($smarty->ext->$name, $name);
|
||||
array_unshift($args, $data);
|
||||
if (isset($callback) && $callback[ 0 ]->objMap | $data->_objType) {
|
||||
if (isset($callback) && $callback[0]->objMap | $data->_objType) {
|
||||
return call_user_func_array($callback, $args);
|
||||
}
|
||||
return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), $args);
|
||||
@@ -119,19 +143,6 @@ class Smarty_Internal_Extension_Handler
|
||||
return implode('_', $_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* set extension property
|
||||
*
|
||||
* @param string $property_name property name
|
||||
* @param mixed $value value
|
||||
*
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public function __set($property_name, $value)
|
||||
{
|
||||
$this->$property_name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get extension object
|
||||
*
|
||||
@@ -143,7 +154,7 @@ class Smarty_Internal_Extension_Handler
|
||||
public function __get($property_name)
|
||||
{
|
||||
// object properties of runtime template extensions will start with '_'
|
||||
if ($property_name[ 0 ] == '_') {
|
||||
if ($property_name[0] == '_') {
|
||||
$class = 'Smarty_Internal_Runtime' . $this->upperCase($property_name);
|
||||
} else {
|
||||
$class = 'Smarty_Internal_Method_' . $this->upperCase($property_name);
|
||||
@@ -154,6 +165,19 @@ class Smarty_Internal_Extension_Handler
|
||||
return $this->$property_name = new $class();
|
||||
}
|
||||
|
||||
/**
|
||||
* set extension property
|
||||
*
|
||||
* @param string $property_name property name
|
||||
* @param mixed $value value
|
||||
*
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public function __set($property_name, $value)
|
||||
{
|
||||
$this->$property_name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call error handler for undefined method
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user