BakeShell.php 7.5 KB

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