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_template_source.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Smarty Resource Data Object
  4. * Meta Data Container for Template Files
  5. *
  6. * @package Smarty
  7. * @subpackage TemplateResources
  8. * @author Rodney Rehm
  9. *
  10. */
  11. class Smarty_Template_Source
  12. {
  13. /**
  14. * Unique Template ID
  15. *
  16. * @var string
  17. */
  18. public $uid = null;
  19. /**
  20. * Template Resource (Smarty_Internal_Template::$template_resource)
  21. *
  22. * @var string
  23. */
  24. public $resource = null;
  25. /**
  26. * Resource Type
  27. *
  28. * @var string
  29. */
  30. public $type = null;
  31. /**
  32. * Resource Name
  33. *
  34. * @var string
  35. */
  36. public $name = null;
  37. /**
  38. * Source Filepath
  39. *
  40. * @var string
  41. */
  42. public $filepath = null;
  43. /**
  44. * Source Timestamp
  45. *
  46. * @var integer
  47. */
  48. public $timestamp = null;
  49. /**
  50. * Source Existence
  51. *
  52. * @var boolean
  53. */
  54. public $exists = false;
  55. /**
  56. * Source File Base name
  57. *
  58. * @var string
  59. */
  60. public $basename = null;
  61. /**
  62. * The Components an extended template is made of
  63. *
  64. * @var \Smarty_Template_Source[]
  65. */
  66. public $components = null;
  67. /**
  68. * Resource Handler
  69. *
  70. * @var \Smarty_Resource
  71. */
  72. public $handler = null;
  73. /**
  74. * Smarty instance
  75. *
  76. * @var Smarty
  77. */
  78. public $smarty = null;
  79. /**
  80. * Resource is source
  81. *
  82. * @var bool
  83. */
  84. public $isConfig = false;
  85. /**
  86. * Template source content eventually set by default handler
  87. *
  88. * @var string
  89. */
  90. public $content = null;
  91. /**
  92. * Name of the Class to compile this resource's contents with
  93. *
  94. * @var string
  95. */
  96. public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
  97. /**
  98. * Name of the Class to tokenize this resource's contents with
  99. *
  100. * @var string
  101. */
  102. public $template_lexer_class = 'Smarty_Internal_Templatelexer';
  103. /**
  104. * Name of the Class to parse this resource's contents with
  105. *
  106. * @var string
  107. */
  108. public $template_parser_class = 'Smarty_Internal_Templateparser';
  109. /**
  110. * create Source Object container
  111. *
  112. * @param Smarty $smarty Smarty instance this source object belongs to
  113. * @param string $resource full template_resource
  114. * @param string $type type of resource
  115. * @param string $name resource name
  116. *
  117. * @throws \SmartyException
  118. * @internal param \Smarty_Resource $handler Resource Handler this source object communicates with
  119. */
  120. public function __construct(Smarty $smarty, $resource, $type, $name)
  121. {
  122. $this->handler =
  123. isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] :
  124. Smarty_Resource::load($smarty, $type);
  125. $this->smarty = $smarty;
  126. $this->resource = $resource;
  127. $this->type = $type;
  128. $this->name = $name;
  129. }
  130. /**
  131. * initialize Source Object for given resource
  132. * Either [$_template] or [$smarty, $template_resource] must be specified
  133. *
  134. * @param Smarty_Internal_Template $_template template object
  135. * @param Smarty $smarty smarty object
  136. * @param string $template_resource resource identifier
  137. *
  138. * @return Smarty_Template_Source Source Object
  139. * @throws SmartyException
  140. */
  141. public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
  142. $template_resource = null)
  143. {
  144. if ($_template) {
  145. $smarty = $_template->smarty;
  146. $template_resource = $_template->template_resource;
  147. }
  148. if (empty($template_resource)) {
  149. throw new SmartyException('Source: Missing name');
  150. }
  151. // parse resource_name, load resource handler, identify unique resource name
  152. if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]([\s\S]*)$/', $template_resource, $match)) {
  153. $type = $match[ 1 ];
  154. $name = $match[ 2 ];
  155. } else {
  156. // no resource given, use default
  157. // or single character before the colon is not a resource type, but part of the filepath
  158. $type = $smarty->default_resource_type;
  159. $name = $template_resource;
  160. }
  161. // create new source object
  162. $source = new Smarty_Template_Source($smarty, $template_resource, $type, $name);
  163. $source->handler->populate($source, $_template);
  164. if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
  165. Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
  166. $source->handler->populate($source, $_template);
  167. }
  168. return $source;
  169. }
  170. /**
  171. * Get source time stamp
  172. *
  173. * @return int
  174. */
  175. public function getTimeStamp()
  176. {
  177. if (!isset($this->timestamp)) {
  178. $this->handler->populateTimestamp($this);
  179. }
  180. return $this->timestamp;
  181. }
  182. /**
  183. * Get source content
  184. *
  185. * @return string
  186. * @throws \SmartyException
  187. */
  188. public function getContent()
  189. {
  190. return isset($this->content) ? $this->content : $this->handler->getContent($this);
  191. }
  192. }