CViewRenderer.php 3.4 KB

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