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