ControllerTask.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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\Configure;
  18. use Cake\ORM\TableRegistry;
  19. /**
  20. * Task class for creating and updating controller files.
  21. *
  22. */
  23. class ControllerTask extends BakeTask {
  24. /**
  25. * Tasks to be loaded by this Task
  26. *
  27. * @var array
  28. */
  29. public $tasks = ['Model', 'Test', 'Template'];
  30. /**
  31. * Path fragment for generated code.
  32. *
  33. * @var string
  34. */
  35. public $pathFragment = 'Controller/';
  36. /**
  37. * Execution method always used for tasks
  38. *
  39. * @param string $name The name of the controller to bake.
  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. $controllersCreated++;
  66. }
  67. }
  68. /**
  69. * Bake scaffold actions
  70. *
  71. * @param string $controllerName Controller name
  72. * @return string Baked actions
  73. */
  74. public function bakeActions($controllerName) {
  75. if (!empty($this->params['no-actions'])) {
  76. return '';
  77. }
  78. $currentModelName = $controllerName;
  79. $plugin = $this->plugin;
  80. if ($plugin) {
  81. $plugin .= '.';
  82. }
  83. $modelObj = TableRegistry::get($currentModelName);
  84. $pluralName = $this->_pluralName($currentModelName);
  85. $singularName = $this->_singularName($currentModelName);
  86. $singularHumanName = $this->_singularHumanName($controllerName);
  87. $pluralHumanName = $this->_pluralName($controllerName);
  88. $this->Template->set(compact(
  89. 'plugin', 'admin', 'pluralName', 'singularName',
  90. 'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName'
  91. ));
  92. $actions = $this->Template->generate('actions', 'controller_actions');
  93. return $actions;
  94. }
  95. /**
  96. * Assembles and writes a Controller file
  97. *
  98. * @param string $controllerName Controller name already pluralized and correctly cased.
  99. * @return string Baked controller
  100. */
  101. public function bake($controllerName) {
  102. $this->out("\n" . __d('cake_console', 'Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
  103. $actions = $this->bakeActions($controllerName);
  104. $helpers = $this->getHelpers();
  105. $components = $this->getComponents();
  106. $prefix = '';
  107. if (isset($this->params['prefix'])) {
  108. $prefix = '\\' . $this->params['prefix'];
  109. }
  110. $namespace = Configure::read('App.namespace');
  111. if ($this->plugin) {
  112. $namespace = $this->plugin;
  113. }
  114. $data = compact(
  115. 'prefix',
  116. 'actions',
  117. 'helpers',
  118. 'components',
  119. 'namespace'
  120. );
  121. $data['name'] = $controllerName;
  122. $out = $this->bakeController($controllerName, $data);
  123. $this->bakeTest($controllerName);
  124. return $out;
  125. }
  126. /**
  127. * Generate the controller code
  128. *
  129. * @param string $controllerName The name of the controller.
  130. * @param array $data The data to turn into code.
  131. * @return string The generated controller file.
  132. */
  133. public function bakeController($controllerName, array $data) {
  134. $data += [
  135. 'name' => null,
  136. 'namespace' => null,
  137. 'prefix' => null,
  138. 'actions' => null,
  139. 'helpers' => null,
  140. 'components' => null,
  141. 'plugin' => null,
  142. 'pluginPath' => null,
  143. ];
  144. $this->Template->set($data);
  145. $contents = $this->Template->generate('classes', 'controller');
  146. $path = $this->getPath();
  147. $filename = $path . $controllerName . 'Controller.php';
  148. $this->createFile($filename, $contents);
  149. return $contents;
  150. }
  151. /**
  152. * Gets the path for output. Checks the plugin property
  153. * and returns the correct path.
  154. *
  155. * @return string Path to output.
  156. */
  157. public function getPath() {
  158. $path = parent::getPath();
  159. if (!empty($this->params['prefix'])) {
  160. $path .= $this->_camelize($this->params['prefix']) . DS;
  161. }
  162. return $path;
  163. }
  164. /**
  165. * Assembles and writes a unit test file
  166. *
  167. * @param string $className Controller class name
  168. * @return string Baked test
  169. */
  170. public function bakeTest($className) {
  171. if (!empty($this->params['no-test'])) {
  172. return;
  173. }
  174. $this->Test->plugin = $this->plugin;
  175. $this->Test->connection = $this->connection;
  176. if (!empty($this->params['prefix'])) {
  177. $className = $this->params['prefix'] . '\\' . $className;
  178. }
  179. return $this->Test->bake('Controller', $className);
  180. }
  181. /**
  182. * Get the list of components for the controller.
  183. *
  184. * @return array
  185. */
  186. public function getComponents() {
  187. $components = [];
  188. if (!empty($this->params['components'])) {
  189. $components = explode(',', $this->params['components']);
  190. $components = array_values(array_filter(array_map('trim', $components)));
  191. }
  192. return $components;
  193. }
  194. /**
  195. * Get the list of helpers for the controller.
  196. *
  197. * @return array
  198. */
  199. public function getHelpers() {
  200. $helpers = [];
  201. if (!empty($this->params['helpers'])) {
  202. $helpers = explode(',', $this->params['helpers']);
  203. $helpers = array_values(array_filter(array_map('trim', $helpers)));
  204. }
  205. return $helpers;
  206. }
  207. /**
  208. * Outputs and gets the list of possible controllers from database
  209. *
  210. * @return array Set of controllers
  211. */
  212. public function listAll() {
  213. $this->Model->connection = $this->connection;
  214. return $this->Model->listAll();
  215. }
  216. /**
  217. * Gets the option parser instance and configures it.
  218. *
  219. * @return \Cake\Console\ConsoleOptionParser
  220. */
  221. public function getOptionParser() {
  222. $parser = parent::getOptionParser();
  223. $parser->description(
  224. __d('cake_console', 'Bake a controller skeleton.')
  225. )->addArgument('name', [
  226. 'help' => __d('cake_console', 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
  227. ])->addOption('components', [
  228. 'help' => __d('cake_console', 'The comma separated list of components to use.')
  229. ])->addOption('helpers', [
  230. 'help' => __d('cake_console', 'The comma separated list of helpers to use.')
  231. ])->addOption('prefix', [
  232. 'help' => __d('cake_console', 'The namespace/routing prefix to use.')
  233. ])->addOption('no-test', [
  234. 'boolean' => true,
  235. 'help' => __d('cake_console', 'Do not generate a test skeleton.')
  236. ])->addOption('no-actions', [
  237. 'boolean' => true,
  238. 'help' => __d('cake_console', 'Do not generate basic CRUD action methods.')
  239. ])->addSubcommand('all', [
  240. 'help' => __d('cake_console', 'Bake all controllers with CRUD methods.')
  241. ]);
  242. return $parser;
  243. }
  244. }