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