bake.php

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

Generated on Sun Nov 22 00:30:51 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7