pictcode / lib / Cake / Console / Command / BakeShell.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (8.037 KB)
| 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 |  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 | 
| 10 |  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 | 
| 11 |  *
 | 
| 12 |  * Licensed under The MIT License
 | 
| 13 |  * For full copyright and license information, please see the LICENSE.txt
 | 
| 14 |  * Redistributions of files must retain the above copyright notice.
 | 
| 15 |  *
 | 
| 16 |  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 | 
| 17 |  * @link          http://cakephp.org CakePHP(tm) Project
 | 
| 18 |  * @since         CakePHP(tm) v 1.2.0.5012
 | 
| 19 |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 | 
| 20 |  */
 | 
| 21 |  | 
| 22 | App::uses('AppShell', 'Console/Command'); | 
| 23 | App::uses('Model', 'Model'); | 
| 24 |  | 
| 25 | /**
 | 
| 26 |  * Command-line code generation utility to automate programmer chores.
 | 
| 27 |  *
 | 
| 28 |  * Bake is CakePHP's code generation script, which can help you kickstart
 | 
| 29 |  * application development by writing fully functional skeleton controllers,
 | 
| 30 |  * models, and views. Going further, Bake can also write Unit Tests for you.
 | 
| 31 |  *
 | 
| 32 |  * @package       Cake.Console.Command
 | 
| 33 |  * @link          http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
 | 
| 34 |  */
 | 
| 35 | class BakeShell extends AppShell { | 
| 36 |  | 
| 37 | /**
 | 
| 38 |  * Contains tasks to load and instantiate
 | 
| 39 |  *
 | 
| 40 |  * @var array
 | 
| 41 |  */
 | 
| 42 | public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'); | 
| 43 |  | 
| 44 | /**
 | 
| 45 |  * The connection being used.
 | 
| 46 |  *
 | 
| 47 |  * @var string
 | 
| 48 |  */
 | 
| 49 | public $connection = 'default'; | 
| 50 |  | 
| 51 | /**
 | 
| 52 |  * Assign $this->connection to the active task if a connection param is set.
 | 
| 53 |  *
 | 
| 54 |  * @return void
 | 
| 55 |  */
 | 
| 56 | public function startup() { | 
| 57 |                 parent::startup();
 | 
| 58 | Configure::write('debug', 2); | 
| 59 | Configure::write('Cache.disable', 1); | 
| 60 |  | 
| 61 | $task = Inflector::classify($this->command); | 
| 62 | if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) { | 
| 63 | if (isset($this->params['connection'])) { | 
| 64 | $this->{$task}->connection = $this->params['connection']; | 
| 65 | } | 
| 66 | } | 
| 67 | if (isset($this->params['connection'])) { | 
| 68 | $this->connection = $this->params['connection']; | 
| 69 | } | 
| 70 | } | 
| 71 |  | 
| 72 | /**
 | 
| 73 |  * Override main() to handle action
 | 
| 74 |  *
 | 
| 75 |  * @return mixed
 | 
| 76 |  */
 | 
