ControllerTask.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console\Command\Task;
  16. use Cake\Console\Shell;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\ORM\TableRegistry;
  20. /**
  21. * Task class for creating and updating controller files.
  22. *
  23. */
  24. class ControllerTask extends BakeTask {
  25. /**
  26. * Tasks to be loaded by this Task
  27. *
  28. * @var array
  29. */
  30. public $tasks = ['Model', 'Test', 'Template'];
  31. /**
  32. * Path fragment for generated code.
  33. *
  34. * @var string
  35. */
  36. public $pathFragment = 'Controller/';
  37. /**
  38. * Execution method always used for tasks
  39. *
  40. * @return void
  41. */
  42. public function main($name = null) {
  43. parent::main();
  44. $name = $this->_getName($name);
  45. if (empty($name)) {
  46. $this->out(__d('cake_console', 'Possible controllers based on your current database:'));
  47. foreach ($this->listAll() as $table) {
  48. $this->out('- ' . $this->_controllerName($table));
  49. }
  50. return true;
  51. }
  52. $controller = $this->_controllerName($name);
  53. $this->bake($controller);
  54. }
  55. /**
  56. * Bake All the controllers at once. Will only bake controllers for models that exist.
  57. *
  58. * @return void
  59. */
  60. public function all() {
  61. $controllersCreated = 0;
  62. foreach ($this->listAll() as $table) {
  63. $controller = $this->_controllerName($table);
  64. $this->bake($controller);
  65. $this->bakeTest($controller);
  66. $controllersCreated++;
  67. }
  68. }
  69. /**
  70. * Bake scaffold actions
  71. *
  72. * @param string $controllerName Controller name
  73. * @return string Baked actions
  74. */
  75. public function bakeActions($controllerName) {
  76. if (!empty($this->params['no-actions'])) {
  77. return '';
  78. }
  79. $currentModelName = $controllerName;
  80. $plugin = $this->plugin;
  81. if ($plugin) {
  82. $plugin .= '.';
  83. }
  84. $modelObj = TableRegistry::get($currentModelName);
  85. $pluralName = $this->_pluralName($currentModelName);
  86. $singularName = $this->_singularName($currentModelName);
  87. $singularHumanName = $this->_singularHumanName($controllerName);
  88. $pluralHumanName = $this->_pluralName($controllerName);
  89. $this->Template->set(compact(
  90. 'plugin', 'admin', 'pluralName', 'singularName',
  91. 'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName'
  92. ));
  93. $actions = $this->Template->generate('actions', 'controller_actions');
  94. return $actions;
  95. }
  96. /**
  97. * Assembles and writes a Controller file
  98. *
  99. * @param string $controllerName Controller name already pluralized and correctly cased.
  100. * @return string Baked controller
  101. */
  102. public function bake($controllerName) {
  103. $this->out("\n" . __d('cake_console', 'Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
  104. $actions = $this->bakeActions($controllerName);
  105. $helpers = $this->getHelpers();
  106. $components = $this->getComponents();
  107. $prefix = '';
  108. if (isset($this->params['prefix'])) {
  109. $prefix = '\\' . $this->params['prefix'];
  110. }
  111. $namespace = Configure::read('App.namespace');
  112. if ($this->plugin) {
  113. $namespace = $this->plugin;
  114. }
  115. $data = compact(
  116. 'prefix',
  117. 'actions',
  118. 'helpers',
  119. 'components',
  120. 'namespace'
  121. );
  122. $data['name'] = $controllerName;
  123. $out = $this->bakeController($controllerName, $data);
  124. $this->bakeTest($controllerName);
  125. return $out;
  126. }
  127. /**
  128. * Generate the controller code
  129. *
  130. * @param string $controllerName The name of the controller.
  131. * @param array $data The data to turn into code.
  132. * @return string The generated controller file.
  133. */
  134. public function bakeController($controllerName, array $data) {
  135. $data += [
  136. 'name' => null,
  137. 'namespace' => null,
  138. 'prefix' => null,
  139. 'actions' => null,
  140. 'helpers' => null,
  141. 'components' => null,
  142. 'plugin' => null,
  143. 'pluginPath' => null,
  144. ];
  145. $this->Template->set($data);
  146. $contents = $this->Template->generate('classes', 'controller');
  147. $path = $this->getPath();
  148. $filename = $path . $controllerName . 'Controller.php';
  149. $this->createFile($filename, $contents);
  150. return $contents;
  151. }
  152. /**
  153. * Gets the path for output. Checks the plugin property
  154. * and returns the correct path.
  155. *
  156. * @return string Path to output.
  157. */
  158. public function getPath() {
  159. $path = parent::getPath();
  160. if (!empty($this->params['prefix'])) {
  161. $path .= $this->_camelize($this->params['prefix']) . DS;
  162. }
  163. return $path;
  164. }
  165. /**
  166. * Assembles and writes a unit test file
  167. *
  168. * @param string $className Controller class name
  169. * @return string Baked test
  170. */
  171. public function bakeTest($className) {
  172. if (!empty($this->params['no-test'])) {
  173. return;
  174. }
  175. $this->Test->plugin = $this->plugin;
  176. $this->Test->connection = $this->connection;
  177. if (!empty($this->params['prefix'])) {
  178. $className = $this->params['prefix'] . '\\' . $className;
  179. }
  180. return $this->Test->bake('Controller', $className);
  181. }
  182. /**
  183. * Get the list of components for the controller.
  184. *
  185. * @return array
  186. */
  187. public function getComponents() {
  188. $components = [];
  189. if (!empty($this->params['components'])) {
  190. $components = explode(',', $this->params['components']);
  191. $components = array_values(array_filter(array_map('trim', $components)));
  192. }
  193. return $components;
  194. }
  195. /**
  196. * Get the list of helpers for the controller.
  197. *
  198. * @return array
  199. */
  200. public function getHelpers() {
  201. $helpers = [];
  202. if (!empty($this->params['helpers'])) {
  203. $helpers = explode(',', $this->params['helpers']);
  204. $helpers = array_values(array_filter(array_map('trim', $helpers)));
  205. }
  206. return $helpers;
  207. }
  208. /**
  209. * Outputs and gets the list of possible controllers from database
  210. *
  211. * @param string $useDbConfig Database configuration name
  212. * @return array Set of controllers
  213. */
  214. public function listAll() {
  215. $this->Model->connection = $this->connection;
  216. return $this->Model->listAll();
  217. }
  218. /**
  219. * Gets the option parser instance and configures it.
  220. *
  221. * @return \Cake\Console\ConsoleOptionParser
  222. */
  223. public function getOptionParser() {
  224. $parser = parent::getOptionParser();
  225. $parser->description(
  226. __d('cake_console', 'Bake a controller skeleton.')
  227. )->addArgument('name', [
  228. 'help' => __d('cake_console', 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
  229. ])->addOption('components', [
  230. 'help' => __d('cake_console', 'The comma separated list of components to use.')
  231. ])->addOption('helpers', [
  232. 'help' => __d('cake_console', 'The comma separated list of helpers to use.')
  233. ])->addOption('prefix', [
  234. 'help' => __d('cake_console', 'The namespace/routing prefix to use.')
  235. ])->addOption('no-test', [
  236. 'boolean' => true,
  237. 'help' => __d('cake_console', 'Do not generate a test skeleton.')
  238. ])->addOption('no-actions', [
  239. 'boolean' => true,
  240. 'help' => __d('cake_console', 'Do not generate basic CRUD action methods.')
  241. ])->addSubcommand('all', [
  242. 'help' => __d('cake_console', 'Bake all controllers with CRUD methods.')
  243. ]);
  244. return $parser;
  245. }
  246. }