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