PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

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