1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255:
<?php
class FileTemplateLoader implements TemplateLoader
{
private $filepath;
private $real_filepath = '';
private $cache_filepath = '';
private $pictures_data_path = '';
private $module;
private $file;
private $templates_folder;
private $default_templates_folder;
private $theme_templates_folder;
public function __construct($identifier, TemplateData $data)
{
$this->filepath = $identifier;
$this->compute_real_file_path();
$this->compute_cache_file_path();
$data->put('PICTURES_DATA_PATH', $this->pictures_data_path);
}
private function compute_cache_file_path()
{
$template_folder = new Folder(PATH_TO_ROOT . '/cache/tpl/' . AppContext::get_current_user()->get_theme());
if (!$template_folder->exists())
mkdir(PATH_TO_ROOT . '/cache/tpl/' . AppContext::get_current_user()->get_theme());
$this->cache_filepath = PATH_TO_ROOT . '/cache/tpl/' . AppContext::get_current_user()->get_theme() . '/' . trim(str_replace(
array('/', '.', '..', 'tpl', 'templates'),
array('_', '', '', '', 'tpl'),
$this->real_filepath
), '_') . '.php';
}
public function load()
{
if (!$this->is_cache_file_up_to_date())
{
$this->generate_cache_file();
}
return $this->get_file_cache_content();
}
private function is_cache_file_up_to_date()
{
if (file_exists($this->cache_filepath))
{
return @filemtime($this->real_filepath) <= @filemtime($this->cache_filepath) && @filesize($this->cache_filepath) !== 0;
}
return false;
}
private function generate_cache_file()
{
$real_file_content = @file_get_contents($this->real_filepath);
if ($real_file_content === false)
{
throw new FileTemplateLoadingException($this->filepath, $this->real_filepath);
}
$parser = new TemplateSyntaxParser();
$result = $parser->parse($real_file_content);
try
{
$cache_file = new File($this->cache_filepath);
$cache_file->open(File::WRITE);
$cache_file->lock();
$cache_file->write($result);
$cache_file->unlock();
$cache_file->close();
$cache_file->change_chmod(0666);
}
catch(IOException $ex)
{
throw new TemplateLoadingException('The template file cache couldn\'t been written due to this problem :' . $ex->getMessage());
}
}
private function get_file_cache_content()
{
return @file_get_contents($this->cache_filepath);
}
private function compute_real_file_path()
{
if (TextHelper::strpos($this->filepath, '/') === 0)
{
if (file_exists(PATH_TO_ROOT . $this->filepath))
{
$this->get_template_real_filepaths_and_data_path(array(PATH_TO_ROOT . $this->filepath));
return;
}
}
$i = TextHelper::strpos($this->filepath, '/');
$this->module = TextHelper::substr($this->filepath, 0, $i);
$this->file = trim(TextHelper::substr($this->filepath, $i), '/');
$this->filename = trim(TextHelper::substr($this->filepath, TextHelper::strrpos($this->filepath, '/')));
$this->templates_folder = PATH_TO_ROOT . '/templates/';
$this->default_templates_folder = $this->templates_folder . 'default/';
$this->theme_templates_folder = $this->templates_folder . AppContext::get_current_user()->get_theme() . '/';
if (empty($this->module) || !TextHelper::strpos($this->filepath, '/'))
{
$this->get_template_paths();
}
else if (!in_array($this->module, array('default', 'admin', 'framework')))
{
$this->get_module_paths();
}
else
{
$this->get_kernel_paths();
}
}
private function get_template_paths()
{
$this->get_template_real_filepaths_and_data_path(array(
$this->theme_templates_folder . $this->filepath,
$this->theme_templates_folder . 'default/' . $this->filepath,
$this->default_templates_folder . $this->filepath
));
}
private function get_kernel_paths()
{
$this->get_template_real_filepaths_and_data_path(array(
$this->theme_templates_folder . 'default/' . str_replace('default/', '', $this->filepath),
($this->module == 'default' ? $this->templates_folder : $this->default_templates_folder) . $this->filepath
));
}
private function get_module_paths()
{
$this->get_template_real_filepaths_and_data_path(array(
$this->theme_templates_folder . 'modules/' . $this->module . '/' . $this->file,
PATH_TO_ROOT . '/' . $this->module . '/templates/' . $this->file
));
}
private function get_template_real_filepaths_and_data_path($paths)
{
foreach ($paths as $path)
{
$dirpath = dirname($path);
if (file_exists($dirpath . '/images'))
{
$this->pictures_data_path = $this->convert_to_tpl_path($dirpath);
break;
}
}
foreach ($paths as $path)
{
if (file_exists($path))
{
$this->real_filepath = $path;
break;
}
}
if (empty($this->real_filepath) && count($paths) > 0)
{
$this->real_filepath = $paths[count($paths) - 1];
}
}
private function convert_to_tpl_path($path_to_root_filepath)
{
return TPL_PATH_TO_ROOT . TextHelper::substr($path_to_root_filepath, TextHelper::strlen(PATH_TO_ROOT));
}
public function supports_caching()
{
return true;
}
public function get_cache_file_path()
{
if (!$this->is_cache_file_up_to_date())
{
$this->generate_cache_file();
}
return $this->cache_filepath;
}
public function get_pictures_data_path() {
return $this->pictures_data_path;
}
}
?>