[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class template 24 { 25 private $self_name; 26 27 public $use_cache = true; 28 29 private $blocks = array(); 30 private $values = array(); 31 32 protected $remove_php = true; 33 34 protected $tag_block = '<tpl:(%1$s)(?:(\s+.*?)>|>)(.*)</tpl:%1$s>'; 35 protected $tag_value = '{{tpl:(%s)(\s(.*?))?\}}'; 36 37 protected $tpl_path = array(); 38 protected $cache_dir; 39 40 protected $compile_stack = array(); 41 42 # Inclusion variables 43 private static $superglobals = array('GLOBALS','_SERVER','_GET','_POST','_COOKIE','_FILES','_ENV','_REQUEST','_SESSION'); 44 private static $_k; 45 private static $_n; 46 private static $_r; 47 48 public function __construct($cache_dir,$self_name) 49 { 50 $this->setCacheDir($cache_dir); 51 52 $this->self_name = $self_name; 53 $this->addValue('include',array($this,'includeFile')); 54 } 55 56 public function includeFile($attr) 57 { 58 if (!isset($attr['src'])) { return; } 59 60 $src = path::clean($attr['src']); 61 62 $tpl_file = $this->getFilePath($src); 63 if (!$tpl_file) { return; } 64 if (in_array($tpl_file,$this->compile_stack)) { return; } 65 66 return 67 '<?php echo '. 68 $this->self_name."->getData('".str_replace("'","\'",$src)."'); ?>"; 69 } 70 71 public function setPath() 72 { 73 $path = array(); 74 75 foreach (func_get_args() as $v) 76 { 77 if (is_array($v)) { 78 $path = array_merge($path,array_values($v)); 79 } else { 80 $path[] = $v; 81 } 82 } 83 84 foreach ($path as $k => $v) 85 { 86 if (($v = path::real($v)) === false) { 87 unset($path[$k]); 88 } 89 } 90 91 $this->tpl_path = array_unique($path); 92 } 93 94 public function getPath() 95 { 96 return $this->tpl_path; 97 } 98 99 public function setCacheDir($dir) 100 { 101 if (!is_dir($dir)) { 102 throw new Exception($dir.' is not a valid directory.'); 103 } 104 105 if (!is_writable($dir)) { 106 throw new Exception($dir.' is not writable.'); 107 } 108 109 $this->cache_dir = path::real($dir).'/'; 110 } 111 112 public function addBlock($name,$callback) 113 { 114 if (!is_callable($callback)) { 115 throw new Exception('No valid callback for '.$name); 116 } 117 118 $this->blocks[$name] = $callback; 119 } 120 121 public function addValue($name,$callback) 122 { 123 if (!is_callable($callback)) { 124 throw new Exception('No valid callback for '.$name); 125 } 126 127 $this->values[$name] = $callback; 128 } 129 130 public function getFile($file) 131 { 132 $tpl_file = $this->getFilePath($file); 133 134 if (!$tpl_file) { 135 throw new Exception('No template found for '.$file); 136 return false; 137 } 138 139 $file_md5 = md5($tpl_file); 140 $dest_file = sprintf('%s/%s/%s/%s/%s.php', 141 $this->cache_dir, 142 'cbtpl', 143 substr($file_md5,0,2), 144 substr($file_md5,2,2), 145 $file_md5 146 ); 147 148 $create_file = false; 149 150 if (!file_exists($dest_file)) { 151 $create_file = true; 152 } elseif (!$this->use_cache) { 153 $create_file = true; 154 } elseif (filemtime($tpl_file) > filemtime($dest_file)) { 155 $create_file = true; 156 } 157 158 if ($create_file) 159 { 160 files::makeDir(dirname($dest_file),true); 161 162 if (($fp = @fopen($dest_file,'wb')) === false) { 163 throw new Exception('Unable to create cache file'); 164 } 165 166 $fc = $this->compileFile($tpl_file); 167 fwrite($fp,$fc); 168 fclose($fp); 169 chmod($dest_file,fileperms(dirname($dest_file))); 170 } 171 return $dest_file; 172 } 173 174 public function getFilePath($file) 175 { 176 foreach ($this->tpl_path as $p) 177 { 178 if (file_exists($p.'/'.$file)) { 179 return $p.'/'.$file; 180 } 181 } 182 183 return false; 184 } 185 186 public function getData($________) 187 { 188 self::$_k = array_keys($GLOBALS); 189 190 foreach (self::$_k as self::$_n) { 191 if (!in_array(self::$_n,self::$superglobals)) { 192 global $self::$_n}; 193 } 194 } 195 196 ob_start(); 197 include $this->getFile($________); 198 self::$_r = ob_get_contents(); 199 ob_end_clean(); 200 201 return self::$_r; 202 } 203 204 protected function compileFile($file) 205 { 206 $fc = file_get_contents($file); 207 208 $this->compile_stack[] = $file; 209 210 # Remove every PHP tags 211 if ($this->remove_php) 212 { 213 $fc = preg_replace('/<\?(?=php|=|\s).*?\?>/ms','',$fc); 214 } 215 216 # Transform what could be considered as PHP short tags 217 $fc = preg_replace('/(<\?(?!php|=|\s))(.*?)(\?>)/ms', 218 '<?php echo "$1"; ?>$2<?php echo "$3"; ?>',$fc); 219 220 # Remove template comments <!-- #... --> 221 $fc = preg_replace('/(^\s*)?<!-- #(.*?)-->/ms','',$fc); 222 223 # Compile blocks 224 foreach ($this->blocks as $b => $f) { 225 $pattern = sprintf($this->tag_block,preg_quote($b,'#')); 226 227 $fc = preg_replace_callback('#'.$pattern.'#ms', 228 array($this,'compileBlock'),$fc); 229 } 230 231 # Compile values 232 foreach ($this->values as $v => $f) { 233 $pattern = sprintf($this->tag_value,preg_quote($v,'#')); 234 235 $fc = preg_replace_callback('#'.$pattern.'#ms', 236 array($this,'compileValue'),$fc); 237 } 238 239 return $fc; 240 } 241 242 protected function compileBlock($match) 243 { 244 $b = $match[1]; 245 $content = $match[3]; 246 $attr = $this->getAttrs($match[2]); 247 248 # Call block function 249 return call_user_func($this->blocks[$b],$attr,$content); 250 } 251 252 protected function compileValue($match) 253 { 254 $v = $match[1]; 255 $attr = isset($match[2]) ? $this->getAttrs($match[2]) : array(); 256 $str_attr = isset($match[2]) ? $match[2] : null; 257 258 return call_user_func($this->values[$v],$attr,ltrim($str_attr)); 259 } 260 261 protected function getAttrs($str) 262 { 263 $res = array(); 264 if (preg_match_all('|([a-zA-Z0-9_:-]+)="(.+?)"|ms',$str,$m) > 0) { 265 foreach ($m[1] as $i => $v) { 266 $res[$v] = $m[2][$i]; 267 } 268 } 269 return $res; 270 } 271 } 272 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |