You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_extension_handler.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Smarty Extension handler
  4. *
  5. * Load extensions dynamically
  6. *
  7. *
  8. * @package Smarty
  9. * @subpackage PluginsInternal
  10. * @author Uwe Tews
  11. *
  12. * @property Smarty_Internal_Runtime_Inheritance $_inheritance
  13. * @property Smarty_Internal_Runtime_SubTemplate $_subTemplate
  14. * @property Smarty_Internal_Runtime_TplFunction $_tplFunction
  15. * @property Smarty_Internal_Runtime_Var $_var
  16. * @property Smarty_Internal_Runtime_Config $_config
  17. * @property Smarty_Internal_Runtime_Foreach $_foreach
  18. * @property Smarty_Internal_Runtime_Hhvm $_hhvm
  19. * @property Smarty_Internal_Runtime_WriteFile $_writeFile
  20. * @property Smarty_Internal_Runtime_ValidateCompiled $_validateCompiled
  21. * @property Smarty_Internal_Runtime_CodeFrame $_codeFrame
  22. * @property Smarty_Internal_Runtime_FilterHandler $_filterHandler
  23. * @property Smarty_Internal_Runtime_GetIncludePath $_getIncludePath
  24. * @property Smarty_Internal_Runtime_UpdateScope $_updateScope
  25. * @property Smarty_Internal_Runtime_IsCached $_isCached
  26. * @property Smarty_Internal_Runtime_CacheModify $_cacheModify
  27. * @property Smarty_Internal_Runtime_UpdateCache $_updateCache
  28. * @property Smarty_Internal_Method_GetTemplateVars $getTemplateVars
  29. * @property Smarty_Internal_Method_Append $append
  30. * @property Smarty_Internal_Method_AppendByRef $appendByRef
  31. * @property Smarty_Internal_Method_AssignGlobal $assignGlobal
  32. * @property Smarty_Internal_Method_AssignByRef $assignByRef
  33. * @property Smarty_Internal_Method_LoadFilter $loadFilter
  34. * @property Smarty_Internal_Method_LoadPlugin $loadPlugin
  35. * @property Smarty_Internal_Method_RegisterFilter $registerFilter
  36. * @property Smarty_Internal_Method_RegisterObject $registerObject
  37. * @property Smarty_Internal_Method_RegisterPlugin $registerPlugin
  38. */
  39. class Smarty_Internal_Extension_Handler
  40. {
  41. public $objType = null;
  42. /**
  43. * Cache for property information from generic getter/setter
  44. * Preloaded with names which should not use with generic getter/setter
  45. *
  46. * @var array
  47. */
  48. private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
  49. 'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
  50. 'TemplateVars' => 0,);#
  51. private $resolvedProperties = array();
  52. /**
  53. * Call external Method
  54. *
  55. * @param \Smarty_Internal_Data $data
  56. * @param string $name external method names
  57. * @param array $args argument array
  58. *
  59. * @return mixed
  60. * @throws SmartyException
  61. */
  62. public function _callExternalMethod(Smarty_Internal_Data $data, $name, $args)
  63. {
  64. /* @var Smarty $data ->smarty */
  65. $smarty = isset($data->smarty) ? $data->smarty : $data;
  66. if (!isset($smarty->ext->$name)) {
  67. $class = 'Smarty_Internal_Method_' . ucfirst($name);
  68. if (preg_match('/^(set|get)([A-Z].*)$/', $name, $match)) {
  69. if (!isset($this->_property_info[$prop = $match[2]])) {
  70. // convert camel case to underscored name
  71. $this->resolvedProperties[$prop] = $pn = strtolower(join('_',
  72. preg_split('/([A-Z][^A-Z]*)/', $prop, - 1,
  73. PREG_SPLIT_NO_EMPTY |
  74. PREG_SPLIT_DELIM_CAPTURE)));
  75. $this->_property_info[$prop] = property_exists($data, $pn) ? 1 :
  76. ($data->_objType == 2 && property_exists($smarty, $pn) ? 2 : 0);
  77. }
  78. if ($this->_property_info[$prop]) {
  79. $pn = $this->resolvedProperties[$prop];
  80. if ($match[1] == 'get') {
  81. return $this->_property_info[$prop] == 1 ? $data->$pn : $data->smarty->$pn;
  82. } else {
  83. return $this->_property_info[$prop] == 1 ? $data->$pn = $args[0] :
  84. $data->smarty->$pn = $args[0];
  85. }
  86. } elseif (!class_exists($class)) {
  87. throw new SmartyException("property '$pn' does not exist.");
  88. }
  89. }
  90. if (class_exists($class)) {
  91. $callback = array($smarty->ext->$name = new $class(), $name);
  92. }
  93. } else {
  94. $callback = array($smarty->ext->$name, $name);
  95. }
  96. array_unshift($args, $data);
  97. if (isset($callback) && $callback[0]->objMap | $data->_objType) {
  98. return call_user_func_array($callback, $args);
  99. }
  100. return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), $args);
  101. }
  102. /**
  103. * set extension property
  104. *
  105. * @param string $property_name property name
  106. * @param mixed $value value
  107. *
  108. * @throws SmartyException
  109. */
  110. public function __set($property_name, $value)
  111. {
  112. $this->$property_name = $value;
  113. }
  114. /**
  115. * get extension object
  116. *
  117. * @param string $property_name property name
  118. *
  119. * @return mixed|Smarty_Template_Cached
  120. * @throws SmartyException
  121. */
  122. public function __get($property_name)
  123. {
  124. // object properties of runtime template extensions will start with '_'
  125. if ($property_name[0] == '_') {
  126. $class = 'Smarty_Internal_Runtime_' . ucfirst(substr($property_name, 1));
  127. } else {
  128. $class = 'Smarty_Internal_Method_' . ucfirst($property_name);
  129. }
  130. if (class_exists($class)) {
  131. return $this->$property_name = new $class();
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Call error handler for undefined method
  137. *
  138. * @param string $name unknown method-name
  139. * @param array $args argument array
  140. *
  141. * @return mixed
  142. * @throws SmartyException
  143. */
  144. public function __call($name, $args)
  145. {
  146. return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), $args);
  147. }
  148. }