bake.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: bake_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Command-line code generation utility to automate programmer chores.
00005  *
00006  * Bake is CakePHP's code generation script, which can help you kickstart
00007  * application development by writing fully functional skeleton controllers,
00008  * models, and views. Going further, Bake can also write Unit Tests for you.
00009  *
00010  * PHP versions 4 and 5
00011  *
00012  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00013  * Copyright 2005-2008, Cake Software Foundation, Inc.
00014  *                              1785 E. Sahara Avenue, Suite 490-204
00015  *                              Las Vegas, Nevada 89104
00016  *
00017  * Licensed under The MIT License
00018  * Redistributions of files must retain the above copyright notice.
00019  *
00020  * @filesource
00021  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00022  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00023  * @package         cake
00024  * @subpackage      cake.cake.console.libs
00025  * @since           CakePHP(tm) v 1.2.0.5012
00026  * @version         $Revision: 580 $
00027  * @modifiedby      $LastChangedBy: gwoo $
00028  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00029  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00030  */
00031 /**
00032  * Bake is a command-line code generation utility for automating programmer chores.
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.console.libs
00036  */
00037 class BakeShell extends Shell {
00038 /**
00039  * Contains tasks to load and instantiate
00040  *
00041  * @var array
00042  * @access public
00043  */
00044     var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Test');
00045 /**
00046  * Override loadTasks() to handle paths
00047  *
00048  * @access public
00049  */
00050     function loadTasks() {
00051         parent::loadTasks();
00052         $task = Inflector::classify($this->command);
00053         if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
00054             $path = Inflector::underscore(Inflector::pluralize($this->command));
00055             $this->{$task}->path = $this->params['working'] . DS . $path . DS;
00056             if (!is_dir($this->{$task}->path)) {
00057                 $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
00058                 $this->_stop();
00059             }
00060         }
00061     }
00062 /**
00063  * Override main() to handle action
00064  *
00065  * @access public
00066  */
00067     function main() {
00068         if (!is_dir($this->DbConfig->path)) {
00069             if ($this->Project->execute()) {
00070                 $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
00071             }
00072         }
00073 
00074         if (!config('database')) {
00075             $this->out(__("Your database configuration was not found. Take a moment to create one.", true));
00076             $this->args = null;
00077             return $this->DbConfig->execute();
00078         }
00079         $this->out('Interactive Bake Shell');
00080         $this->hr();
00081         $this->out('[D]atabase Configuration');
00082         $this->out('[M]odel');
00083         $this->out('[V]iew');
00084         $this->out('[C]ontroller');
00085         $this->out('[P]roject');
00086         $this->out('[Q]uit');
00087 
00088         $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q')));
00089         switch($classToBake) {
00090             case 'D':
00091                 $this->DbConfig->execute();
00092                 break;
00093             case 'M':
00094                 $this->Model->execute();
00095                 break;
00096             case 'V':
00097                 $this->View->execute();
00098                 break;
00099             case 'C':
00100                 $this->Controller->execute();
00101                 break;
00102             case 'P':
00103                 $this->Project->execute();
00104                 break;
00105             case 'Q':
00106                 exit(0);
00107                 break;
00108             default:
00109                 $this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, or C.', true));
00110         }
00111         $this->hr();
00112         $this->main();
00113     }
00114 /**
00115  * Quickly bake the MVC
00116  *
00117  * @access public
00118  */
00119     function all() {
00120         $ds = 'default';
00121         $this->hr();
00122         $this->out('Bake All');
00123         $this->hr();
00124 
00125         if (isset($this->params['connection'])) {
00126             $ds = $this->params['connection'];
00127         }
00128 
00129         if (empty($this->args)) {
00130             $name = $this->Model->getName($ds);
00131         }
00132 
00133         if (!empty($this->args[0])) {
00134             $name = $this->args[0];
00135             $this->Model->listAll($ds, false);
00136         }
00137 
00138         $modelExists = false;
00139         $model = $this->_modelName($name);
00140         if (App::import('Model', $model)) {
00141             $object = new $model();
00142             $modelExists = true;
00143         } else {
00144             App::import('Model');
00145             $object = new Model(array('name' => $name, 'ds' => $ds));
00146         }
00147 
00148         $modelBaked = $this->Model->bake($object, false);
00149 
00150         if ($modelBaked && $modelExists === false) {
00151             $this->out(sprintf(__('%s Model was baked.', true), $model));
00152             if ($this->_checkUnitTest()) {
00153                 $this->Model->bakeTest($model);
00154             }
00155             $modelExists = true;
00156         }
00157 
00158         if ($modelExists === true) {
00159             $controller = $this->_controllerName($name);
00160             if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
00161                 $this->out(sprintf(__('%s Controller was baked.', true), $name));
00162                 if ($this->_checkUnitTest()) {
00163                     $this->Controller->bakeTest($controller);
00164                 }
00165             }
00166             if (App::import('Controller', $controller)) {
00167                 $this->View->args = array($controller);
00168                 $this->View->execute();
00169             }
00170             $this->out(__('Bake All complete'));
00171             array_shift($this->args);
00172         } else {
00173             $this->err(__('Bake All could not continue without a valid model', true));
00174         }
00175 
00176         if (empty($this->args)) {
00177             $this->all();
00178         }
00179         $this->_stop();
00180     }
00181 
00182 /**
00183  * Displays help contents
00184  *
00185  * @access public
00186  */
00187     function help() {
00188         $this->out('CakePHP Bake:');
00189         $this->hr();
00190         $this->out('The Bake script generates controllers, views and models for your application.');
00191         $this->out('If run with no command line arguments, Bake guides the user through the class');
00192         $this->out('creation process. You can customize the generation process by telling Bake');
00193         $this->out('where different parts of your application are using command line arguments.');
00194         $this->hr();
00195         $this->out("Usage: cake bake <command> <arg1> <arg2>...");
00196         $this->hr();
00197         $this->out('Params:');
00198         $this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
00199         $this->out('Commands:');
00200         $this->out("\n\tbake help\n\t\tshows this help message.");
00201         $this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
00202         $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");
00203         $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.");
00204         $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
00205         $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
00206         $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
00207         $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
00208         $this->out("");
00209 
00210     }
00211 }
00212 ?>