4 * Smarty Internal Plugin Resource Extends
6 * Implements the file system as resource for Smarty which does extend a chain of template files templates
9 * @subpackage TemplateResources
14 * Smarty Internal Plugin Resource Extends
16 class Smarty_Internal_Resource_Extends {
17 public function __construct($smarty)
19 $this->smarty = $smarty;
20 $this->_rdl = preg_quote($smarty->right_delimiter);
21 $this->_ldl = preg_quote($smarty->left_delimiter);
23 // classes used for compiling Smarty templates from file resource
24 public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
25 public $template_lexer_class = 'Smarty_Internal_Templatelexer';
26 public $template_parser_class = 'Smarty_Internal_Templateparser';
28 public $usesCompiler = true;
29 public $isEvaluated = false;
30 public $allFilepaths = array();
33 * Return flag if template source is existing
35 * @param object $_template template object
36 * @return boolean result
38 public function isExisting($_template)
40 $_template->getTemplateFilepath();
41 foreach ($this->allFilepaths as $_filepath) {
42 if ($_filepath === false) {
49 * Get filepath to template source
51 * @param object $_template template object
52 * @return string filepath to template source file
54 public function getTemplateFilepath($_template)
57 $_files = explode('|', $_template->resource_name);
58 foreach ($_files as $_file) {
59 $_filepath = $_template->buildTemplateFilepath ($_file);
60 if ($_filepath !== false) {
61 if (is_object($_template->smarty->security_policy)) {
62 $_template->smarty->security_policy->isTrustedResourceDir($_filepath);
65 $sha1String .= $_filepath;
66 $this->allFilepaths[$_file] = $_filepath;
68 $_template->templateUid = sha1($sha1String);
73 * Get timestamp to template source
75 * @param object $_template template object
76 * @return integer timestamp of template source file
78 public function getTemplateTimestamp($_template)
80 return filemtime($_template->getTemplateFilepath());
84 * Read template source from file
86 * @param object $_template template object
87 * @return string content of template source file
89 public function getTemplateSource($_template)
91 $this->template = $_template;
92 $_files = array_reverse($this->allFilepaths);
93 $_first = reset($_files);
94 $_last = end($_files);
95 foreach ($_files as $_file => $_filepath) {
96 if ($_filepath === false) {
97 throw new SmartyException("Unable to load template 'file : {$_file}'");
100 if ($_filepath != $_first) {
101 $_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath),'file');
103 $_template->template_filepath = $_filepath;
104 $_content = file_get_contents($_filepath);
105 if ($_filepath != $_last) {
106 if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $_open) !=
107 preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $_close)) {
108 $this->smarty->triggerError("unmatched {block} {/block} pairs in file '$_filepath'");
110 preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
111 $_result_count = count($_result[0]);
113 while ($_start < $_result_count) {
116 while ($_level != 0) {
118 if (!strpos($_result[0][$_start + $_end][0], '/')) {
124 $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
125 substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
126 Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $_template, $_filepath);
127 $_start = $_start + $_end + 1;
130 $_template->template_source = $_content;
138 * Get filepath to compiled template
140 * @param object $_template template object
141 * @return string return path to compiled template
143 public function getCompiledFilepath($_template)
145 $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
146 $_files = explode('|', $_template->resource_name);
147 // calculate Uid if not already done
148 if ($_template->templateUid == '') {
149 $_template->getTemplateFilepath();
151 $_filepath = $_template->templateUid;
152 // if use_sub_dirs, break file into directories
153 if ($_template->smarty->use_sub_dirs) {
154 $_filepath = substr($_filepath, 0, 2) . DS
155 . substr($_filepath, 2, 2) . DS
156 . substr($_filepath, 4, 2) . DS
159 $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
160 if (isset($_compile_id)) {
161 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
163 if ($_template->caching) {
168 $_compile_dir = $_template->smarty->compile_dir;
169 if (substr($_compile_dir, -1) != DS) {
172 return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';