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_method_registerdefaulttemplatehandler.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Smarty Method RegisterDefaultTemplateHandler
  4. *
  5. * Smarty::registerDefaultTemplateHandler() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_RegisterDefaultTemplateHandler
  12. {
  13. /**
  14. * Valid for Smarty and template object
  15. *
  16. * @var int
  17. */
  18. public $objMap = 3;
  19. /**
  20. * Register template default handler
  21. *
  22. * @api Smarty::registerDefaultTemplateHandler()
  23. *
  24. * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
  25. * @param callable $callback class/method name
  26. *
  27. * @return \Smarty|\Smarty_Internal_Template
  28. * @throws SmartyException if $callback is not callable
  29. */
  30. public function registerDefaultTemplateHandler(Smarty_Internal_TemplateBase $obj, $callback)
  31. {
  32. $smarty = $obj->_getSmartyObj();
  33. if (is_callable($callback)) {
  34. $smarty->default_template_handler_func = $callback;
  35. } else {
  36. throw new SmartyException('Default template handler not callable');
  37. }
  38. return $obj;
  39. }
  40. /**
  41. * get default content from template or config resource handler
  42. *
  43. * @param Smarty_Template_Source $source
  44. *
  45. * @throws \SmartyException
  46. */
  47. public static function _getDefaultTemplate(Smarty_Template_Source $source)
  48. {
  49. if ($source->isConfig) {
  50. $default_handler = $source->smarty->default_config_handler_func;
  51. } else {
  52. $default_handler = $source->smarty->default_template_handler_func;
  53. }
  54. $_content = $_timestamp = null;
  55. $_return = call_user_func_array($default_handler,
  56. array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty));
  57. if (is_string($_return)) {
  58. $source->exists = is_file($_return);
  59. if ($source->exists) {
  60. $source->timestamp = filemtime($_return);
  61. } else {
  62. throw new SmartyException('Default handler: Unable to load ' .
  63. ($source->isConfig ? 'config' : 'template') .
  64. " default file '{$_return}' for '{$source->type}:{$source->name}'");
  65. }
  66. $source->name = $source->filepath = $_return;
  67. $source->uid = sha1($source->filepath);
  68. } elseif ($_return === true) {
  69. $source->content = $_content;
  70. $source->exists = true;
  71. $source->uid = $source->name = sha1($_content);
  72. $source->handler = Smarty_Resource::load($source->smarty, 'eval');
  73. } else {
  74. $source->exists = false;
  75. throw new SmartyException('Default handler: No ' . ($source->isConfig ? 'config' : 'template') .
  76. " default content for '{$source->type}:{$source->name}'");
  77. }
  78. }
  79. }