| 77 | public function main() { | 
| 78 | if (!is_dir($this->DbConfig->path)) { | 
| 79 | $path = $this->Project->execute(); | 
| 80 | if (!empty($path)) { | 
| 81 | $this->DbConfig->path = $path . 'Config' . DS; | 
| 82 |                         } else {
 | 
| 83 | return false; | 
| 84 | } | 
| 85 | } | 
| 86 |  | 
| 87 | if (!config('database')) { | 
| 88 | $this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.')); | 
| 89 | $this->args = null; | 
| 90 | return $this->DbConfig->execute(); | 
| 91 | } | 
| 92 | $this->out(__d('cake_console', 'Interactive Bake Shell')); | 
| 93 |                 $this->hr();
 | 
| 94 | $this->out(__d('cake_console', '[D]atabase Configuration')); | 
| 95 | $this->out(__d('cake_console', '[M]odel')); | 
| 96 | $this->out(__d('cake_console', '[V]iew')); | 
| 97 | $this->out(__d('cake_console', '[C]ontroller')); | 
| 98 | $this->out(__d('cake_console', '[P]roject')); | 
| 99 | $this->out(__d('cake_console', '[F]ixture')); | 
| 100 | $this->out(__d('cake_console', '[T]est case')); | 
| 101 | $this->out(__d('cake_console', '[Q]uit')); | 
| 102 |  | 
| 103 | $classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q'))); | 
| 104 | switch ($classToBake) { | 
| 105 | case 'D': | 
| 106 | $this->DbConfig->execute(); | 
| 107 |                                 break;
 | 
| 108 | case 'M': | 
| 109 | $this->Model->execute(); | 
| 110 |                                 break;
 | 
| 111 | case 'V': | 
| 112 | $this->View->execute(); | 
| 113 |                                 break;
 | 
| 114 | case 'C': | 
| 115 | $this->Controller->execute(); | 
| 116 |                                 break;
 | 
| 117 | case 'P': | 
| 118 | $this->Project->execute(); | 
| 119 |                                 break;
 | 
| 120 | case 'F': | 
| 121 | $this->Fixture->execute(); | 
| 122 |                                 break;
 | 
| 123 | case 'T': | 
| 124 | $this->Test->execute(); | 
| 125 |                                 break;
 | 
| 126 | case 'Q': | 
| 127 | return $this->_stop(); | 
| 128 |                         default:
 | 
| 129 | $this->out(__d('cake_console', 'You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.')); | 
| 130 | } | 
| 131 |                 $this->hr();
 | 
| 132 | $this->main(); | 
| 133 | } | 
| 134 |  | 
| 135 | /**
 | 
| 136 |  * Quickly bake the MVC
 | 
| 137 |  *
 | 
| 138 |  * @return void
 | 
| 139 |  */
 | 
| 140 | public function all() { | 
| 141 | $this->out('Bake All'); | 
| 142 |                 $this->hr();
 | 
| 143 |  | 
| 144 | if (!isset($this->params['connection']) && empty($this->connection)) { | 
| 145 | $this->connection = $this->DbConfig->getConfig(); | 
| 146 | } | 
| 147 |  | 
| 148 | if (empty($this->args)) { | 
| 149 | $this->Model->interactive = true; | 
| 150 | $name = $this->Model->getName($this->connection); | 
| 151 | } | 
| 152 |  | 
| 153 | foreach (array('Model', 'Controller', 'View') as $task) { | 
| 154 | $this->{$task}->connection = $this->connection; | 
| 155 | $this->{$task}->interactive = false; | 
| 156 | } | 
| 157 |  | 
| 158 | if (!empty($this->args[0])) { | 
| 159 | $name = $this->args[0]; | 
| 160 | } | 
| 161 |  | 
| 162 | $modelExists = false; | 
| 163 | $model = $this->_modelName($name); | 
| 164 |  | 
| 165 | App::uses('AppModel', 'Model'); | 
| 166 | App::uses($model, 'Model'); | 
| 167 | if (class_exists($model)) { | 
| 168 | $object = new $model(); | 
| 169 | $modelExists = true; | 
| 170 |                 } else {
 | 
| 171 | $object = new Model(array('name' => $name, 'ds' => $this->connection)); | 
| 172 | } | 
| 173 |  | 
| 174 | $modelBaked = $this->Model->bake($object, false); | 
| 175 |  | 
| 176 | if ($modelBaked && $modelExists === false) { | 
| 177 | if ($this->_checkUnitTest()) { | 
| 178 | $this->Model->bakeFixture($model); | 
| 179 | $this->Model->bakeTest($model); | 
| 180 | } | 
| 181 | $modelExists = true; | 
| 182 | } | 
| 183 |  | 
| 184 | if ($modelExists === true) { | 
| 185 | $controller = $this->_controllerName($name); | 
| 186 | if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) { | 
| 187 | if ($this->_checkUnitTest()) { | 
| 188 | $this->Controller->bakeTest($controller); | 
| 189 | } | 
| 190 | } | 
| 191 | App::uses($controller . 'Controller', 'Controller'); | 
| 192 | if (class_exists($controller . 'Controller')) { | 
| 193 | $this->View->args = array($name); | 
| 194 | $this->View->execute(); | 
| 195 | } | 
| 196 | $this->out('', 1, Shell::QUIET); | 
| 197 | $this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET); | 
| 198 | array_shift($this->args); | 
| 199 |                 } else {
 | 
| 200 | $this->error(__d('cake_console', 'Bake All could not continue without a valid model')); | 
| 201 | } | 
| 202 | return $this->_stop(); | 
| 203 | } | 
| 204 |  | 
| 205 | /**
 | 
| 206 |  * Gets the option parser instance and configures it.
 | 
| 207 |  *
 | 
| 208 |  * @return ConsoleOptionParser
 | 
| 209 |  */
 | 
