BakeShell.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @since CakePHP(tm) v 1.2.0.5012
  19. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  20. */
  21. App::uses('AppShell', 'Console/Command');
  22. App::uses('Model', 'Model');
  23. /**
  24. * Command-line code generation utility to automate programmer chores.
  25. *
  26. * Bake is CakePHP's code generation script, which can help you kickstart
  27. * application development by writing fully functional skeleton controllers,
  28. * models, and views. Going further, Bake can also write Unit Tests for you.
  29. *
  30. * @package Cake.Console.Command
  31. * @link http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
  32. */
  33. class BakeShell extends AppShell {
  34. /**
  35. * Contains tasks to load and instantiate
  36. *
  37. * @var array
  38. */
  39. public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
  40. /**
  41. * The connection being used.
  42. *
  43. * @var string
  44. */
  45. public $connection = 'default';
  46. /**
  47. * Assign $this->connection to the active task if a connection param is set.
  48. *
  49. * @return void
  50. */
  51. public function startup() {
  52. parent::startup();
  53. Configure::write('debug', 2);
  54. Configure::write('Cache.disable', 1);
  55. $task = Inflector::classify($this->command);
  56. if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
  57. if (isset($this->params['connection'])) {
  58. $this->{$task}->connection = $this->params['connection'];
  59. }
  60. }
  61. if (isset($this->params['connection'])) {
  62. $this->connection = $this->params['connection'];
  63. }
  64. }
  65. /**
  66. * Override main() to handle action
  67. *
  68. * @return mixed
  69. */
  70. public function main() {
  71. if (!is_dir($this->DbConfig->path)) {
  72. $path = $this->Project->execute();
  73. if (!empty($path)) {
  74. $this->DbConfig->path = $path . 'Config' . DS;
  75. } else {
  76. return false;
  77. }
  78. }
  79. if (!config('database')) {
  80. $this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.'));
  81. $this->args = null;
  82. return $this->DbConfig->execute();
  83. }
  84. $this->out(__d('cake_console', 'Interactive Bake Shell'));
  85. $this->hr();
  86. $this->out(__d('cake_console', '[D]atabase Configuration'));
  87. $this->out(__d('cake_console', '[M]odel'));
  88. $this->out(__d('cake_console', '[V]iew'));
  89. $this->out(__d('cake_console', '[C]ontroller'));
  90. $this->out(__d('cake_console', '[P]roject'));
  91. $this->out(__d('cake_console', '[F]ixture'));
  92. $this->out(__d('cake_console', '[T]est case'));
  93. $this->out(__d('cake_console', '[Q]uit'));
  94. $classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
  95. switch ($classToBake) {
  96. case 'D':
  97. $this->DbConfig->execute();
  98. break;
  99. case 'M':
  100. $this->Model->execute();
  101. break;
  102. case 'V':
  103. $this->View->execute();
  104. break;
  105. case 'C':
  106. $this->Controller->execute();
  107. break;
  108. case 'P':
  109. $this->Project->execute();
  110. break;
  111. case 'F':
  112. $this->Fixture->execute();
  113. break;
  114. case 'T':
  115. $this->Test->execute();
  116. break;
  117. case 'Q':
  118. return $this->_stop();
  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. ))->addOption('theme', array(
  227. 'short' => 't',
  228. 'help' => __d('cake_console', 'Theme to use when baking code.')
  229. ));
  230. }
  231. }