PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/Command/BakeShell.php

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