CPradoViewRenderer.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * CPradoViewRenderer class file.
  4. *
  5. * @author Steve Heyns http://customgothic.com/
  6. * @author Qiang Xue <qiang.xue@gmail.com>
  7. * @link http://www.yiiframework.com/
  8. * @copyright 2008-2013 Yii Software LLC
  9. * @license http://www.yiiframework.com/license/
  10. */
  11. /**
  12. * CPradoViewRenderer implements a view renderer that allows users to use a template syntax similar to PRADO templates.
  13. *
  14. * To use CPradoViewRenderer, configure it as an application component named "viewRenderer" in the application configuration:
  15. * <pre>
  16. * array(
  17. * 'components'=>array(
  18. * ......
  19. * 'viewRenderer'=>array(
  20. * 'class'=>'CPradoViewRenderer',
  21. * ),
  22. * ),
  23. * )
  24. * </pre>
  25. *
  26. * CPradoViewRenderer allows you to write view files with the following syntax:
  27. * <pre>
  28. * // PHP tags:
  29. * <%= expression %>
  30. * // <?php echo expression ?>
  31. * <% statement %>
  32. * // <?php statement ?></li>
  33. *
  34. * // component tags:
  35. * <com:WigetClass name1="value1" name2='value2' name3={value3} >
  36. * // <?php $this->beginWidget('WigetClass',
  37. * // array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3)); ?>
  38. * </com:WigetClass >
  39. * // <?php $this->endWidget('WigetClass'); ?>
  40. * <com:WigetClass name1="value1" name2='value2' name3={value3} />
  41. * // <?php $this->widget('WigetClass',
  42. * // array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3)); ?>
  43. *
  44. * // cache tags:
  45. * <cache:fragmentID name1="value1" name2='value2' name3={value3} >
  46. * // <?php if($this->beginCache('fragmentID',
  47. * // array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3))): ?>
  48. * </cache:fragmentID >
  49. * // <?php $this->endCache('fragmentID'); endif; ?>
  50. *
  51. * // clip tags:
  52. * <clip:clipID >
  53. * // <?php $this->beginClip('clipID'); ?>
  54. * </clip:clipID >
  55. * // <?php $this->endClip('clipID'); ?>
  56. *
  57. * // comment tags:
  58. * <!--- comments --->
  59. * // the whole tag will be stripped off
  60. * </pre>
  61. *
  62. * @author Steve Heyns http://customgothic.com/
  63. * @author Qiang Xue <qiang.xue@gmail.com>
  64. * @package system.web.renderers
  65. * @since 1.0
  66. */
  67. class CPradoViewRenderer extends CViewRenderer
  68. {
  69. private $_input;
  70. private $_output;
  71. private $_sourceFile;
  72. /**
  73. * Parses the source view file and saves the results as another file.
  74. * This method is required by the parent class.
  75. * @param string $sourceFile the source view file path
  76. * @param string $viewFile the resulting view file path
  77. */
  78. protected function generateViewFile($sourceFile,$viewFile)
  79. {
  80. static $regexRules=array(
  81. '<%=?\s*(.*?)\s*%>', // PHP statements or expressions
  82. '<\/?(com|cache|clip):([\w\.]+)\s*((?:\s*\w+\s*=\s*\'.*?(?<!\\\\)\'|\s*\w+\s*=\s*".*?(?<!\\\\)"|\s*\w+\s*=\s*\{.*?\})*)\s*\/?>', // component tags
  83. '<!---.*?--->', // template comments
  84. );
  85. $this->_sourceFile=$sourceFile;
  86. $this->_input=file_get_contents($sourceFile);
  87. $n=preg_match_all('/'.implode('|',$regexRules).'/msS',$this->_input,$matches,PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
  88. $textStart=0;
  89. $this->_output="<?php /* source file: $sourceFile */ ?>\n";
  90. for($i=0;$i<$n;++$i)
  91. {
  92. $match=&$matches[$i];
  93. $str=$match[0][0];
  94. $matchStart=$match[0][1];
  95. $matchEnd=$matchStart+strlen($str)-1;
  96. if($matchStart>$textStart)
  97. $this->_output.=substr($this->_input,$textStart,$matchStart-$textStart);
  98. $textStart=$matchEnd+1;
  99. if(strpos($str,'<com:')===0) // opening component tag
  100. {
  101. $type=$match[3][0];
  102. if($str[strlen($str)-2]!=='/') // open tag
  103. $this->_output.=$this->processBeginWidget($type,$match[4][0],$match[2][1]);
  104. else
  105. $this->_output.=$this->processWidget($type,$match[4][0],$match[2][1]);
  106. }
  107. elseif(strpos($str,'</com:')===0) // closing component tag
  108. $this->_output.=$this->processEndWidget($match[3][0],$match[2][1]);
  109. elseif(strpos($str,'<cache:')===0) // opening cache tag
  110. {
  111. $id=$match[3][0];
  112. if($str[strlen($str)-2]!=='/') // open tag
  113. $this->_output.=$this->processBeginCache($id,$match[4][0],$match[2][1]);
  114. else
  115. $this->_output.=$this->processCache($id,$match[4][0],$match[2][1]);
  116. }
  117. elseif(strpos($str,'</cache:')===0) // closing cache tag
  118. $this->_output.=$this->processEndCache($match[3][0],$match[2][1]);
  119. elseif(strpos($str,'<clip:')===0) // opening clip tag
  120. {
  121. $id=$match[3][0];
  122. if($str[strlen($str)-2]!=='/') // open tag
  123. $this->_output.=$this->processBeginClip($id,$match[4][0],$match[2][1]);
  124. else
  125. $this->_output.=$this->processClip($id,$match[4][0],$match[2][1]);
  126. }
  127. elseif(strpos($str,'</clip:')===0) // closing clip tag
  128. $this->_output.=$this->processEndClip($match[3][0],$match[2][1]);
  129. elseif(strpos($str,'<%=')===0) // expression
  130. $this->_output.=$this->processExpression($match[1][0],$match[1][1]);
  131. elseif(strpos($str,'<%')===0) // statement
  132. $this->_output.=$this->processStatement($match[1][0],$match[1][1]);
  133. }
  134. if($textStart<strlen($this->_input))
  135. $this->_output.=substr($this->_input,$textStart);
  136. file_put_contents($viewFile,$this->_output);
  137. }
  138. /*
  139. * @param string $type type
  140. * @param string $attributes attributes
  141. * @param string $offset offset
  142. */
  143. private function processWidget($type,$attributes,$offset)
  144. {
  145. $attrs=$this->processAttributes($attributes);
  146. if(empty($attrs))
  147. return $this->generatePhpCode("\$this->widget('$type');",$offset);
  148. else
  149. return $this->generatePhpCode("\$this->widget('$type', array($attrs));",$offset);
  150. }
  151. /*
  152. * @param string $type type
  153. * @param string $attributes attributes
  154. * @param string $offset offset
  155. */
  156. private function processBeginWidget($type,$attributes,$offset)
  157. {
  158. $attrs=$this->processAttributes($attributes);
  159. if(empty($attrs))
  160. return $this->generatePhpCode("\$this->beginWidget('$type');",$offset);
  161. else
  162. return $this->generatePhpCode("\$this->beginWidget('$type', array($attrs));",$offset);
  163. }
  164. /*
  165. * @param string $type type
  166. * @param string $offset offset
  167. */
  168. private function processEndWidget($type,$offset)
  169. {
  170. return $this->generatePhpCode("\$this->endWidget('$type');",$offset);
  171. }
  172. /*
  173. * @param string $id id
  174. * @param string $attributes attributes
  175. * @param string $offset offset
  176. */
  177. private function processCache($id,$attributes,$offset)
  178. {
  179. return $this->processBeginCache($id,$attributes,$offset) . $this->processEndCache($id,$offset);
  180. }
  181. /*
  182. * @param string $id id
  183. * @param string $attributes attributes
  184. * @param string $offset offset
  185. */
  186. private function processBeginCache($id,$attributes,$offset)
  187. {
  188. $attrs=$this->processAttributes($attributes);
  189. if(empty($attrs))
  190. return $this->generatePhpCode("if(\$this->beginCache('$id')):",$offset);
  191. else
  192. return $this->generatePhpCode("if(\$this->beginCache('$id', array($attrs))):",$offset);
  193. }
  194. /*
  195. * @param string $id id
  196. * @param string $offset offset
  197. */
  198. private function processEndCache($id,$offset)
  199. {
  200. return $this->generatePhpCode("\$this->endCache('$id'); endif;",$offset);
  201. }
  202. /*
  203. * @param string $id id
  204. * @param string $attributes attributes
  205. * @param string $offset offset
  206. */
  207. private function processClip($id,$attributes,$offset)
  208. {
  209. return $this->processBeginClip($id,$attributes,$offset) . $this->processEndClip($id,$offset);
  210. }
  211. /*
  212. * @param string $id id
  213. * @param string $attributes attributes
  214. * @param string $offset offset
  215. */
  216. private function processBeginClip($id,$attributes,$offset)
  217. {
  218. $attrs=$this->processAttributes($attributes);
  219. if(empty($attrs))
  220. return $this->generatePhpCode("\$this->beginClip('$id');",$offset);
  221. else
  222. return $this->generatePhpCode("\$this->beginClip('$id', array($attrs));",$offset);
  223. }
  224. /*
  225. * @param string $id id
  226. * @param string $offset offset
  227. */
  228. private function processEndClip($id,$offset)
  229. {
  230. return $this->generatePhpCode("\$this->endClip('$id');",$offset);
  231. }
  232. /*
  233. * @param string $expression expression
  234. * @param string $offset offset
  235. */
  236. private function processExpression($expression,$offset)
  237. {
  238. return $this->generatePhpCode('echo '.$expression,$offset);
  239. }
  240. /*
  241. * @param string $statement statement
  242. * @param string $offset offset
  243. */
  244. private function processStatement($statement,$offset)
  245. {
  246. return $this->generatePhpCode($statement,$offset);
  247. }
  248. /*
  249. * @param string $code code
  250. * @param string $offset offset
  251. */
  252. private function generatePhpCode($code,$offset)
  253. {
  254. $line=$this->getLineNumber($offset);
  255. $code=str_replace('__FILE__',var_export($this->_sourceFile,true),$code);
  256. return "<?php /* line $line */ $code ?>";
  257. }
  258. /*
  259. * @param string $str str
  260. */
  261. private function processAttributes($str)
  262. {
  263. static $pattern='/(\w+)\s*=\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)"|\{.*?\})/msS';
  264. $attributes=array();
  265. $n=preg_match_all($pattern,$str,$matches,PREG_SET_ORDER);
  266. for($i=0;$i<$n;++$i)
  267. {
  268. $match=&$matches[$i];
  269. $name=$match[1];
  270. $value=$match[2];
  271. if($value[0]==='{')
  272. $attributes[]="'$name'=>".str_replace('__FILE__',$this->_sourceFile,substr($value,1,-1));
  273. else
  274. $attributes[]="'$name'=>$value";
  275. }
  276. return implode(', ',$attributes);
  277. }
  278. /*
  279. * @param string $offset offset
  280. */
  281. private function getLineNumber($offset)
  282. {
  283. return count(explode("\n",substr($this->_input,0,$offset)));
  284. }
  285. }