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: 25: 26: 27:
28: 29: 30: 31: 32: 33: 34:
35: class BakeShell extends Shell {
36: 37: 38: 39: 40: 41:
42: var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Test');
43: 44: 45: 46: 47:
48: function loadTasks() {
49: parent::loadTasks();
50: $task = Inflector::classify($this->command);
51: if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
52: $path = Inflector::underscore(Inflector::pluralize($this->command));
53: $this->{$task}->path = $this->params['working'] . DS . $path . DS;
54: if (!is_dir($this->{$task}->path)) {
55: $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
56: $this->_stop();
57: }
58: }
59: }
60: 61: 62: 63: 64:
65: function main() {
66: if (!is_dir($this->DbConfig->path)) {
67: if ($this->Project->execute()) {
68: $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
69: }
70: }
71:
72: if (!config('database')) {
73: $this->out(__("Your database configuration was not found. Take a moment to create one.", true));
74: $this->args = null;
75: return $this->DbConfig->execute();
76: }
77: $this->out('Interactive Bake Shell');
78: $this->hr();
79: $this->out('[D]atabase Configuration');
80: $this->out('[M]odel');
81: $this->out('[V]iew');
82: $this->out('[C]ontroller');
83: $this->out('[P]roject');
84: $this->out('[Q]uit');
85:
86: $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q')));
87: switch ($classToBake) {
88: case 'D':
89: $this->DbConfig->execute();
90: break;
91: case 'M':
92: $this->Model->execute();
93: break;
94: case 'V':
95: $this->View->execute();
96: break;
97: case 'C':
98: $this->Controller->execute();
99: break;
100: case 'P':
101: $this->Project->execute();
102: break;
103: case 'Q':
104: exit(0);
105: break;
106: default:
107: $this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, or C.', true));
108: }
109: $this->hr();
110: $this->main();
111: }
112: 113: 114: 115: 116:
117: function all() {
118: $ds = 'default';
119: $this->hr();
120: $this->out('Bake All');
121: $this->hr();
122:
123: if (isset($this->params['connection'])) {
124: $ds = $this->params['connection'];
125: }
126:
127: if (empty($this->args)) {
128: $name = $this->Model->getName($ds);
129: }
130:
131: if (!empty($this->args[0])) {
132: $name = $this->args[0];
133: $this->Model->listAll($ds, false);
134: }
135:
136: $modelExists = false;
137: $model = $this->_modelName($name);
138: if (App::import('Model', $model)) {
139: $object = new $model();
140: $modelExists = true;
141: } else {
142: App::import('Model');
143: $object = new Model(array('name' => $name, 'ds' => $ds));
144: }
145:
146: $modelBaked = $this->Model->bake($object, false);
147:
148: if ($modelBaked && $modelExists === false) {
149: $this->out(sprintf(__('%s Model was baked.', true), $model));
150: if ($this->_checkUnitTest()) {
151: $this->Model->bakeTest($model);
152: }
153: $modelExists = true;
154: }
155:
156: if ($modelExists === true) {
157: $controller = $this->_controllerName($name);
158: if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
159: $this->out(sprintf(__('%s Controller was baked.', true), $name));
160: if ($this->_checkUnitTest()) {
161: $this->Controller->bakeTest($controller);
162: }
163: }
164: if (App::import('Controller', $controller)) {
165: $this->View->args = array($controller);
166: $this->View->execute();
167: }
168: $this->out(__('Bake All complete'));
169: array_shift($this->args);
170: } else {
171: $this->err(__('Bake All could not continue without a valid model', true));
172: }
173:
174: if (empty($this->args)) {
175: $this->all();
176: }
177: $this->_stop();
178: }
179:
180: 181: 182: 183: 184:
185: function help() {
186: $this->out('CakePHP Bake:');
187: $this->hr();
188: $this->out('The Bake script generates controllers, views and models for your application.');
189: $this->out('If run with no command line arguments, Bake guides the user through the class');
190: $this->out('creation process. You can customize the generation process by telling Bake');
191: $this->out('where different parts of your application are using command line arguments.');
192: $this->hr();
193: $this->out("Usage: cake bake <command> <arg1> <arg2>...");
194: $this->hr();
195: $this->out('Params:');
196: $this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
197: $this->out('Commands:');
198: $this->out("\n\tbake help\n\t\tshows this help message.");
199: $this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
200: $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");
201: $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.");
202: $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
203: $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
204: $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
205: $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
206: $this->out("");
207:
208: }
209: }
210: ?>