2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Command-line code generation utility to automate programmer chores.
|
|
|
|
*
|
|
|
|
* Bake is CakePHP's code generation script, which can help you kickstart
|
|
|
|
* application development by writing fully functional skeleton controllers,
|
|
|
|
* models, and views. Going further, Bake can also write Unit Tests for you.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5012
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Bake is a command-line code generation utility for automating programmer chores.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
2010-03-28 15:34:58 +00:00
|
|
|
* @link http://book.cakephp.org/view/1522/Code-Generation-with-Bake
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class BakeShell extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains tasks to load and instantiate
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override loadTasks() to handle paths
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function loadTasks() {
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::loadTasks();
|
|
|
|
$task = Inflector::classify($this->command);
|
2009-06-07 00:03:26 +00:00
|
|
|
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
|
2009-05-04 00:48:13 +00:00
|
|
|
if (isset($this->params['connection'])) {
|
|
|
|
$this->{$task}->connection = $this->params['connection'];
|
|
|
|
}
|
2009-07-28 20:19:15 +00:00
|
|
|
foreach($this->args as $i => $arg) {
|
2009-07-28 07:09:35 +00:00
|
|
|
if (strpos($arg, '.')) {
|
2009-11-16 00:55:20 +00:00
|
|
|
list($this->params['plugin'], $this->args[$i]) = pluginSplit($arg);
|
2009-07-28 20:19:15 +00:00
|
|
|
break;
|
2009-07-28 07:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-04 00:48:13 +00:00
|
|
|
if (isset($this->params['plugin'])) {
|
|
|
|
$this->{$task}->plugin = $this->params['plugin'];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override main() to handle action
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function main() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_dir($this->DbConfig->path)) {
|
|
|
|
if ($this->Project->execute()) {
|
|
|
|
$this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
|
2009-10-31 02:08:06 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config('database')) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Your database configuration was not found. Take a moment to create one.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->args = null;
|
|
|
|
return $this->DbConfig->execute();
|
|
|
|
}
|
|
|
|
$this->out('Interactive Bake Shell');
|
|
|
|
$this->hr();
|
|
|
|
$this->out('[D]atabase Configuration');
|
|
|
|
$this->out('[M]odel');
|
|
|
|
$this->out('[V]iew');
|
|
|
|
$this->out('[C]ontroller');
|
|
|
|
$this->out('[P]roject');
|
2009-04-29 00:32:15 +00:00
|
|
|
$this->out('[F]ixture');
|
2009-08-07 12:56:49 +00:00
|
|
|
$this->out('[T]est case');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('[Q]uit');
|
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$classToBake = strtoupper($this->in(__('What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($classToBake) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'D':
|
|
|
|
$this->DbConfig->execute();
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
$this->Model->execute();
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
$this->View->execute();
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
$this->Controller->execute();
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
$this->Project->execute();
|
|
|
|
break;
|
2009-04-29 00:32:15 +00:00
|
|
|
case 'F':
|
|
|
|
$this->Fixture->execute();
|
|
|
|
break;
|
2009-08-07 12:56:49 +00:00
|
|
|
case 'T':
|
|
|
|
$this->Test->execute();
|
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'Q':
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
2010-04-15 15:43:39 +00:00
|
|
|
$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.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$this->hr();
|
|
|
|
$this->main();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Quickly bake the MVC
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function all() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out('Bake All');
|
|
|
|
$this->hr();
|
|
|
|
|
2009-07-16 12:52:33 +00:00
|
|
|
if (!isset($this->params['connection']) && empty($this->connection)) {
|
|
|
|
$this->connection = $this->DbConfig->getConfig();
|
|
|
|
}
|
|
|
|
|
2009-07-16 13:12:32 +00:00
|
|
|
if (empty($this->args)) {
|
2009-07-17 03:55:41 +00:00
|
|
|
$this->Model->interactive = true;
|
2009-07-16 13:12:32 +00:00
|
|
|
$name = $this->Model->getName($this->connection);
|
|
|
|
}
|
2009-07-17 03:55:41 +00:00
|
|
|
|
2009-07-16 12:52:33 +00:00
|
|
|
foreach (array('Model', 'Controller', 'View') as $task) {
|
|
|
|
$this->{$task}->connection = $this->connection;
|
|
|
|
$this->{$task}->interactive = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->args[0])) {
|
|
|
|
$name = $this->args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$modelExists = false;
|
|
|
|
$model = $this->_modelName($name);
|
|
|
|
if (App::import('Model', $model)) {
|
|
|
|
$object = new $model();
|
|
|
|
$modelExists = true;
|
|
|
|
} else {
|
2009-07-30 22:01:22 +00:00
|
|
|
App::import('Model', 'Model', false);
|
2009-07-16 12:52:33 +00:00
|
|
|
$object = new Model(array('name' => $name, 'ds' => $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$modelBaked = $this->Model->bake($object, false);
|
|
|
|
|
|
|
|
if ($modelBaked && $modelExists === false) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('%s Model was baked.'), $model));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
2009-07-16 13:12:32 +00:00
|
|
|
$this->Model->bakeFixture($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Model->bakeTest($model);
|
|
|
|
}
|
|
|
|
$modelExists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($modelExists === true) {
|
|
|
|
$controller = $this->_controllerName($name);
|
|
|
|
if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('%s Controller was baked.'), $name));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
|
|
|
$this->Controller->bakeTest($controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (App::import('Controller', $controller)) {
|
|
|
|
$this->View->args = array($controller);
|
|
|
|
$this->View->execute();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('%s Views were baked.'), $name));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Bake All complete'));
|
2008-06-05 02:23:18 +00:00
|
|
|
array_shift($this->args);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(__('Bake All could not continue without a valid model'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function help() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('CakePHP Bake:');
|
|
|
|
$this->hr();
|
|
|
|
$this->out('The Bake script generates controllers, views and models for your application.');
|
|
|
|
$this->out('If run with no command line arguments, Bake guides the user through the class');
|
|
|
|
$this->out('creation process. You can customize the generation process by telling Bake');
|
|
|
|
$this->out('where different parts of your application are using command line arguments.');
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Usage: cake bake <command> <arg1> <arg2>...");
|
|
|
|
$this->hr();
|
|
|
|
$this->out('Params:');
|
|
|
|
$this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
|
|
|
|
$this->out('Commands:');
|
|
|
|
$this->out("\n\tbake help\n\t\tshows this help message.");
|
|
|
|
$this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
|
|
|
|
$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");
|
|
|
|
$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.");
|
|
|
|
$this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
|
|
|
|
$this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
|
|
|
|
$this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
|
|
|
|
$this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
|
2009-11-08 18:46:45 +00:00
|
|
|
$this->out("\n\tbake fixture\n\t\tbakes fixtures. run 'bake fixture help' for more info.");
|
|
|
|
$this->out("\n\tbake test\n\t\tbakes unit tests. run 'bake test help' for more info.");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|