~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/console/libs/bake.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * PHP versions 4 and 5
 
10
 *
 
11
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
12
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
13
 *
 
14
 * Licensed under The MIT License
 
15
 * Redistributions of files must retain the above copyright notice.
 
16
 *
 
17
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
18
 * @link          http://cakephp.org CakePHP(tm) Project
 
19
 * @package       cake
 
20
 * @subpackage    cake.cake.console.libs
 
21
 * @since         CakePHP(tm) v 1.2.0.5012
 
22
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
23
 */
 
24
 
 
25
/**
 
26
 * Bake is a command-line code generation utility for automating programmer chores.
 
27
 *
 
28
 * @package       cake
 
29
 * @subpackage    cake.cake.console.libs
 
30
 * @link          http://book.cakephp.org/view/1522/Code-Generation-with-Bake
 
31
 */
 
32
class BakeShell extends Shell {
 
33
 
 
34
/**
 
35
 * Contains tasks to load and instantiate
 
36
 *
 
37
 * @var array
 
38
 * @access public
 
39
 */
 
40
        var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
 
41
 
 
42
/**
 
43
 * Override loadTasks() to handle paths
 
44
 *
 
45
 * @access public
 
46
 */
 
47
        function loadTasks() {
 
48
                parent::loadTasks();
 
49
                $task = Inflector::classify($this->command);
 
50
                if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
 
51
                        if (isset($this->params['connection'])) {
 
52
                                $this->{$task}->connection = $this->params['connection'];
 
53
                        }
 
54
                        foreach($this->args as $i => $arg) {
 
55
                                if (strpos($arg, '.')) {
 
56
                                        list($this->params['plugin'], $this->args[$i]) = pluginSplit($arg);
 
57
                                        break;
 
58
                                }
 
59
                        }
 
60
                        if (isset($this->params['plugin'])) {
 
61
                                $this->{$task}->plugin = $this->params['plugin'];
 
62
                        }
 
63
                }
 
64
        }
 
65
 
 
66
/**
 
67
 * Override main() to handle action
 
68
 *
 
69
 * @access public
 
70
 */
 
71
        function main() {
 
72
                if (!is_dir($this->DbConfig->path)) {
 
73
                        if ($this->Project->execute()) {
 
74
                                $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
 
75
                        } else {
 
76
                                return false;
 
77
                        }
 
78
                }
 
79
 
 
80
                if (!config('database')) {
 
81
                        $this->out(__("Your database configuration was not found. Take a moment to create one.", true));
 
82
                        $this->args = null;
 
83
                        return $this->DbConfig->execute();
 
84
                }
 
85
                $this->out('Interactive Bake Shell');
 
86
                $this->hr();
 
87
                $this->out('[D]atabase Configuration');
 
88
                $this->out('[M]odel');
 
89
                $this->out('[V]iew');
 
90
                $this->out('[C]ontroller');
 
91
                $this->out('[P]roject');
 
92
                $this->out('[F]ixture');
 
93
                $this->out('[T]est case');
 
94
                $this->out('[Q]uit');
 
95
 
 
96
                $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
 
97
                switch ($classToBake) {
 
98
                        case 'D':
 
99
                                $this->DbConfig->execute();
 
100
                                break;
 
101
                        case 'M':
 
102
                                $this->Model->execute();
 
103
                                break;
 
104
                        case 'V':
 
105
                                $this->View->execute();
 
106
                                break;
 
107
                        case 'C':
 
108
                                $this->Controller->execute();
 
109
                                break;
 
110
                        case 'P':
 
111
                                $this->Project->execute();
 
112
                                break;
 
113
                        case 'F':
 
114
                                $this->Fixture->execute();
 
115
                                break;
 
116
                        case 'T':
 
117
                                $this->Test->execute();
 
118
                                break;
 
119
                        case 'Q':
 
120
                                exit(0);
 
121
                                break;
 
122
                        default:
 
123
                                $this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.', true));
 
124
                }
 
125
                $this->hr();
 
126
                $this->main();
 
127
        }
 
128
 
 
129
/**
 
130
 * Quickly bake the MVC
 
131
 *
 
132
 * @access public
 
133
 */
 
