ControllerCode.php
3.28 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
<?php
class ControllerCode extends CCodeModel
{
public $controller;
public $baseClass='Controller';
public $actions='index';
public function rules()
{
return array_merge(parent::rules(), array(
array('controller, actions, baseClass', 'filter', 'filter'=>'trim'),
array('controller, baseClass', 'required'),
array('controller', 'match', 'pattern'=>'/^\w+[\w+\\/]*$/', 'message'=>'{attribute} should only contain word characters and slashes.'),
array('actions', 'match', 'pattern'=>'/^\w+[\w\s,]*$/', 'message'=>'{attribute} should only contain word characters, spaces and commas.'),
array('baseClass', 'match', 'pattern'=>'/^[a-zA-Z_]\w*$/', 'message'=>'{attribute} should only contain word characters.'),
array('baseClass', 'validateReservedWord', 'skipOnError'=>true),
array('baseClass, actions', 'sticky'),
));
}
public function attributeLabels()
{
return array_merge(parent::attributeLabels(), array(
'baseClass'=>'Base Class',
'controller'=>'Controller ID',
'actions'=>'Action IDs',
));
}
public function requiredTemplates()
{
return array(
'controller.php',
'view.php',
);
}
public function successMessage()
{
$link=CHtml::link('try it now', Yii::app()->createUrl($this->controller), array('target'=>'_blank'));
return "The controller has been generated successfully. You may $link.";
}
public function prepare()
{
$this->files=array();
$templatePath=$this->templatePath;
$this->files[]=new CCodeFile(
$this->controllerFile,
$this->render($templatePath.'/controller.php')
);
foreach($this->getActionIDs() as $action)
{
$this->files[]=new CCodeFile(
$this->getViewFile($action),
$this->render($templatePath.'/view.php', array('action'=>$action))
);
}
}
public function getActionIDs()
{
$actions=preg_split('/[\s,]+/',$this->actions,-1,PREG_SPLIT_NO_EMPTY);
$actions=array_unique($actions);
sort($actions);
return $actions;
}
public function getControllerClass()
{
if(($pos=strrpos($this->controller,'/'))!==false)
return ucfirst(substr($this->controller,$pos+1)).'Controller';
else
return ucfirst($this->controller).'Controller';
}
public function getModule()
{
if(($pos=strpos($this->controller,'/'))!==false)
{
$id=substr($this->controller,0,$pos);
if(($module=Yii::app()->getModule($id))!==null)
return $module;
}
return Yii::app();
}
public function getControllerID()
{
if($this->getModule()!==Yii::app())
$id=substr($this->controller,strpos($this->controller,'/')+1);
else
$id=$this->controller;
if(($pos=strrpos($id,'/'))!==false)
$id[$pos+1]=strtolower($id[$pos+1]);
else
$id[0]=strtolower($id[0]);
return $id;
}
public function getUniqueControllerID()
{
$id=$this->controller;
if(($pos=strrpos($id,'/'))!==false)
$id[$pos+1]=strtolower($id[$pos+1]);
else
$id[0]=strtolower($id[0]);
return $id;
}
public function getControllerFile()
{
$module=$this->getModule();
$id=$this->getControllerID();
if(($pos=strrpos($id,'/'))!==false)
$id[$pos+1]=strtoupper($id[$pos+1]);
else
$id[0]=strtoupper($id[0]);
return $module->getControllerPath().'/'.$id.'Controller.php';
}
public function getViewFile($action)
{
$module=$this->getModule();
return $module->getViewPath().'/'.$this->getControllerID().'/'.$action.'.php';
}
}