Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

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