134
        function all() {
 
135
                $this->hr();
 
136
                $this->out('Bake All');
 
137
                $this->hr();
 
138
 
 
139
                if (!isset($this->params['connection']) && empty($this->connection)) {
 
140
                        $this->connection = $this->DbConfig->getConfig();
 
141
                }
 
142
 
 
143
                if (empty($this->args)) {
 
144
                        $this->Model->interactive = true;
 
145
                        $name = $this->Model->getName($this->connection);
 
146
                }
 
147
 
 
148
                foreach (array('Model', 'Controller', 'View') as $task) {
 
149
                        $this->{$task}->connection = $this->connection;
 
150
                        $this->{$task}->interactive = false;
 
151
                }
 
152
 
 
153
                if (!empty($this->args[0])) {
 
154
                        $name = $this->args[0];
 
155
                }
 
156
 
 
157
                $modelExists = false;
 
158
                $model = $this->_modelName($name);
 
159
                if (App::import('Model', $model)) {
 
160
                        $object = new $model();
 
161
                        $modelExists = true;
 
162
                } else {
 
163
                        App::import('Model', 'Model', false);
 
164
                        $object = new Model(array('name' => $name, 'ds' => $this->connection));
 
165
                }
 
166
 
 
167
                $modelBaked = $this->Model->bake($object, false);
 
168
 
 
169
                if ($modelBaked && $modelExists === false) {
 
170
                        $this->out(sprintf(__('%s Model was baked.', true), $model));
 
171
                        if ($this->_checkUnitTest()) {
 
172
                                $this->Model->bakeFixture($model);
 
173
                                $this->Model->bakeTest($model);
 
174
                        }
 
175
                        $modelExists = true;
 
176
                }
 
177
 
 
178
                if ($modelExists === true) {
 
179
                        $controller = $this->_controllerName($name);
 
180
                        if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
 
181
                                $this->out(sprintf(__('%s Controller was baked.', true), $name));
 
182
                                if ($this->_checkUnitTest()) {
 
183
                                        $this->Controller->bakeTest($controller);
 
184
                                }
 
185
                        }
 
186
                        if (App::import('Controller', $controller)) {
 
187
                                $this->View->args = array($controller);
 
188
                                $this->View->execute();
 
189
                                $this->out(sprintf(__('%s Views were baked.', true), $name));
 
190
                        }
 
191
                        $this->out(__('Bake All complete', true));
 
192
                        array_shift($this->args);
 
193
                } else {
 
194
                        $this->err(__('Bake All could not continue without a valid model', true));
 
195
                }
 
196
                $this->_stop();
 
197
        }
 
198
 
 
199
/**
 
200
 * Displays help contents
 
201
 *
 
202
 * @access public
 
203
 */
 
204
        function help() {
 
205
                $this->out('CakePHP Bake:');
 
206
                $this->hr();
 
207
                $this->out('The Bake script generates controllers, views and models for your application.');
 
208
                $this->out('If run with no command line arguments, Bake guides the user through the class');
 
209
                $this->out('creation process. You can customize the generation process by telling Bake');
 
210
                $this->out('where different parts of your application are using command line arguments.');
 
211
                $this->hr();
 
212
                $this->out("Usage: cake bake <command> <arg1> <arg2>...");
 
213
                $this->hr();
 
214
                $this->out('Params:');
 
215
                $this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
 
216
                $this->out('Commands:');
 
217
                $this->out("\n\tbake help\n\t\tshows this help message.");
 
218
                $this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
 
219
                $this->out("\n\tbake project <path>\n\t\tbakes a new app folder in the path supplied\n\t\tor in current directory if no path is specified");
 
220
                $this->out("\n\tbake plugin <name>\n\t\tbakes a new plugin folder in the path supplied\n\t\tor in current directory if no path is specified.");
 
221
                $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
 
222
                $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
 
223
                $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
 
224
                $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
 
225
                $this->out("\n\tbake fixture\n\t\tbakes fixtures. run 'bake fixture help' for more info.");
 
226
                $this->out("\n\tbake test\n\t\tbakes unit tests. run 'bake test help' for more info.");
 
227
                $this->out();
 
228
 
 
229
        }
 
230
}