diff --git a/TODO.txt b/TODO.txt index af24694a..dc8f9e6a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,2 +1,3 @@ - [ ] find ->ext-> calls, rewrite and remove first function call param -- [ ] review usages of ->_getSmartyObj and ->smarty: maybe change this so we can hide more \ No newline at end of file +- [ ] review usages of ->_getSmartyObj and ->smarty: maybe change this so we can hide more +- [ ] ->_objType and ->objMap \ No newline at end of file diff --git a/src/Method/smarty_internal_method_registercacheresource.php b/src/Method/smarty_internal_method_registercacheresource.php deleted file mode 100644 index ce2e735c..00000000 --- a/src/Method/smarty_internal_method_registercacheresource.php +++ /dev/null @@ -1,42 +0,0 @@ -_getSmartyObj(); - $smarty->registered_cache_resources[ $name ] = $resource_handler; - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerclass.php b/src/Method/smarty_internal_method_registerclass.php deleted file mode 100644 index 76a69c6e..00000000 --- a/src/Method/smarty_internal_method_registerclass.php +++ /dev/null @@ -1,46 +0,0 @@ -_getSmartyObj(); - // test if exists - if (!class_exists($class_impl)) { - throw new SmartyException("Undefined class '$class_impl' in register template class"); - } - // register the class - $smarty->registered_classes[ $class_name ] = $class_impl; - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerdefaultconfighandler.php b/src/Method/smarty_internal_method_registerdefaultconfighandler.php deleted file mode 100644 index b340f178..00000000 --- a/src/Method/smarty_internal_method_registerdefaultconfighandler.php +++ /dev/null @@ -1,42 +0,0 @@ -_getSmartyObj(); - if (is_callable($callback)) { - $smarty->default_config_handler_func = $callback; - } else { - throw new SmartyException('Default config handler not callable'); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerdefaultpluginhandler.php b/src/Method/smarty_internal_method_registerdefaultpluginhandler.php deleted file mode 100644 index 4cda5b05..00000000 --- a/src/Method/smarty_internal_method_registerdefaultpluginhandler.php +++ /dev/null @@ -1,43 +0,0 @@ -_getSmartyObj(); - if (is_callable($callback)) { - $smarty->default_plugin_handler_func = $callback; - } else { - throw new SmartyException("Default plugin handler '$callback' not callable"); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerdefaulttemplatehandler.php b/src/Method/smarty_internal_method_registerdefaulttemplatehandler.php deleted file mode 100644 index cbc133cc..00000000 --- a/src/Method/smarty_internal_method_registerdefaulttemplatehandler.php +++ /dev/null @@ -1,88 +0,0 @@ -_getSmartyObj(); - if (is_callable($callback)) { - $smarty->default_template_handler_func = $callback; - } else { - throw new SmartyException('Default template handler not callable'); - } - return $obj; - } - - /** - * get default content from template or config resource handler - * - * @param Smarty_Template_Source $source - * - * @throws \SmartyException - */ - public static function _getDefaultTemplate(Smarty_Template_Source $source) - { - if ($source->isConfig) { - $default_handler = $source->smarty->default_config_handler_func; - } else { - $default_handler = $source->smarty->default_template_handler_func; - } - $_content = $_timestamp = null; - $_return = call_user_func_array( - $default_handler, - array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty) - ); - if (is_string($_return)) { - $source->exists = is_file($_return); - if ($source->exists) { - $source->timestamp = filemtime($_return); - } else { - throw new SmartyException( - 'Default handler: Unable to load ' . - ($source->isConfig ? 'config' : 'template') . - " default file '{$_return}' for '{$source->type}:{$source->name}'" - ); - } - $source->name = $source->filepath = $_return; - $source->uid = sha1($source->filepath); - } elseif ($_return === true) { - $source->content = $_content; - $source->exists = true; - $source->uid = $source->name = sha1($_content); - $source->handler = Smarty_Resource::load($source->smarty, 'eval'); - } else { - $source->exists = false; - throw new SmartyException( - 'Default handler: No ' . ($source->isConfig ? 'config' : 'template') . - " default content for '{$source->type}:{$source->name}'" - ); - } - } -} diff --git a/src/Method/smarty_internal_method_registerobject.php b/src/Method/smarty_internal_method_registerobject.php deleted file mode 100644 index 8e6fe052..00000000 --- a/src/Method/smarty_internal_method_registerobject.php +++ /dev/null @@ -1,84 +0,0 @@ -_getSmartyObj(); - // test if allowed methods callable - if (!empty($allowed_methods_properties)) { - foreach ((array)$allowed_methods_properties as $method) { - if (!is_callable(array($object, $method)) && !property_exists($object, $method)) { - throw new SmartyException("Undefined method or property '$method' in registered object"); - } - } - } - // test if block methods callable - if (!empty($block_methods)) { - foreach ((array)$block_methods as $method) { - if (!is_callable(array($object, $method))) { - throw new SmartyException("Undefined method '$method' in registered object"); - } - } - } - // register the object - $smarty->registered_objects[ $object_name ] = - array($object, (array)$allowed_methods_properties, (boolean)$format, (array)$block_methods); - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerplugin.php b/src/Method/smarty_internal_method_registerplugin.php deleted file mode 100644 index 74c0ae90..00000000 --- a/src/Method/smarty_internal_method_registerplugin.php +++ /dev/null @@ -1,58 +0,0 @@ -_getSmartyObj(); - if (isset($smarty->registered_plugins[ $type ][ $name ])) { - throw new SmartyException("Plugin tag '{$name}' already registered"); - } elseif (!is_callable($callback)) { - throw new SmartyException("Plugin '{$name}' not callable"); - } elseif ($cacheable && $cache_attr) { - throw new SmartyException("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); - } else { - $smarty->registered_plugins[ $type ][ $name ] = array($callback, (bool)$cacheable, (array)$cache_attr); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_registerresource.php b/src/Method/smarty_internal_method_registerresource.php deleted file mode 100644 index 302657ae..00000000 --- a/src/Method/smarty_internal_method_registerresource.php +++ /dev/null @@ -1,39 +0,0 @@ -_getSmartyObj(); - $smarty->registered_resources[ $name ] = $resource_handler; - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_setdebugtemplate.php b/src/Method/smarty_internal_method_setdebugtemplate.php deleted file mode 100644 index cc9d23e2..00000000 --- a/src/Method/smarty_internal_method_setdebugtemplate.php +++ /dev/null @@ -1,41 +0,0 @@ -_getSmartyObj(); - if (!is_readable($tpl_name)) { - throw new SmartyException("Unknown file '{$tpl_name}'"); - } - $smarty->debug_tpl = $tpl_name; - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_setdefaultmodifiers.php b/src/Method/smarty_internal_method_setdefaultmodifiers.php deleted file mode 100644 index eadc2de1..00000000 --- a/src/Method/smarty_internal_method_setdefaultmodifiers.php +++ /dev/null @@ -1,38 +0,0 @@ -_getSmartyObj(); - $smarty->default_modifiers = (array)$modifiers; - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_unregistercacheresource.php b/src/Method/smarty_internal_method_unregistercacheresource.php deleted file mode 100644 index 377397e9..00000000 --- a/src/Method/smarty_internal_method_unregistercacheresource.php +++ /dev/null @@ -1,40 +0,0 @@ -_getSmartyObj(); - if (isset($smarty->registered_cache_resources[ $name ])) { - unset($smarty->registered_cache_resources[ $name ]); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_unregisterobject.php b/src/Method/smarty_internal_method_unregisterobject.php deleted file mode 100644 index 77d61963..00000000 --- a/src/Method/smarty_internal_method_unregisterobject.php +++ /dev/null @@ -1,40 +0,0 @@ -_getSmartyObj(); - if (isset($smarty->registered_objects[ $object_name ])) { - unset($smarty->registered_objects[ $object_name ]); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_unregisterplugin.php b/src/Method/smarty_internal_method_unregisterplugin.php deleted file mode 100644 index 2431d5c2..00000000 --- a/src/Method/smarty_internal_method_unregisterplugin.php +++ /dev/null @@ -1,41 +0,0 @@ -_getSmartyObj(); - if (isset($smarty->registered_plugins[ $type ][ $name ])) { - unset($smarty->registered_plugins[ $type ][ $name ]); - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_unregisterresource.php b/src/Method/smarty_internal_method_unregisterresource.php deleted file mode 100644 index bbb6a861..00000000 --- a/src/Method/smarty_internal_method_unregisterresource.php +++ /dev/null @@ -1,40 +0,0 @@ -_getSmartyObj(); - if (isset($smarty->registered_resources[ $type ])) { - unset($smarty->registered_resources[ $type ]); - } - return $obj; - } -} diff --git a/src/Template/smarty_template_config.php b/src/Template/smarty_template_config.php index d5cac3eb..b81e73dd 100644 --- a/src/Template/smarty_template_config.php +++ b/src/Template/smarty_template_config.php @@ -92,7 +92,7 @@ class Smarty_Template_Config extends Smarty_Template_Source $source = new Smarty_Template_Config($smarty, $template_resource, $type, $name); $source->handler->populate($source, $_template); if (!$source->exists && isset($smarty->default_config_handler_func)) { - Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source); + $source->_getDefaultTemplate($smarty->default_config_handler_func); $source->handler->populate($source, $_template); } return $source; diff --git a/src/Template/smarty_template_source.php b/src/Template/smarty_template_source.php index 8ad039db..e2e432be 100644 --- a/src/Template/smarty_template_source.php +++ b/src/Template/smarty_template_source.php @@ -180,7 +180,7 @@ class Smarty_Template_Source $source = new Smarty_Template_Source($smarty, $template_resource, $type, $name); $source->handler->populate($source, $_template); if (!$source->exists && isset($_template->smarty->default_template_handler_func)) { - Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source); + $source->_getDefaultTemplate($_template->smarty->default_template_handler_func); $source->handler->populate($source, $_template); } return $source; @@ -209,4 +209,43 @@ class Smarty_Template_Source { return isset($this->content) ? $this->content : $this->handler->getContent($this); } + + /** + * get default content from template or config resource handler + * + * @throws \SmartyException + */ + public function _getDefaultTemplate($default_handler) + { + $_content = $_timestamp = null; + $_return = call_user_func_array( + $default_handler, + array($this->type, $this->name, &$_content, &$_timestamp, $this->smarty) + ); + if (is_string($_return)) { + $this->exists = is_file($_return); + if ($this->exists) { + $this->timestamp = filemtime($_return); + } else { + throw new SmartyException( + 'Default handler: Unable to load ' . + ($this->isConfig ? 'config' : 'template') . + " default file '{$_return}' for '{$this->type}:{$this->name}'" + ); + } + $this->name = $this->filepath = $_return; + $this->uid = sha1($this->filepath); + } elseif ($_return === true) { + $this->content = $_content; + $this->exists = true; + $this->uid = $this->name = sha1($_content); + $this->handler = Smarty_Resource::load($this->smarty, 'eval'); + } else { + $this->exists = false; + throw new SmartyException( + 'Default handler: No ' . ($this->isConfig ? 'config' : 'template') . + " default content for '{$this->type}:{$this->name}'" + ); + } + } } diff --git a/src/smarty_internal_templatebase.php b/src/smarty_internal_templatebase.php index 49c05abe..0c17f92b 100644 --- a/src/smarty_internal_templatebase.php +++ b/src/smarty_internal_templatebase.php @@ -8,6 +8,7 @@ * @author Uwe Tews */ +use Smarty\Cacheresource\Base; use Smarty\Data; use Smarty\Smarty; @@ -16,21 +17,6 @@ use Smarty\Smarty; * * @property int $_objType * - * 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 registerCacheResource(string $name, \Smarty\Cacheresource\Base $resource_handler) - * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl) - * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback) - * @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 setDebugTemplate(string $tpl_name) - * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers) - * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name) - * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name) - * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name) - * @method Smarty_Internal_TemplateBase unregisterResource(string $name) */ abstract class Smarty_Internal_TemplateBase extends Data { @@ -283,9 +269,39 @@ abstract class Smarty_Internal_TemplateBase extends Data */ public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null) { - return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr); + $smarty = $this->_getSmartyObj(); + if (isset($smarty->registered_plugins[ $type ][ $name ])) { + throw new SmartyException("Plugin tag '{$name}' already registered"); + } elseif (!is_callable($callback)) { + throw new SmartyException("Plugin '{$name}' not callable"); + } elseif ($cacheable && $cache_attr) { + throw new SmartyException("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); + } else { + $smarty->registered_plugins[ $type ][ $name ] = array($callback, (bool)$cacheable, (array)$cache_attr); + } + return $this; } + /** + * Registers plugin to be used in templates + * + * @api Smarty::unregisterPlugin() + * @link https://www.smarty.net/docs/en/api.unregister.plugin.tpl + * + * @param string $type plugin type + * @param string $name name of template tag + * + * @return \Smarty|\Smarty_Internal_Template + */ + public function unregisterPlugin($type, $name) + { + $smarty = $this->_getSmartyObj(); + if (isset($smarty->registered_plugins[ $type ][ $name ])) { + unset($smarty->registered_plugins[ $type ][ $name ]); + } + return $this; + } + /** * load a filter of specified type and name * @@ -423,16 +439,48 @@ abstract class Smarty_Internal_TemplateBase extends Data $format = true, $block_methods = array() ) { - return $this->ext->registerObject->registerObject( - $this, - $object_name, - $object, - $allowed_methods_properties, - $format, - $block_methods - ); + $smarty = $this->_getSmartyObj(); + // test if allowed methods callable + if (!empty($allowed_methods_properties)) { + foreach ((array)$allowed_methods_properties as $method) { + if (!is_callable(array($object, $method)) && !property_exists($object, $method)) { + throw new SmartyException("Undefined method or property '$method' in registered object"); + } + } + } + // test if block methods callable + if (!empty($block_methods)) { + foreach ((array)$block_methods as $method) { + if (!is_callable(array($object, $method))) { + throw new SmartyException("Undefined method '$method' in registered object"); + } + } + } + // register the object + $smarty->registered_objects[ $object_name ] = + array($object, (array)$allowed_methods_properties, (boolean)$format, (array)$block_methods); + return $this; } + /** + * Registers plugin to be used in templates + * + * @param string $object_name name of object + * + * @return Smarty_Internal_TemplateBase + * @api Smarty::unregisterObject() + * @link https://www.smarty.net/docs/en/api.unregister.object.tpl + * + */ + public function unregisterObject($object_name) + { + $smarty = $this->_getSmartyObj(); + if (isset($smarty->registered_objects[ $object_name ])) { + unset($smarty->registered_objects[ $object_name ]); + } + return $this; + } + /** * @param int $compile_check */ @@ -677,4 +725,203 @@ abstract class Smarty_Internal_TemplateBase extends Data } } + /** + * Registers static classes to be used in templates + * + * @param string $class_name + * @param string $class_impl the referenced PHP class to + * register + * + * @return Smarty_Internal_TemplateBase + * @throws \SmartyException + *@api Smarty::registerClass() + * @link https://www.smarty.net/docs/en/api.register.class.tpl + * + */ + public function registerClass($class_name, $class_impl) + { + $smarty = $this->_getSmartyObj(); + // test if exists + if (!class_exists($class_impl)) { + throw new SmartyException("Undefined class '$class_impl' in register template class"); + } + // register the class + $smarty->registered_classes[ $class_name ] = $class_impl; + return $this; + } + + /** + * Registers a resource to fetch a template + * + * @param string $name name of resource type + * @param Base $resource_handler + * + * @return Smarty_Internal_TemplateBase + * @link https://www.smarty.net/docs/en/api.register.cacheresource.tpl + * + * @api Smarty::registerCacheResource() + */ + public function registerCacheResource($name, Base $resource_handler) { + $smarty = $this->_getSmartyObj(); + $smarty->registered_cache_resources[ $name ] = $resource_handler; + return $this; + } + + /** + * Unregisters a resource to fetch a template + * + * @api Smarty::unregisterCacheResource() + * @link https://www.smarty.net/docs/en/api.unregister.cacheresource.tpl + * + * @param $name + * + * @return \Smarty|\Smarty_Internal_Template + */ + public function unregisterCacheResource($name) + { + $smarty = $this->_getSmartyObj(); + if (isset($smarty->registered_cache_resources[ $name ])) { + unset($smarty->registered_cache_resources[ $name ]); + } + return $this; + } + + /** + * Register config default handler + * + * @param callable $callback class/method name + * + * @return Smarty_Internal_TemplateBase + * @throws SmartyException if $callback is not callable + *@api Smarty::registerDefaultConfigHandler() + * + */ + public function registerDefaultConfigHandler($callback) + { + $smarty = $this->_getSmartyObj(); + if (is_callable($callback)) { + $smarty->default_config_handler_func = $callback; + } else { + throw new SmartyException('Default config handler not callable'); + } + return $this; + } + + /** + * Registers a default plugin handler + * + * @param callable $callback class/method name + * + * @return Smarty_Internal_TemplateBase + * @throws SmartyException if $callback is not callable + * @link https://www.smarty.net/docs/en/api.register.default.plugin.handler.tpl + * + * @api Smarty::registerDefaultPluginHandler() + */ + public function registerDefaultPluginHandler($callback) + { + $smarty = $this->_getSmartyObj(); + if (is_callable($callback)) { + $smarty->default_plugin_handler_func = $callback; + } else { + throw new SmartyException("Default plugin handler '$callback' not callable"); + } + return $this; + } + + /** + * Register template default handler + * + * @param callable $callback class/method name + * + * @return Smarty_Internal_TemplateBase + * @throws SmartyException if $callback is not callable + * @api Smarty::registerDefaultTemplateHandler() + * + */ + public function registerDefaultTemplateHandler($callback) + { + $smarty = $this->_getSmartyObj(); + if (is_callable($callback)) { + $smarty->default_template_handler_func = $callback; + } else { + throw new SmartyException('Default template handler not callable'); + } + return $this; + } + + /** + * Registers a resource to fetch a template + * + * @api Smarty::registerResource() + * @link https://www.smarty.net/docs/en/api.register.resource.tpl + * + * @param string $name name of resource type + * @param Smarty_Resource $resource_handler instance of Smarty_Resource + * + * @return \Smarty|\Smarty_Internal_Template + */ + public function registerResource($name, Smarty_Resource $resource_handler) + { + $smarty = $this->_getSmartyObj(); + $smarty->registered_resources[ $name ] = $resource_handler; + return $this; + } + + /** + * Unregisters a resource to fetch a template + * + * @param string $type name of resource type + * + * @return Smarty_Internal_TemplateBase + * @api Smarty::unregisterResource() + * @link https://www.smarty.net/docs/en/api.unregister.resource.tpl + * + */ + public function unregisterResource($type) + { + $smarty = $this->_getSmartyObj(); + if (isset($smarty->registered_resources[ $type ])) { + unset($smarty->registered_resources[ $type ]); + } + return $this; + } + + /** + * set the debug template + * + * @param string $tpl_name + * + * @return Smarty_Internal_TemplateBase + * @throws SmartyException if file is not readable + *@api Smarty::setDebugTemplate() + * + */ + public function setDebugTemplate($tpl_name) + { + $smarty = $this->_getSmartyObj(); + if (!is_readable($tpl_name)) { + throw new SmartyException("Unknown file '{$tpl_name}'"); + } + $smarty->debug_tpl = $tpl_name; + return $this; + } + + /** + * Set default modifiers + * + * @param array|string $modifiers modifier or list of modifiers + * to set + * + * @return Smarty_Internal_TemplateBase + * @api Smarty::setDefaultModifiers() + * + */ + public function setDefaultModifiers($modifiers) + { + $smarty = $this->_getSmartyObj(); + $smarty->default_modifiers = (array)$modifiers; + return $this; + } + }