Finished moving smarty_internal_method_*

This commit is contained in:
Simon Wisselink
2022-12-21 14:47:33 +01:00
parent 5cac5e42f1
commit b0db4705a0
18 changed files with 314 additions and 709 deletions

View File

@@ -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
- [ ] review usages of ->_getSmartyObj and ->smarty: maybe change this so we can hide more
- [ ] ->_objType and ->objMap

View File

@@ -1,42 +0,0 @@
<?php
/**
* Smarty Method RegisterCacheResource
*
* Smarty::registerCacheResource() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterCacheResource
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers a resource to fetch a template
*
* @api Smarty::registerCacheResource()
* @link https://www.smarty.net/docs/en/api.register.cacheresource.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $name name of resource type
* @param \Smarty\Cacheresource\Base $resource_handler
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function registerCacheResource(
Smarty_Internal_TemplateBase $obj,
$name,
\Smarty\Cacheresource\Base $resource_handler
) {
$smarty = $obj->_getSmartyObj();
$smarty->registered_cache_resources[ $name ] = $resource_handler;
return $obj;
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
* Smarty Method RegisterClass
*
* Smarty::registerClass() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterClass
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers static classes to be used in templates
*
* @api Smarty::registerClass()
* @link https://www.smarty.net/docs/en/api.register.class.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $class_name
* @param string $class_impl the referenced PHP class to
* register
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function registerClass(Smarty_Internal_TemplateBase $obj, $class_name, $class_impl)
{
$smarty = $obj->_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;
}
}

View File

@@ -1,42 +0,0 @@
<?php
/**
* Smarty Method RegisterDefaultConfigHandler
*
* Smarty::registerDefaultConfigHandler() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterDefaultConfigHandler
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Register config default handler
*
* @api Smarty::registerDefaultConfigHandler()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param callable $callback class/method name
*
* @return \Smarty|\Smarty_Internal_Template
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultConfigHandler(Smarty_Internal_TemplateBase $obj, $callback)
{
$smarty = $obj->_getSmartyObj();
if (is_callable($callback)) {
$smarty->default_config_handler_func = $callback;
} else {
throw new SmartyException('Default config handler not callable');
}
return $obj;
}
}

View File

@@ -1,43 +0,0 @@
<?php
/**
* Smarty Method RegisterDefaultPluginHandler
*
* Smarty::registerDefaultPluginHandler() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterDefaultPluginHandler
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers a default plugin handler
*
* @api Smarty::registerDefaultPluginHandler()
* @link https://www.smarty.net/docs/en/api.register.default.plugin.handler.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param callable $callback class/method name
*
* @return \Smarty|\Smarty_Internal_Template
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultPluginHandler(Smarty_Internal_TemplateBase $obj, $callback)
{
$smarty = $obj->_getSmartyObj();
if (is_callable($callback)) {
$smarty->default_plugin_handler_func = $callback;
} else {
throw new SmartyException("Default plugin handler '$callback' not callable");
}
return $obj;
}
}

View File

@@ -1,88 +0,0 @@
<?php
/**
* Smarty Method RegisterDefaultTemplateHandler
*
* Smarty::registerDefaultTemplateHandler() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterDefaultTemplateHandler
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Register template default handler
*
* @api Smarty::registerDefaultTemplateHandler()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param callable $callback class/method name
*
* @return \Smarty|\Smarty_Internal_Template
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultTemplateHandler(Smarty_Internal_TemplateBase $obj, $callback)
{
$smarty = $obj->_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}'"
);
}
}
}

View File

@@ -1,84 +0,0 @@
<?php
/**
* Smarty Method RegisterObject
*
* Smarty::registerObject() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterObject
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers object to be used in templates
*
* @api Smarty::registerObject()
* @link https://www.smarty.net/docs/en/api.register.object.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $object_name
* @param object $object the
* referenced
* PHP
* object
* to
* register
*
* @param array $allowed_methods_properties list of
* allowed
* methods
* (empty
* = all)
*
* @param bool $format smarty
* argument
* format,
* else
* traditional
*
* @param array $block_methods list of
* block-methods
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function registerObject(
Smarty_Internal_TemplateBase $obj,
$object_name,
$object,
$allowed_methods_properties = array(),
$format = true,
$block_methods = array()
) {
$smarty = $obj->_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;
}
}

View File

@@ -1,58 +0,0 @@
<?php
/**
* Smarty Method RegisterPlugin
*
* Smarty::registerPlugin() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterPlugin
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers plugin to be used in templates
*
* @api Smarty::registerPlugin()
* @link https://www.smarty.net/docs/en/api.register.plugin.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type plugin type
* @param string $name name of template tag
* @param callback $callback PHP callback to register
* @param bool $cacheable if true (default) this
* function is cache able
* @param mixed $cache_attr caching attributes if any
*
* @return \Smarty|\Smarty_Internal_Template
* @throws SmartyException when the plugin tag is invalid
*/
public function registerPlugin(
Smarty_Internal_TemplateBase $obj,
$type,
$name,
$callback,
$cacheable = true,
$cache_attr = null
) {
$smarty = $obj->_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;
}
}

