| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 /** 11 * class lmbMacroTemplate. 12 * 13 * @package macro 14 * @version $Id$ 15 */ 16 class lmbMacroTemplate 17 { 18 protected $file; 19 protected $cache_dir; 20 protected $vars = array(); 21 protected $includes = array(); 22 23 function __construct($file, $cache_dir) 24 { 25 $this->file = $file; 26 $this->cache_dir = $cache_dir; 27 } 28 29 function set($name, $value) 30 { 31 $this->vars[$name] = $value; 32 } 33 34 function render() 35 { 36 ob_start(); 37 $file = $this->_compile($class); 38 include($file); 39 $body = new $class($this->vars); 40 $body->paint(); 41 $out = ob_get_contents(); 42 ob_end_clean(); 43 return $out; 44 } 45 46 protected function _compile(&$class_name) 47 { 48 $contents = file_get_contents($this->file); 49 $prefix = 'p' . md5($this->file); 50 $class_name = "{$prefix}Body"; 51 52 $this->_processVars($contents); 53 $body = $this->_generateBody($class_name, $contents); 54 55 $compiled_file = $this->cache_dir . '/' . $prefix . '.php'; 56 file_put_contents($compiled_file, $body, LOCK_EX); 57 return $compiled_file; 58 } 59 60 protected function _generateBody($class_name, $contents) 61 { 62 $include_methods = ''; 63 foreach($this->includes as $name => $body) 64 $include_methods .= "$body\n"; 65 66 $code = <<<EOD 67 <?php 68 class {$class_name} 69 { 70 protected \$vars = array(); 71 72 function __construct(\$vars) 73 { 74 \$this->vars = \$vars; 75 } 76 77 function __get(\$name) 78 { 79 if(isset(\$this->vars[\$name])) 80 return \$this->vars[\$name]; 81 } 82 83 $include_methods 84 85 function paint(){ ?>$contents<?php } 86 } 87 ?> 88 EOD; 89 return $code; 90 } 91 92 protected function _processVars(&$contents) 93 { 94 $contents = str_replace('<?=', '<?php echo ', $contents); 95 $contents = preg_replace('~<\?(?!php|=)~', '<?php ', $contents); 96 $contents = str_replace('@$', '$this->', $contents); 97 $contents = preg_replace_callback('~\{(\$[^\W]+)\}~', array($this, '_varSugarCallback'), $contents); 98 $contents = preg_replace_callback('~\{([^\W]+\([^\}]+)\}~', array($this, '_functionSugarCallback'), $contents); 99 } 100 101 protected function _varSugarCallback($matches) 102 { 103 return '<?php echo ' . $matches[1] . ' ?>'; 104 } 105 106 protected function _functionSugarCallback($matches) 107 { 108 return '<?php echo ' . $matches[1] . ' ?>'; 109 } 110 111 protected function _includeCallback($matches) 112 { 113 if(!preg_match('~file=(?:"|\')([^"\']+)(?:"|\')~', $matches[0], $m)) 114 throw new lmbException('Invalid <%INCLUDE..>: ' . $matches[0]); 115 116 $file = lmbFs :: normalizePath($m[1]); 117 118 $args = ''; 119 if(preg_match('~args=(?:"|\')\(([^\)]+)\)(?:"|\')~', $matches[0], $m)) 120 $args = $m[1]; 121 122 $contents = file_get_contents($file); 123 $this->_processIncludes($contents); 124 $method_name = 'paintInclude' . sizeof($this->includes); 125 $method_body = "function $method_name(){ \$args = func_get_args();extract(\$args);?>$contents<?php }"; 126 127 $this->includes[$method_name] = $method_body; 128 129 return "<?php \$this->$method_name(array($args)); ?>"; 130 } 131 } 132 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Nov 22 03:48:54 2008 | Cross-referenced by PHPXref 0.7 |