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_resource_extends.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /* @var \Smarty_Template_Source $_s */
  41. $_s = Smarty_Template_Source::load(null, $source->smarty, $component);
  42. if ($_s->type == 'php') {
  43. throw new SmartyException("Resource type {$_s->type} cannot be used with the extends resource type");
  44. }
  45. $sources[ $_s->uid ] = $_s;
  46. $uid .= $_s->filepath;
  47. if ($_template) {
  48. $exists = $exists && $_s->exists;
  49. }
  50. }
  51. $source->components = $sources;
  52. $source->filepath = $_s->filepath;
  53. $source->uid = sha1($uid);
  54. $source->exists = $exists;
  55. if ($_template) {
  56. $source->timestamp = $_s->timestamp;
  57. }
  58. }
  59. /**
  60. * populate Source Object with timestamp and exists from Resource
  61. *
  62. * @param Smarty_Template_Source $source source object
  63. */
  64. public function populateTimestamp(Smarty_Template_Source $source)
  65. {
  66. $source->exists = true;
  67. /* @var \Smarty_Template_Source $_s */
  68. foreach ($source->components as $_s) {
  69. $source->exists = $source->exists && $_s->exists;
  70. }
  71. $source->timestamp = $source->exists ? $_s->getTimeStamp() : false;
  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. /* @var \Smarty_Template_Source $_s */
  89. foreach ($_components as $_s) {
  90. // read content
  91. $_content .= $_s->getContent();
  92. }
  93. return $_content;
  94. }
  95. /**
  96. * Determine basename for compiled filename
  97. *
  98. * @param Smarty_Template_Source $source source object
  99. *
  100. * @return string resource's basename
  101. */
  102. public function getBasename(Smarty_Template_Source $source)
  103. {
  104. return str_replace(':', '.', basename($source->filepath));
  105. }
  106. /*
  107. * Disable timestamp checks for extends resource.
  108. * The individual source components will be checked.
  109. *
  110. * @return bool
  111. */
  112. public function checkTimestamps()
  113. {
  114. return false;
  115. }
  116. }