smarty_internal_extension_handler.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * Runtime extensions
  13. * @property Smarty_Internal_Runtime_CacheModify $_cacheModify
  14. * @property Smarty_Internal_Runtime_CacheResourceFile $_cacheResourceFile
  15. * @property Smarty_Internal_Runtime_Capture $_capture
  16. * @property Smarty_Internal_Runtime_CodeFrame $_codeFrame
  17. * @property Smarty_Internal_Runtime_FilterHandler $_filterHandler
  18. * @property Smarty_Internal_Runtime_Foreach $_foreach
  19. * @property Smarty_Internal_Runtime_GetIncludePath $_getIncludePath
  20. * @property Smarty_Internal_Runtime_Make_Nocache $_make_nocache
  21. * @property Smarty_Internal_Runtime_UpdateCache $_updateCache
  22. * @property Smarty_Internal_Runtime_UpdateScope $_updateScope
  23. * @property Smarty_Internal_Runtime_TplFunction $_tplFunction
  24. * @property Smarty_Internal_Runtime_WriteFile $_writeFile
  25. *
  26. * Method extensions
  27. * @property Smarty_Internal_Method_GetTemplateVars $getTemplateVars
  28. * @property Smarty_Internal_Method_Append $append
  29. * @property Smarty_Internal_Method_AppendByRef $appendByRef
  30. * @property Smarty_Internal_Method_AssignGlobal $assignGlobal
  31. * @property Smarty_Internal_Method_AssignByRef $assignByRef
  32. * @property Smarty_Internal_Method_LoadFilter $loadFilter
  33. * @property Smarty_Internal_Method_LoadPlugin $loadPlugin
  34. * @property Smarty_Internal_Method_RegisterFilter $registerFilter
  35. * @property Smarty_Internal_Method_RegisterObject $registerObject
  36. * @property Smarty_Internal_Method_RegisterPlugin $registerPlugin
  37. */
  38. class Smarty_Internal_Extension_Handler
  39. {
  40. public $objType = null;
  41. /**
  42. * Cache for property information from generic getter/setter
  43. * Preloaded with names which should not use with generic getter/setter
  44. *
  45. * @var array
  46. */
  47. private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
  48. 'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
  49. 'TemplateVars' => 0,);#
  50. private $resolvedProperties = array();
  51. /**
  52. * Call external Method
  53. *
  54. * @param \Smarty_Internal_Data $data
  55. * @param string $name external method names
  56. * @param array $args argument array
  57. *
  58. * @return mixed
  59. * @throws SmartyException
  60. */
  61. public function _callExternalMethod(Smarty_Internal_Data $data, $name, $args)
  62. {
  63. /* @var Smarty $data ->smarty */
  64. $smarty = isset($data->smarty) ? $data->smarty : $data;
  65. if (!isset($smarty->ext->$name)) {
  66. $class = 'Smarty_Internal_Method_' . $this->upperCase($name);
  67. if (preg_match('/^(set|get)([A-Z].*)$/', $name, $match)) {
  68. $pn = '';
  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,
  73. - 1, PREG_SPLIT_NO_EMPTY |
  74. PREG_SPLIT_DELIM_CAPTURE)));
  75. $this->_property_info[ $prop ] =
  76. property_exists($data, $pn) ? 1 : ($data->_isTplObj() && 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. * Make first character of name parts upper case
  104. *
  105. * @param string $name
  106. *
  107. * @return string
  108. */
  109. public function upperCase($name)
  110. {
  111. $_name = explode('_', $name);
  112. $_name = array_map('ucfirst', $_name);
  113. return implode('_', $_name);
  114. }
  115. /**
  116. * set extension property
  117. *
  118. * @param string $property_name property name
  119. * @param mixed $value value
  120. *
  121. * @throws SmartyException
  122. */
  123. public function __set($property_name, $value)
  124. {
  125. $this->$property_name = $value;
  126. }
  127. /**
  128. * get extension object
  129. *
  130. * @param string $property_name property name
  131. *
  132. * @return mixed|Smarty_Template_Cached
  133. * @throws SmartyException
  134. */
  135. public function __get($property_name)
  136. {
  137. // object properties of runtime template extensions will start with '_'
  138. if ($property_name[ 0 ] == '_') {
  139. $class = 'Smarty_Internal_Runtime' . $this->upperCase($property_name);
  140. } else {
  141. $class = 'Smarty_Internal_Method_' . $this->upperCase($property_name);
  142. }
  143. if (!class_exists($class)) {
  144. return $this->$property_name = new Smarty_Internal_Undefined($class);
  145. }
  146. return $this->$property_name = new $class();
  147. }
  148. /**
  149. * Call error handler for undefined method
  150. *
  151. * @param string $name unknown method-name
  152. * @param array $args argument array
  153. *
  154. * @return mixed
  155. * @throws SmartyException
  156. */
  157. public function __call($name, $args)
  158. {
  159. return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), array($this));
  160. }
  161. }