smarty_internal_resource_extends.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Extends
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource Extends
  12. * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
  13. *
  14. * @package Smarty
  15. * @subpackage TemplateResources
  16. */
  17. class Smarty_Internal_Resource_Extends extends Smarty_Resource
  18. {
  19. /**
  20. * mbstring.overload flag
  21. *
  22. * @var int
  23. */
  24. public $mbstring_overload = 0;
  25. /**
  26. * populate Source Object with meta data from Resource
  27. *
  28. * @param Smarty_Template_Source $source source object
  29. * @param Smarty_Internal_Template $_template template object
  30. *
  31. * @throws SmartyException
  32. */
  33. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  34. {
  35. $uid = '';
  36. $sources = array();
  37. $components = explode('|', $source->name);
  38. $exists = true;
  39. foreach ($components as $component) {
  40. $s = Smarty_Resource::source(null, $source->smarty, $component);
  41. if ($s->type == 'php') {
  42. throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
  43. }
  44. $sources[$s->uid] = $s;
  45. $uid .= realpath($s->filepath);
  46. if ($_template && $_template->smarty->compile_check) {
  47. $exists = $exists && $s->exists;
  48. }
  49. }
  50. $source->components = $sources;
  51. $source->filepath = $s->filepath;
  52. $source->uid = sha1($uid);
  53. if ($_template && $_template->smarty->compile_check) {
  54. $source->timestamp = $s->timestamp;
  55. $source->exists = $exists;
  56. }
  57. // need the template at getContent()
  58. $source->template = $_template;
  59. }
  60. /**
  61. * populate Source Object with timestamp and exists from Resource
  62. *
  63. * @param Smarty_Template_Source $source source object
  64. */
  65. public function populateTimestamp(Smarty_Template_Source $source)
  66. {
  67. $source->exists = true;
  68. foreach ($source->components as $s) {
  69. $source->exists = $source->exists && $s->exists;
  70. }
  71. $source->timestamp = $s->timestamp;
  72. }
  73. /**
  74. * Load template's source from files into current template object
  75. *
  76. * @param Smarty_Template_Source $source source object
  77. *
  78. * @return string template source
  79. * @throws SmartyException if source cannot be loaded
  80. */
  81. public function getContent(Smarty_Template_Source $source)
  82. {
  83. if (!$source->exists) {
  84. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  85. }
  86. $_components = array_reverse($source->components);
  87. $_content = '';
  88. foreach ($_components as $_component) {
  89. // read content
  90. $_content .= $_component->content;
  91. }
  92. return $_content;
  93. }
  94. /**
  95. * Determine basename for compiled filename
  96. *
  97. * @param Smarty_Template_Source $source source object
  98. *
  99. * @return string resource's basename
  100. */
  101. public function getBasename(Smarty_Template_Source $source)
  102. {
  103. return str_replace(':', '.', basename($source->filepath));
  104. }
  105. }