View File

@@ -1,39 +0,0 @@
<?php
/**
* Smarty Method RegisterResource
*
* Smarty::registerResource() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterResource
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers a resource to fetch a template
*
* @api Smarty::registerResource()
* @link https://www.smarty.net/docs/en/api.register.resource.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $name name of resource type
* @param Smarty_Resource $resource_handler instance of Smarty_Resource
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function registerResource(Smarty_Internal_TemplateBase $obj, $name, Smarty_Resource $resource_handler)
{
$smarty = $obj->_getSmartyObj();
$smarty->registered_resources[ $name ] = $resource_handler;
return $obj;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* Smarty Method SetDebugTemplate
*
* Smarty::setDebugTemplate() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_SetDebugTemplate
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* set the debug template
*
* @api Smarty::setDebugTemplate()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $tpl_name
*
* @return \Smarty|\Smarty_Internal_Template
* @throws SmartyException if file is not readable
*/
public function setDebugTemplate(Smarty_Internal_TemplateBase $obj, $tpl_name)
{
$smarty = $obj->_getSmartyObj();
if (!is_readable($tpl_name)) {
throw new SmartyException("Unknown file '{$tpl_name}'");
}
$smarty->debug_tpl = $tpl_name;
return $obj;
}
}

View File

@@ -1,38 +0,0 @@
<?php
/**
* Smarty Method SetDefaultModifiers
*
* Smarty::setDefaultModifiers() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_SetDefaultModifiers
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Set default modifiers
*
* @api Smarty::setDefaultModifiers()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array|string $modifiers modifier or list of modifiers
* to set
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function setDefaultModifiers(Smarty_Internal_TemplateBase $obj, $modifiers)
{
$smarty = $obj->_getSmartyObj();
$smarty->default_modifiers = (array)$modifiers;
return $obj;
}
}

View File

@@ -1,40 +0,0 @@
<?php
/**
* Smarty Method UnregisterCacheResource
*
* Smarty::unregisterCacheResource() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnregisterCacheResource
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers a resource to fetch a template
*
* @api Smarty::unregisterCacheResource()
* @link https://www.smarty.net/docs/en/api.unregister.cacheresource.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param $name
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function unregisterCacheResource(Smarty_Internal_TemplateBase $obj, $name)
{
$smarty = $obj->_getSmartyObj();
if (isset($smarty->registered_cache_resources[ $name ])) {
unset($smarty->registered_cache_resources[ $name ]);
}
return $obj;
}
}

View File

@@ -1,40 +0,0 @@
<?php
/**
* Smarty Method UnregisterObject
*
* Smarty::unregisterObject() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnregisterObject
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers plugin to be used in templates
*
* @api Smarty::unregisterObject()
* @link https://www.smarty.net/docs/en/api.unregister.object.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $object_name name of object
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function unregisterObject(Smarty_Internal_TemplateBase $obj, $object_name)
{
$smarty = $obj->_getSmartyObj();
if (isset($smarty->registered_objects[ $object_name ])) {
unset($smarty->registered_objects[ $object_name ]);
}
return $obj;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* Smarty Method UnregisterPlugin
*
* Smarty::unregisterPlugin() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnregisterPlugin
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers plugin to be used in templates
*
* @api Smarty::unregisterPlugin()
* @link https://www.smarty.net/docs/en/api.unregister.plugin.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type plugin type
* @param string $name name of template tag
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function unregisterPlugin(Smarty_Internal_TemplateBase $obj, $type, $name)
{
$smarty = $obj->_getSmartyObj();
if (isset($smarty->registered_plugins[ $type ][ $name ])) {
unset($smarty->registered_plugins[ $type ][ $name ]);
}
return $obj;
}
}

View File

@@ -1,40 +0,0 @@
<?php
/**
* Smarty Method UnregisterResource
*
* Smarty::unregisterResource() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnregisterResource
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Registers a resource to fetch a template
*
* @api Smarty::unregisterResource()
* @link https://www.smarty.net/docs/en/api.unregister.resource.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type name of resource type
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function unregisterResource(Smarty_Internal_TemplateBase $obj, $type)
{
$smarty = $obj->_getSmartyObj();
if (isset($smarty->registered_resources[ $type ])) {
unset($smarty->registered_resources[ $type ]);
}
return $obj;
}
}

View File

@@ -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;

View File

@@ -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}'"
);
}
}
}

View File

@@ -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;
}
}