mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 10:54:27 +02:00
- move registerObject / registerClass into extension
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
- optimization of template/config file normalization
|
||||
- optimization of directory handling / build realpath
|
||||
- optimization of filter execution
|
||||
- move registerObject / registerClass into extension
|
||||
|
||||
19.06.2015
|
||||
- improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59
|
||||
|
@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* smarty version
|
||||
*/
|
||||
const SMARTY_VERSION = '3.1.28-dev/4';
|
||||
const SMARTY_VERSION = '3.1.28-dev/5';
|
||||
|
||||
/**
|
||||
* define variable scopes
|
||||
|
110
libs/sysplugins/smarty_internal_extension_object.php
Normal file
110
libs/sysplugins/smarty_internal_extension_object.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Smarty Extension Object
|
||||
*
|
||||
* Register object methods
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage PluginsInternal
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
class Smarty_Internal_Extension_Object
|
||||
{
|
||||
/**
|
||||
* Registers object to be used in templates
|
||||
*
|
||||
* @param \Smarty_Internal_Template|\Smarty $obj
|
||||
* @param string $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
|
||||
*
|
||||
* @return \Smarty_Internal_Templatebase
|
||||
* @throws \SmartyException
|
||||
*/
|
||||
static function registerObject($obj, $object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
|
||||
{
|
||||
// test if allowed methods callable
|
||||
if (!empty($allowed)) {
|
||||
foreach ((array) $allowed as $method) {
|
||||
if (!is_callable(array($object_impl, $method)) && !property_exists($object_impl, $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_impl, $method))) {
|
||||
throw new SmartyException("Undefined method '$method' in registered object");
|
||||
}
|
||||
}
|
||||
}
|
||||
// register the object
|
||||
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
|
||||
$smarty->registered_objects[$object_name] = array($object_impl, (array) $allowed, (boolean) $smarty_args,
|
||||
(array) $block_methods);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a reference to a registered object
|
||||
*
|
||||
* @param \Smarty_Internal_Template|\Smarty $obj
|
||||
* @param string $name object name
|
||||
*
|
||||
* @return object
|
||||
* @throws \SmartyException if no such object is found
|
||||
*/
|
||||
static function getRegisteredObject($obj, $name)
|
||||
{
|
||||
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
|
||||
if (!isset($smarty->registered_objects[$name])) {
|
||||
throw new SmartyException("'$name' is not a registered object");
|
||||
}
|
||||
if (!is_object($smarty->registered_objects[$name][0])) {
|
||||
throw new SmartyException("registered '$name' is not an object");
|
||||
}
|
||||
return $smarty->registered_objects[$name][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister an object
|
||||
*
|
||||
* @param \Smarty_Internal_Template|\Smarty $obj
|
||||
* @param string $name object name
|
||||
*
|
||||
* @return \Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or
|
||||
* Smarty_Internal_Template) instance for chaining
|
||||
*/
|
||||
static function unregisterObject($obj, $name)
|
||||
{
|
||||
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
|
||||
if (isset($smarty->registered_objects[$name])) {
|
||||
unset($smarty->registered_objects[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers static classes to be used in templates
|
||||
*
|
||||
* @param \Smarty_Internal_Template|\Smarty $obj
|
||||
* @param string $class_name
|
||||
* @param string $class_impl the referenced PHP class to register
|
||||
*
|
||||
* @return \Smarty_Internal_Templatebase
|
||||
* @throws \SmartyException
|
||||
*/
|
||||
static function registerClass($obj, $class_name, $class_impl)
|
||||
{
|
||||
// test if exists
|
||||
if (!class_exists($class_impl)) {
|
||||
throw new SmartyException("Undefined class '$class_impl' in register template class");
|
||||
}
|
||||
// register the class
|
||||
$smarty = isset($obj->smarty) ? $obj->smarty : $obj;
|
||||
$smarty->registered_classes[$class_name] = $class_impl;
|
||||
}
|
||||
|
||||
}
|
@@ -251,27 +251,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
|
||||
*/
|
||||
public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
|
||||
{
|
||||
// test if allowed methods callable
|
||||
if (!empty($allowed)) {
|
||||
foreach ((array) $allowed as $method) {
|
||||
if (!is_callable(array($object_impl, $method)) && !property_exists($object_impl, $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_impl, $method))) {
|
||||
throw new SmartyException("Undefined method '$method' in registered object");
|
||||
}
|
||||
}
|
||||
}
|
||||
// register the object
|
||||
$smarty = isset($this->smarty) ? $this->smarty : $this;
|
||||
$smarty->registered_objects[$object_name] = array($object_impl, (array) $allowed, (boolean) $smarty_args,
|
||||
(array) $block_methods);
|
||||
|
||||
Smarty_Internal_Extension_Object::registerObject($this, $object_name, $object_impl, $allowed, $smarty_args, $block_methods);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -285,15 +265,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
|
||||
*/
|
||||
public function getRegisteredObject($name)
|
||||
{
|
||||
$smarty = isset($this->smarty) ? $this->smarty : $this;
|
||||
if (!isset($smarty->registered_objects[$name])) {
|
||||
throw new SmartyException("'$name' is not a registered object");
|
||||
}
|
||||
if (!is_object($smarty->registered_objects[$name][0])) {
|
||||
throw new SmartyException("registered '$name' is not an object");
|
||||
}
|
||||
|
||||
return $smarty->registered_objects[$name][0];
|
||||
return Smarty_Internal_Extension_Object::getRegisteredObject($this, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,11 +278,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
|
||||
*/
|
||||
public function unregisterObject($name)
|
||||
{
|
||||
$smarty = isset($this->smarty) ? $this->smarty : $this;
|
||||
if (isset($smarty->registered_objects[$name])) {
|
||||
unset($smarty->registered_objects[$name]);
|
||||
}
|
||||
|
||||
Smarty_Internal_Extension_Object::unregisterObject($this, $name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -326,14 +294,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
|
||||
*/
|
||||
public function registerClass($class_name, $class_impl)
|
||||
{
|
||||
// test if exists
|
||||
if (!class_exists($class_impl)) {
|
||||
throw new SmartyException("Undefined class '$class_impl' in register template class");
|
||||
}
|
||||
// register the class
|
||||
$smarty = isset($this->smarty) ? $this->smarty : $this;
|
||||
$smarty->registered_classes[$class_name] = $class_impl;
|
||||
|
||||
Smarty_Internal_Extension_Object::registerClass($this, $class_name, $class_impl);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user