| 210 | public function getOptionParser() { | 
| 211 | $parser = parent::getOptionParser(); | 
| 212 |  | 
| 213 |                 $parser->description(
 | 
| 214 | __d('cake_console', 'The Bake script generates controllers, views and models for your application.' . | 
| 215 |                         ' If run with no command line arguments, Bake guides the user through the class creation process.' .
 | 
| 216 |                         ' You can customize the generation process by telling Bake where different parts of your application are using command line arguments.')
 | 
| 217 | )->addSubcommand('all', array( | 
| 218 | 'help' => __d('cake_console', 'Bake a complete MVC. optional <name> of a Model') | 
| 219 | ))->addSubcommand('project', array( | 
| 220 | 'help' => __d('cake_console', 'Bake a new app folder in the path supplied or in current directory if no path is specified'), | 
| 221 | 'parser' => $this->Project->getOptionParser() | 
| 222 | ))->addSubcommand('plugin', array( | 
| 223 | 'help' => __d('cake_console', 'Bake a new plugin folder in the path supplied or in current directory if no path is specified.'), | 
| 224 | 'parser' => $this->Plugin->getOptionParser() | 
| 225 | ))->addSubcommand('db_config', array( | 
| 226 | 'help' => __d('cake_console', 'Bake a database.php file in config directory.'), | 
| 227 | 'parser' => $this->DbConfig->getOptionParser() | 
| 228 | ))->addSubcommand('model', array( | 
| 229 | 'help' => __d('cake_console', 'Bake a model.'), | 
| 230 | 'parser' => $this->Model->getOptionParser() | 
| 231 | ))->addSubcommand('view', array( | 
| 232 | 'help' => __d('cake_console', 'Bake views for controllers.'), | 
| 233 | 'parser' => $this->View->getOptionParser() | 
| 234 | ))->addSubcommand('controller', array( | 
| 235 | 'help' => __d('cake_console', 'Bake a controller.'), | 
| 236 | 'parser' => $this->Controller->getOptionParser() | 
| 237 | ))->addSubcommand('fixture', array( | 
| 238 | 'help' => __d('cake_console', 'Bake a fixture.'), | 
| 239 | 'parser' => $this->Fixture->getOptionParser() | 
| 240 | ))->addSubcommand('test', array( | 
| 241 | 'help' => __d('cake_console', 'Bake a unit test.'), | 
| 242 | 'parser' => $this->Test->getOptionParser() | 
| 243 | ))->addOption('connection', array( | 
| 244 | 'help' => __d('cake_console', 'Database connection to use in conjunction with `bake all`.'), | 
| 245 | 'short' => 'c', | 
| 246 | 'default' => 'default' | 
| 247 | ))->addOption('theme', array( | 
| 248 | 'short' => 't', | 
| 249 | 'help' => __d('cake_console', 'Theme to use when baking code.') | 
| 250 | )); | 
| 251 |  | 
| 252 | return $parser; | 
| 253 | } | 
| 254 |  | 
| 255 | } |