BakeShell.php 7.4 KB

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