BakeShell.php 7.8 KB

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