| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Command-line code generation utility to automate programmer chores.
|
|
|
4 |
*
|
|
|
5 |
* Bake is CakePHP's code generation script, which can help you kickstart
|
|
|
6 |
* application development by writing fully functional skeleton controllers,
|
|
|
7 |
* models, and views. Going further, Bake can also write Unit Tests for you.
|
|
|
8 |
*
|
|
|
9 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
10 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
11 |
*
|
|
|
12 |
* Licensed under The MIT License
|
|
|
13 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
17 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
18 |
* @since CakePHP(tm) v 1.2.0.5012
|
|
|
19 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
App::uses('AppShell', 'Console/Command');
|
|
|
23 |
App::uses('Model', 'Model');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Command-line code generation utility to automate programmer chores.
|
|
|
27 |
*
|
|
|
28 |
* Bake is CakePHP's code generation script, which can help you kickstart
|
|
|
29 |
* application development by writing fully functional skeleton controllers,
|
|
|
30 |
* models, and views. Going further, Bake can also write Unit Tests for you.
|
|
|
31 |
*
|
|
|
32 |
* @package Cake.Console.Command
|
|
|
33 |
* @link http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
|
|
|
34 |
*/
|
|
|
35 |
class BakeShell extends AppShell {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Contains tasks to load and instantiate
|
|
|
39 |
*
|
|
|
40 |
* @var array
|
|
|
41 |
*/
|
|
|
42 |
public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* The connection being used.
|
|
|
46 |
*
|
|
|
47 |
* @var string
|
|
|
48 |
*/
|
|
|
49 |
public $connection = 'default';
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Assign $this->connection to the active task if a connection param is set.
|
|
|
53 |
*
|
|
|
54 |
* @return void
|
|
|
55 |
*/
|
|
|
56 |
public function startup() {
|
|
|
57 |
parent::startup();
|
|
|
58 |
Configure::write('debug', 2);
|
|
|
59 |
Configure::write('Cache.disable', 1);
|
|
|
60 |
|
|
|
61 |
$task = Inflector::classify($this->command);
|
|
|
62 |
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
|
|
|
63 |
if (isset($this->params['connection'])) {
|
|
|
64 |
$this->{$task}->connection = $this->params['connection'];
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
if (isset($this->params['connection'])) {
|
|
|
68 |
$this->connection = $this->params['connection'];
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Override main() to handle action
|
|
|
74 |
*
|
|
|
75 |
* @return mixed
|
|
|
76 |
*/
|
|
|
77 |
public function main() {
|
|
|
78 |
if (!is_dir($this->DbConfig->path)) {
|
|
|
79 |
$path = $this->Project->execute();
|
|
|
80 |
if (!empty($path)) {
|
|
|
81 |
$this->DbConfig->path = $path . 'Config' . DS;
|
|
|
82 |
} else {
|
|
|
83 |
return false;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if (!config('database')) {
|
|
|
88 |
$this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.'));
|
|
|
89 |
$this->args = null;
|
|
|
90 |
return $this->DbConfig->execute();
|
|
|
91 |
}
|
|
|
92 |
$this->out(__d('cake_console', 'Interactive Bake Shell'));
|
|
|
93 |
$this->hr();
|
|
|
94 |
$this->out(__d('cake_console', '[D]atabase Configuration'));
|
|
|
95 |
$this->out(__d('cake_console', '[M]odel'));
|
|
|
96 |
$this->out(__d('cake_console', '[V]iew'));
|
|
|
97 |
$this->out(__d('cake_console', '[C]ontroller'));
|
|
|
98 |
$this->out(__d('cake_console', '[P]roject'));
|
|
|
99 |
$this->out(__d('cake_console', '[F]ixture'));
|
|
|
100 |
$this->out(__d('cake_console', '[T]est case'));
|
|
|
101 |
$this->out(__d('cake_console', '[Q]uit'));
|
|
|
102 |
|
|
|
103 |
$classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
|
|
|
104 |
switch ($classToBake) {
|
|
|
105 |
case 'D':
|
|
|
106 |
$this->DbConfig->execute();
|
|
|
107 |
break;
|
|
|
108 |
case 'M':
|
|
|
109 |
$this->Model->execute();
|
|
|
110 |
break;
|
|
|
111 |
case 'V':
|
|
|
112 |
$this->View->execute();
|
|
|
113 |
break;
|
|
|
114 |
case 'C':
|
|
|
115 |
$this->Controller->execute();
|
|
|
116 |
break;
|
|
|
117 |
case 'P':
|
|
|
118 |
$this->Project->execute();
|
|
|
119 |
break;
|
|
|
120 |
case 'F':
|
|
|
121 |
$this->Fixture->execute();
|
|
|
122 |
break;
|
|
|
123 |
case 'T':
|
|
|
124 |
$this->Test->execute();
|
|
|
125 |
break;
|
|
|
126 |
case 'Q':
|
|
|
127 |
return $this->_stop();
|
|
|
128 |
default:
|
|
|
129 |
$this->out(__d('cake_console', 'You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.'));
|
|
|
130 |
}
|
|
|
131 |
$this->hr();
|
|
|
132 |
$this->main();
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Quickly bake the MVC
|
|
|
137 |
*
|
|
|
138 |
* @return void
|
|
|
139 |
*/
|
|
|
140 |
public function all() {
|
|
|
141 |
$this->out('Bake All');
|
|
|
142 |
$this->hr();
|
|
|
143 |
|
|
|
144 |
if (!isset($this->params['connection']) && empty($this->connection)) {
|
|
|
145 |
$this->connection = $this->DbConfig->getConfig();
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
if (empty($this->args)) {
|
|
|
149 |
$this->Model->interactive = true;
|
|
|
150 |
$name = $this->Model->getName($this->connection);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
foreach (array('Model', 'Controller', 'View') as $task) {
|
|
|
154 |
$this->{$task}->connection = $this->connection;
|
|
|
155 |
$this->{$task}->interactive = false;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (!empty($this->args[0])) {
|
|
|
159 |
$name = $this->args[0];
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$modelExists = false;
|
|
|
163 |
$model = $this->_modelName($name);
|
|
|
164 |
|
|
|
165 |
App::uses('AppModel', 'Model');
|
|
|
166 |
App::uses($model, 'Model');
|
|
|
167 |
if (class_exists($model)) {
|
|
|
168 |
$object = new $model();
|
|
|
169 |
$modelExists = true;
|
|
|
170 |
} else {
|
|
|
171 |
$object = new Model(array('name' => $name, 'ds' => $this->connection));
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$modelBaked = $this->Model->bake($object, false);
|
|
|
175 |
|
|
|
176 |
if ($modelBaked && $modelExists === false) {
|
|
|
177 |
if ($this->_checkUnitTest()) {
|
|
|
178 |
$this->Model->bakeFixture($model);
|
|
|
179 |
$this->Model->bakeTest($model);
|
|
|
180 |
}
|
|
|
181 |
$modelExists = true;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
if ($modelExists === true) {
|
|
|
185 |
$controller = $this->_controllerName($name);
|
|
|
186 |
if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
|
|
|
187 |
if ($this->_checkUnitTest()) {
|
|
|
188 |
$this->Controller->bakeTest($controller);
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
App::uses($controller . 'Controller', 'Controller');
|
|
|
192 |
if (class_exists($controller . 'Controller')) {
|
|
|
193 |
$this->View->args = array($name);
|
|
|
194 |
$this->View->execute();
|
|
|
195 |
}
|
|
|
196 |
$this->out('', 1, Shell::QUIET);
|
|
|
197 |
$this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
|
|
|
198 |
array_shift($this->args);
|
|
|
199 |
} else {
|
|
|
200 |
$this->error(__d('cake_console', 'Bake All could not continue without a valid model'));
|
|
|
201 |
}
|
|
|
202 |
return $this->_stop();
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* get the option parser.
|
|
|
207 |
*
|
|
|
208 |
* @return void
|
|
|
209 |
*/
|
|
|
210 |
public function getOptionParser() {
|
|
|
211 |
$parser = parent::getOptionParser();
|
|
|
212 |
return $parser->description(__d('cake_console',
|
|
|
213 |
'The Bake script generates controllers, views and models for your application.' .
|
|
|
214 |
' If run with no command line arguments, Bake guides the user through the class creation process.' .
|
|
|
215 |
' You can customize the generation process by telling Bake where different parts of your application are using command line arguments.'
|
|
|
216 |
))->addSubcommand('all', array(
|
|
|
217 |
'help' => __d('cake_console', 'Bake a complete MVC. optional <name> of a Model'),
|
|
|
218 |
))->addSubcommand('project', array(
|
|
|
219 |
'help' => __d('cake_console', 'Bake a new app folder in the path supplied or in current directory if no path is specified'),
|
|
|
220 |
'parser' => $this->Project->getOptionParser()
|
|
|
221 |
))->addSubcommand('plugin', array(
|
|
|
222 |
'help' => __d('cake_console', 'Bake a new plugin folder in the path supplied or in current directory if no path is specified.'),
|
|
|
223 |
'parser' => $this->Plugin->getOptionParser()
|
|
|
224 |
))->addSubcommand('db_config', array(
|
|
|
225 |
'help' => __d('cake_console', 'Bake a database.php file in config directory.'),
|
|
|
226 |
'parser' => $this->DbConfig->getOptionParser()
|
|
|
227 |
))->addSubcommand('model', array(
|
|
|
228 |
'help' => __d('cake_console', 'Bake a model.'),
|
|
|
229 |
'parser' => $this->Model->getOptionParser()
|
|
|
230 |
))->addSubcommand('view', array(
|
|
|
231 |
'help' => __d('cake_console', 'Bake views for controllers.'),
|
|
|
232 |
'parser' => $this->View->getOptionParser()
|
|
|
233 |
))->addSubcommand('controller', array(
|
|
|
234 |
'help' => __d('cake_console', 'Bake a controller.'),
|
|
|
235 |
'parser' => $this->Controller->getOptionParser()
|
|
|
236 |
))->addSubcommand('fixture', array(
|
|
|
237 |
'help' => __d('cake_console', 'Bake a fixture.'),
|
|
|
238 |
'parser' => $this->Fixture->getOptionParser()
|
|
|
239 |
))->addSubcommand('test', array(
|
|
|
240 |
'help' => __d('cake_console', 'Bake a unit test.'),
|
|
|
241 |
'parser' => $this->Test->getOptionParser()
|
|
|
242 |
))->addOption('connection', array(
|
|
|
243 |
'help' => __d('cake_console', 'Database connection to use in conjunction with `bake all`.'),
|
|
|
244 |
'short' => 'c',
|
|
|
245 |
'default' => 'default'
|
|
|
246 |
))->addOption('theme', array(
|
|
|
247 |
'short' => 't',
|
|
|
248 |
'help' => __d('cake_console', 'Theme to use when baking code.')
|
|
|
249 |
));
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
}
|