1: <?php
2: /**
3: * CViewRenderer class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CViewRenderer is the base class for view renderer classes.
13: *
14: * A view renderer is an application component that renders views written
15: * in a customized syntax.
16: *
17: * Once installing a view renderer as a 'viewRenderer' application component,
18: * the normal view rendering process will be intercepted by the renderer.
19: * The renderer will first parse the source view file and then render the
20: * the resulting view file.
21: *
22: * Parsing results are saved as temporary files that may be stored
23: * under the application runtime directory or together with the source view file.
24: *
25: * @author Steve Heyns http://customgothic.com/
26: * @author Qiang Xue <qiang.xue@gmail.com>
27: * @package system.web.renderers
28: * @since 1.0
29: */
30: abstract class CViewRenderer extends CApplicationComponent implements IViewRenderer
31: {
32: /**
33: * @var boolean whether to store the parsing results in the application's
34: * runtime directory. Defaults to true. If false, the parsing results will
35: * be saved as files under the same directory as the source view files and the
36: * file names will be the source file names appended with letter 'c'.
37: */
38: public $useRuntimePath=true;
39: /**
40: * @var integer the chmod permission for temporary directories and files
41: * generated during parsing. Defaults to 0755 (owner rwx, group rx and others rx).
42: */
43: public $filePermission=0755;
44: /**
45: * @var string the extension name of the view file. Defaults to '.php'.
46: */
47: public $fileExtension='.php';
48:
49: /**
50: * Parses the source view file and saves the results as another file.
51: * @param string $sourceFile the source view file path
52: * @param string $viewFile the resulting view file path
53: */
54: abstract protected function generateViewFile($sourceFile,$viewFile);
55:
56: /**
57: * Renders a view file.
58: * This method is required by {@link IViewRenderer}.
59: * @param CBaseController $context the controller or widget who is rendering the view file.
60: * @param string $sourceFile the view file path
61: * @param mixed $data the data to be passed to the view
62: * @param boolean $return whether the rendering result should be returned
63: * @return mixed the rendering result, or null if the rendering result is not needed.
64: */
65: public function renderFile($context,$sourceFile,$data,$return)
66: {
67: if(!is_file($sourceFile) || ($file=realpath($sourceFile))===false)
68: throw new CException(Yii::t('yii','View file "{file}" does not exist.',array('{file}'=>$sourceFile)));
69: $viewFile=$this->getViewFile($sourceFile);
70: if(@filemtime($sourceFile)>@filemtime($viewFile))
71: {
72: $this->generateViewFile($sourceFile,$viewFile);
73: @chmod($viewFile,$this->filePermission);
74: }
75: return $context->renderInternal($viewFile,$data,$return);
76: }
77:
78: /**
79: * Generates the resulting view file path.
80: * @param string $file source view file path
81: * @return string resulting view file path
82: */
83: protected function getViewFile($file)
84: {
85: if($this->useRuntimePath)
86: {
87: $crc=sprintf('%x', crc32(get_class($this).Yii::getVersion().dirname($file)));
88: $viewFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$crc.DIRECTORY_SEPARATOR.basename($file);
89: if(!is_file($viewFile))
90: @mkdir(dirname($viewFile),$this->filePermission,true);
91: return $viewFile;
92: }
93: else
94: return $file.'c';
95: }
96: }
97: