BakeShell.php 8.0 KB

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