BakeShell.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * PHP 5
  10. *
  11. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  12. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. *
  14. * Licensed under The MIT License
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  18. * @link http://cakephp.org CakePHP(tm) Project
  19. * @since CakePHP(tm) v 1.2.0.5012
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Model', 'Model');
  23. /**
  24. * Bake is a command-line code generation utility for automating programmer chores.
  25. *
  26. * @package Cake.Console.Command
  27. * @link http://book.cakephp.org/view/1522/Code-Generation-with-Bake
  28. */
  29. class BakeShell extends Shell {
  30. /**
  31. * Contains tasks to load and instantiate
  32. *
  33. * @var array
  34. */
  35. public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
  36. /**
  37. * The connection being used.
  38. *
  39. * @var string
  40. */
  41. public $connection = 'default';
  42. /**
  43. * Assign $this->connection to the active task if a connection param is set.
  44. *
  45. */
  46. public function startup() {
  47. parent::startup();
  48. $task = Inflector::classify($this->command);
  49. if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
  50. if (isset($this->params['connection'])) {
  51. $this->{$task}->connection = $this->params['connection'];
  52. }
  53. }
  54. }
  55. /**
  56. * Override main() to handle action
  57. *
  58. */
  59. public function main() {
  60. if (!is_dir($this->DbConfig->path)) {
  61. $path = $this->Project->execute();
  62. if (!empty($path)) {
  63. $this->DbConfig->path = $path . 'Config' . DS;
  64. } else {
  65. return false;
  66. }
  67. }
  68. if (!config('database')) {
  69. $this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.'));
  70. $this->args = null;
  71. return $this->DbConfig->execute();
  72. }
  73. $this->out(__d('cake_console', 'Interactive Bake Shell'));
  74. $this->hr();
  75. $this->out(__d('cake_console', '[D]atabase Configuration'));
  76. $this->out(__d('cake_console', '[M]odel'));
  77. $this->out(__d('cake_console', '[V]iew'));
  78. $this->out(__d('cake_console', '[C]ontroller'));
  79. $this->out(__d('cake_console', '[P]roject'));
  80. $this->out(__d('cake_console', '[F]ixture'));
  81. $this->out(__d('cake_console', '[T]est case'));
  82. $this->out(__d('cake_console', '[Q]uit'));
  83. $classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
  84. switch ($classToBake) {
  85. case 'D':
  86. $this->DbConfig->execute();
  87. break;
  88. case 'M':
  89. $this->Model->execute();
  90. break;
  91. case 'V':
  92. $this->View->execute();
  93. break;
  94. case 'C':
  95. $this->Controller->execute();
  96. break;
  97. case 'P':
  98. $this->Project->execute();
  99. break;
  100. case 'F':
  101. $this->Fixture->execute();
  102. break;
  103. case 'T':
  104. $this->Test->execute();
  105. break;
  106. case 'Q':
  107. exit(0);
  108. break;
  109. default:
  110. $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.'));
  111. }
  112. $this->hr();
  113. $this->main();
  114. }
  115. /**
  116. * Quickly bake the MVC
  117. *
  118. */
  119. public function all() {
  120. $this->out('Bake All');
  121. $this->hr();
  122. if (!isset($this->params['connection']) && empty($this->connection)) {
  123. $this->connection = $this->DbConfig->getConfig();
  124. }
  125. if (empty($this->args)) {
  126. $this->Model->interactive = true;
  127. $name = $this->Model->getName($this->connection);
  128. }
  129. foreach (array('Model', 'Controller', 'View') as $task) {
  130. $this->{$task}->connection = $this->connection;
  131. $this->{$task}->interactive = false;
  132. }
  133. if (!empty($this->args[0])) {
  134. $name = $this->args[0];
  135. }
  136. $modelExists = false;
  137. $model = $this->_modelName($name);
  138. App::uses('AppModel', 'Model');
  139. App::uses($model, 'Model');
  140. if (class_exists($model)) {
  141. $object = new $model();
  142. $modelExists = true;
  143. } else {
  144. $object = new Model(array('name' => $name, 'ds' => $this->connection));
  145. }
  146. $modelBaked = $this->Model->bake($object, false);
  147. if ($modelBaked && $modelExists === false) {
  148. if ($this->_checkUnitTest()) {
  149. $this->Model->bakeFixture($model);
  150. $this->Model->bakeTest($model);
  151. }
  152. $modelExists = true;
  153. }
  154. if ($modelExists === true) {
  155. $controller = $this->_controllerName($name);
  156. if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
  157. if ($this->_checkUnitTest()) {
  158. $this->Controller->bakeTest($controller);
  159. }
  160. }
  161. App::uses($controller . 'Controller', 'Controller');
  162. if (class_exists($controller . 'Controller')) {
  163. $this->View->args = array($controller);
  164. $this->View->execute();
  165. }
  166. $this->out('', 1, Shell::QUIET);
  167. $this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
  168. array_shift($this->args);
  169. } else {
  170. $this->error(__d('cake_console', 'Bake All could not continue without a valid model'));
  171. }
  172. return $this->_stop();
  173. }
  174. /**
  175. * get the option parser.
  176. *
  177. * @return void
  178. */
  179. public function getOptionParser() {
  180. $parser = parent::getOptionParser();
  181. return $parser->description(__d('cake_console',
  182. 'The Bake script generates controllers, views and models for your application.'
  183. . ' If run with no command line arguments, Bake guides the user through the class creation process.'
  184. . ' You can customize the generation process by telling Bake where different parts of your application are using command line arguments.'
  185. ))->addSubcommand('all', array(
  186. 'help' => __d('cake_console', 'Bake a complete MVC. optional <name> of a Model'),
  187. ))->addSubcommand('project', array(
  188. 'help' => __d('cake_console', 'Bake a new app folder in the path supplied or in current directory if no path is specified'),
  189. 'parser' => $this->Project->getOptionParser()
  190. ))->addSubcommand('plugin', array(
  191. 'help' => __d('cake_console', 'Bake a new plugin folder in the path supplied or in current directory if no path is specified.'),
  192. 'parser' => $this->Plugin->getOptionParser()
  193. ))->addSubcommand('db_config', array(
  194. 'help' => __d('cake_console', 'Bake a database.php file in config directory.'),
  195. 'parser' => $this->DbConfig->getOptionParser()
  196. ))->addSubcommand('model', array(
  197. 'help' => __d('cake_console', 'Bake a model.'),
  198. 'parser' => $this->Model->getOptionParser()
  199. ))->addSubcommand('view', array(
  200. 'help' => __d('cake_console', 'Bake views for controllers.'),
  201. 'parser' => $this->View->getOptionParser()
  202. ))->addSubcommand('controller', array(
  203. 'help' => __d('cake_console', 'Bake a controller.'),
  204. 'parser' => $this->Controller->getOptionParser()
  205. ))->addSubcommand('fixture', array(
  206. 'help' => __d('cake_console', 'Bake a fixture.'),
  207. 'parser' => $this->Fixture->getOptionParser()
  208. ))->addSubcommand('test', array(
  209. 'help' => __d('cake_console', 'Bake a unit test.'),
  210. 'parser' => $this->Test->getOptionParser()
  211. ))->addOption('connection', array(
  212. 'help' => __d('cake_console', 'Database connection to use in conjunction with `bake all`.'),
  213. 'short' => 'c',
  214. 'default' => 'default'
  215. ));
  216. }
  217. }