CCodeGenerator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * CCodeGenerator 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. * CCodeGenerator is the base class for code generator classes.
  12. *
  13. * CCodeGenerator is a controller that predefines several actions for code generation purpose.
  14. * Derived classes mainly need to configure the {@link codeModel} property
  15. * override the {@link getSuccessMessage} method. The former specifies which
  16. * code model (extending {@link CCodeModel}) that this generator should use,
  17. * while the latter should return a success message to be displayed when
  18. * code files are successfully generated.
  19. *
  20. * @property string $pageTitle The page title.
  21. * @property string $viewPath The view path of the generator.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @package system.gii
  25. * @since 1.1.2
  26. */
  27. class CCodeGenerator extends CController
  28. {
  29. /**
  30. * @var string the layout to be used by the generator. Defaults to 'generator'.
  31. */
  32. public $layout='generator';
  33. /**
  34. * @var array a list of available code templates (name=>path)
  35. */
  36. public $templates=array();
  37. /**
  38. * @var string the code model class. This can be either a class name (if it can be autoloaded)
  39. * or a path alias referring to the class file.
  40. * Child classes must configure this property with a concrete value.
  41. */
  42. public $codeModel;
  43. private $_viewPath;
  44. /**
  45. * @return string the page title
  46. */
  47. public function getPageTitle()
  48. {
  49. return 'Gii - '.ucfirst($this->id).' Generator';
  50. }
  51. /**
  52. * The code generation action.
  53. * This is the action that displays the code generation interface.
  54. * Child classes mainly need to provide the 'index' view for collecting user parameters
  55. * for code generation.
  56. */
  57. public function actionIndex()
  58. {
  59. $model=$this->prepare();
  60. if($model->files!=array() && isset($_POST['generate'], $_POST['answers']))
  61. {
  62. $model->answers=$_POST['answers'];
  63. $model->status=$model->save() ? CCodeModel::STATUS_SUCCESS : CCodeModel::STATUS_ERROR;
  64. }
  65. $this->render('index',array(
  66. 'model'=>$model,
  67. ));
  68. }
  69. /**
  70. * The code preview action.
  71. * This action shows up the specified generated code.
  72. * @throws CHttpException if unable to find code generated.
  73. */
  74. public function actionCode()
  75. {
  76. $model=$this->prepare();
  77. if(isset($_GET['id']) && isset($model->files[$_GET['id']]))
  78. {
  79. $this->renderPartial('/common/code', array(
  80. 'file'=>$model->files[$_GET['id']],
  81. ));
  82. }
  83. else
  84. throw new CHttpException(404,'Unable to find the code you requested.');
  85. }
  86. /**
  87. * The code diff action.
  88. * This action shows up the difference between the newly generated code and the corresponding existing code.
  89. * @throws CHttpException if unable to find code generated.
  90. */
  91. public function actionDiff()
  92. {
  93. Yii::import('gii.components.TextDiff');
  94. $model=$this->prepare();
  95. if(isset($_GET['id']) && isset($model->files[$_GET['id']]))
  96. {
  97. $file=$model->files[$_GET['id']];
  98. if(!in_array($file->type,array('php', 'txt','js','css','sql')))
  99. $diff=false;
  100. elseif($file->operation===CCodeFile::OP_OVERWRITE)
  101. $diff=TextDiff::compare(file_get_contents($file->path), $file->content);
  102. else
  103. $diff='';
  104. $this->renderPartial('/common/diff',array(
  105. 'file'=>$file,
  106. 'diff'=>$diff,
  107. ));
  108. }
  109. else
  110. throw new CHttpException(404,'Unable to find the code you requested.');
  111. }
  112. /**
  113. * Returns the view path of the generator.
  114. * The "views" directory under the directory containing the generator class file will be returned.
  115. * @return string the view path of the generator
  116. */
  117. public function getViewPath()
  118. {
  119. if($this->_viewPath===null)
  120. {
  121. $class=new ReflectionClass(get_class($this));
  122. $this->_viewPath=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'views';
  123. }
  124. return $this->_viewPath;
  125. }
  126. /**
  127. * @param string $value the view path of the generator.
  128. */
  129. public function setViewPath($value)
  130. {
  131. $this->_viewPath=$value;
  132. }
  133. /**
  134. * Prepares the code model.
  135. */
  136. protected function prepare()
  137. {
  138. if($this->codeModel===null)
  139. throw new CException(get_class($this).'.codeModel property must be specified.');
  140. $modelClass=Yii::import($this->codeModel,true);
  141. $model=new $modelClass;
  142. $model->loadStickyAttributes();
  143. if(isset($_POST[$modelClass]))
  144. {
  145. $model->attributes=$_POST[$modelClass];
  146. $model->status=CCodeModel::STATUS_PREVIEW;
  147. if($model->validate())
  148. {
  149. $model->saveStickyAttributes();
  150. $model->prepare();
  151. }
  152. }
  153. return $model;
  154. }
  155. }