Files
smarty/libs/sysplugins/smarty_internal_templatebase.php

482 lines
17 KiB
PHP
Raw Normal View History

2014-10-18 00:18:11 +02:00
<?php
/**
* Smarty Internal Plugin Smarty Template Base
* This file contains the basic shared methods for template handling
*
* @package Smarty
* @subpackage Template
* @author Uwe Tews
*/
/**
* Class with shared template methods
*
* @package Smarty
* @subpackage Template
*/
abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
{
/**
* Set this if you want different sets of cache files for the same
* templates.
*
* @var string
*/
public $cache_id = null;
/**
* Set this if you want different sets of compiled files for the same
* templates.
*
* @var string
*/
public $compile_id = null;
/**
* caching enabled
*
* @var boolean
*/
public $caching = false;
/**
* cache lifetime in seconds
*
* @var integer
*/
public $cache_lifetime = 3600;
2014-10-18 00:18:11 +02:00
/**
* test if cache is valid
*
* @param string|\Smarty_Internal_Template $template the resource handle of the template file or template object
* @param mixed $cache_id cache id to be used with this template
* @param mixed $compile_id compile id to be used with this template
* @param object $parent next higher level of Smarty variables
2014-10-18 00:18:11 +02:00
*
* @return boolean cache status
*/
public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
{
if ($template === null && $this instanceof $this->template_class) {
$template = $this;
} else {
if (!($template instanceof $this->template_class)) {
if ($parent === null) {
$parent = $this;
}
$smarty = isset($this->smarty) ? $this->smarty : $this;
$template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
2014-10-18 00:18:11 +02:00
}
}
// return cache status of template
if (!isset($template->cached)) {
$template->loadCached();
}
return $template->cached->isCached($template);
2014-10-18 00:18:11 +02:00
}
/**
* creates a data object
*
* @param object $parent next higher level of Smarty variables
2014-11-08 21:06:53 +01:00
* @param string $name optional data block name
2014-10-18 00:18:11 +02:00
*
* @returns Smarty_Data data object
*/
public function createData($parent = null, $name = null)
2014-10-18 00:18:11 +02:00
{
$dataObj = new Smarty_Data($parent, $this, $name);
if ($this->debugging) {
Smarty_Internal_Debug::register_data($dataObj);
}
return $dataObj;
2014-10-18 00:18:11 +02:00
}
/**
* Get unique template id
*
* @param string $template_name
* @param null|mixed $cache_id
* @param null|mixed $compile_id
*
* @return string
*/
public function getTemplateId($template_name, $cache_id = null, $compile_id = null)
{
$cache_id = isset($cache_id) ? $cache_id : $this->cache_id;
$compile_id = isset($compile_id) ? $compile_id : $this->compile_id;
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if ($smarty->allow_ambiguous_resources) {
$_templateId = Smarty_Resource::getUniqueTemplateName($this, $template_name) . "#{$cache_id}#{$compile_id}";
} else {
2015-02-15 01:45:37 +01:00
$_templateId = $smarty->joined_template_dir . "#{$template_name}#{$cache_id}#{$compile_id}";
}
if (isset($_templateId[150])) {
$_templateId = sha1($_templateId);
}
return $_templateId;
}
2014-10-18 00:18:11 +02:00
/**
* Registers plugin to be used in templates
*
* @param string $type plugin type
* @param string $tag name of template tag
* @param callback $callback PHP callback to register
* @param boolean $cacheable if true (default) this function is cache able
2014-10-18 00:18:11 +02:00
* @param array $cache_attr caching attributes if any
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
* @throws SmartyException when the plugin tag is invalid
*/
public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (isset($smarty->registered_plugins[$type][$tag])) {
2014-10-18 00:18:11 +02:00
throw new SmartyException("Plugin tag \"{$tag}\" already registered");
} elseif (!is_callable($callback)) {
throw new SmartyException("Plugin \"{$tag}\" not callable");
} else {
2015-02-15 01:45:37 +01:00
$smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
2014-10-18 00:18:11 +02:00
}
return $this;
}
/**
* Unregister Plugin
*
* @param string $type of plugin
* @param string $tag name of plugin
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unregisterPlugin($type, $tag)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (isset($smarty->registered_plugins[$type][$tag])) {
unset($smarty->registered_plugins[$type][$tag]);
2014-10-18 00:18:11 +02:00
}
return $this;
}
/**
* Registers a resource to fetch a template
*
* @param string $type name of resource type
* @param Smarty_Resource|array $callback or instance of Smarty_Resource, or array of callbacks to handle resource
* (deprecated)
2014-10-18 00:18:11 +02:00
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function registerResource($type, $callback)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
$smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback,
false);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Unregisters a resource
*
* @param string $type name of resource type
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unregisterResource($type)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (isset($smarty->registered_resources[$type])) {
unset($smarty->registered_resources[$type]);
2014-10-18 00:18:11 +02:00
}
return $this;
}
/**
* Registers a cache resource to cache a template's output
*
* @param string $type name of cache resource type
* @param Smarty_CacheResource $callback instance of Smarty_CacheResource to handle output caching
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function registerCacheResource($type, Smarty_CacheResource $callback)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
$smarty->registered_cache_resources[$type] = $callback;
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Unregisters a cache resource
*
* @param string $type name of cache resource type
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unregisterCacheResource($type)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (isset($smarty->registered_cache_resources[$type])) {
unset($smarty->registered_cache_resources[$type]);
2014-10-18 00:18:11 +02:00
}
return $this;
}
/**
* Registers object to be used in templates
*
* @param $object_name
* @param object $object_impl the referenced PHP object to register
* @param array $allowed list of allowed methods (empty = all)
* @param boolean $smarty_args smarty argument format, else traditional
* @param array $block_methods list of block-methods
*
* @throws SmartyException
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
{
Smarty_Internal_Extension_Object::registerObject($this, $object_name, $object_impl, $allowed, $smarty_args, $block_methods);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* return a reference to a registered object
*
* @param string $name object name
*
* @return object
* @throws SmartyException if no such object is found
*/
public function getRegisteredObject($name)
{
return Smarty_Internal_Extension_Object::getRegisteredObject($this, $name);
2014-10-18 00:18:11 +02:00
}
/**
* unregister an object
*
* @param string $name object name
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unregisterObject($name)
{
Smarty_Internal_Extension_Object::unregisterObject($this, $name);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Registers static classes to be used in templates
*
* @param $class_name
* @param string $class_impl the referenced PHP class to register
*
* @throws SmartyException
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function registerClass($class_name, $class_impl)
{
Smarty_Internal_Extension_Object::registerClass($this, $class_name, $class_impl);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Registers a default plugin handler
*
* @param callable $callback class/method name
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultPluginHandler($callback)
{
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
2014-10-18 00:18:11 +02:00
if (is_callable($callback)) {
2015-02-15 01:45:37 +01:00
$smarty->default_plugin_handler_func = $callback;
2014-10-18 00:18:11 +02:00
} else {
throw new SmartyException("Default plugin handler '$callback' not callable");
}
return $this;
}
/**
* Registers a default template handler
*
* @param callable $callback class/method name
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultTemplateHandler($callback)
{
Smarty_Internal_Extension_DefaultTemplateHandler::registerDefaultTemplateHandler($this, $callback);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Registers a default template handler
*
* @param callable $callback class/method name
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
* @throws SmartyException if $callback is not callable
*/
public function registerDefaultConfigHandler($callback)
{
Smarty_Internal_Extension_DefaultTemplateHandler::registerDefaultConfigHandler($this, $callback);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Registers a filter function
*
* @param string $type filter type
* @param callback $callback
* @param null|string $name option filter name
2014-10-18 00:18:11 +02:00
*
* @return \Smarty_Internal_TemplateBase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
* @throws \SmartyException
2014-10-18 00:18:11 +02:00
*/
public function registerFilter($type, $callback, $name = null)
2014-10-18 00:18:11 +02:00
{
Smarty_Internal_Extension_Filter::registerFilter($this, $type, $callback, $name);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Unregisters a filter function
*
* @param string $type filter type
* @param callback|string $callback
2014-10-18 00:18:11 +02:00
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unregisterFilter($type, $callback)
{
Smarty_Internal_Extension_Filter::unregisterFilter($this, $type, $callback);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* load a filter of specified type and name
*
* @param string $type filter type
* @param string $name filter name
*
* @return bool
2014-10-18 00:18:11 +02:00
* @throws SmartyException if filter could not be loaded
*/
public function loadFilter($type, $name)
{
return Smarty_Internal_Extension_Filter::loadFilter($this, $type, $name);
2014-10-18 00:18:11 +02:00
}
/**
* unload a filter of specified type and name
*
* @param string $type filter type
* @param string $name filter name
*
* @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
* Smarty_Internal_Template) instance for chaining
2014-10-18 00:18:11 +02:00
*/
public function unloadFilter($type, $name)
{
Smarty_Internal_Extension_Filter::unloadFilter($this, $type, $name);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* preg_replace callback to convert camelcase getter/setter to underscore property names
*
* @param string $match match string
*
* @return string replacement
2014-10-18 00:18:11 +02:00
*/
private function replaceCamelcase($match)
{
return "_" . strtolower($match[1]);
}
/**
* Handle unknown class methods
*
* @param string $name unknown method-name
* @param array $args argument array
*
* @throws SmartyException
*/
public function __call($name, $args)
{
static $_prefixes = array('set' => true, 'get' => true);
static $_resolved_property_name = array();
static $_resolved_property_source = array();
// see if this is a set/get for a property
$first3 = strtolower(substr($name, 0, 3));
if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') {
if (isset($_resolved_property_name[$name])) {
$property_name = $_resolved_property_name[$name];
} else {
// 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])/', array($this, 'replaceCamelcase'), $property_name);
$_resolved_property_name[$name] = $property_name;
}
if (isset($_resolved_property_source[$property_name])) {
2015-02-15 01:45:37 +01:00
$status = $_resolved_property_source[$property_name];
2014-10-18 00:18:11 +02:00
} else {
2015-02-15 01:45:37 +01:00
$status = null;
2014-10-18 00:18:11 +02:00
if (property_exists($this, $property_name)) {
2015-02-15 01:45:37 +01:00
$status = true;
2014-10-18 00:18:11 +02:00
} elseif (property_exists($this->smarty, $property_name)) {
2015-02-15 01:45:37 +01:00
$status = false;
2014-10-18 00:18:11 +02:00
}
2015-02-15 01:45:37 +01:00
$_resolved_property_source[$property_name] = $status;
2014-10-18 00:18:11 +02:00
}
2015-02-15 01:45:37 +01:00
$smarty = null;
if ($status === true) {
$smarty = $this;
} elseif ($status === false) {
$smarty = $this->smarty;
}
if ($smarty) {
2014-10-18 00:18:11 +02:00
if ($first3 == 'get') {
2015-02-15 01:45:37 +01:00
return $smarty->$property_name;
2014-10-18 00:18:11 +02:00
} else {
2015-02-15 01:45:37 +01:00
return $smarty->$property_name = $args[0];
2014-10-18 00:18:11 +02:00
}
}
2015-02-15 01:45:37 +01:00
throw new SmartyException("property '$property_name' does not exist.");
2014-10-18 00:18:11 +02:00
}
throw new SmartyException("Call of unknown method '$name'.");
}
}
2015-02-15 01:45:37 +01:00