BakeShell.php 7.5 KB

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