BakeShell.php 8.0 KB

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