PageRenderTime 63ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/bake/src/Shell/BakeShell.php

https://gitlab.com/alexandresgv/siteentec
PHP | 317 lines | 239 code | 13 blank | 65 comment | 11 complexity | a91d0ea54425e1d82e26741362d9d25b MD5 | raw file
  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 0.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Bake\Shell;
  16. use Cake\Cache\Cache;
  17. use Cake\Console\Shell;
  18. use Cake\Core\Configure;
  19. use Cake\Core\ConventionsTrait;
  20. use Cake\Core\Plugin;
  21. use Cake\Datasource\ConnectionManager;
  22. use Cake\Utility\Inflector;
  23. /**
  24. * Command-line code generation utility to automate programmer chores.
  25. *
  26. * Bake is CakePHP's code generation script, which can help you kickstart
  27. * application development by writing fully functional skeleton controllers,
  28. * models, and templates. Going further, Bake can also write Unit Tests for you.
  29. *
  30. * @link http://book.cakephp.org/3.0/en/console-and-shells/code-generation-with-bake.html
  31. */
  32. class BakeShell extends Shell
  33. {
  34. use ConventionsTrait;
  35. /**
  36. * The connection being used.
  37. *
  38. * @var string
  39. */
  40. public $connection = 'default';
  41. /**
  42. * Assign $this->connection to the active task if a connection param is set.
  43. *
  44. * @return void
  45. */
  46. public function startup()
  47. {
  48. parent::startup();
  49. Configure::write('debug', true);
  50. Cache::disable();
  51. $task = $this->_camelize($this->command);
  52. if (isset($this->{$task}) && !in_array($task, ['Project'])) {
  53. if (isset($this->params['connection'])) {
  54. $this->{$task}->connection = $this->params['connection'];
  55. }
  56. }
  57. if (isset($this->params['connection'])) {
  58. $this->connection = $this->params['connection'];
  59. }
  60. }
  61. /**
  62. * Override main() to handle action
  63. *
  64. * @return mixed
  65. */
  66. public function main()
  67. {
  68. if ($this->args && $this->args[0] === 'view') {
  69. $this->out('<error>The view command has been renamed.</error>');
  70. $this->out('To create template files, please use the template command:', 2);
  71. $args = $this->args;
  72. array_shift($args);
  73. $args = implode($args, ' ');
  74. $this->out(sprintf(' <info>`bin/cake bake template %s`</info>', $args), 2);
  75. return false;
  76. }
  77. $connections = ConnectionManager::configured();
  78. if (empty($connections)) {
  79. $this->out('Your database configuration was not found.');
  80. $this->out('Add your database connection information to config/app.php.');
  81. return false;
  82. }
  83. $this->out('The following commands can be used to generate skeleton code for your application.', 2);
  84. $this->out('<info>Available bake commands:</info>', 2);
  85. $this->out('- all');
  86. $names = [];
  87. foreach ($this->tasks as $task) {
  88. list(, $name) = pluginSplit($task);
  89. $names[] = Inflector::underscore($name);
  90. }
  91. sort($names);
  92. foreach ($names as $name) {
  93. $this->out('- ' . $name);
  94. }
  95. $this->out('');
  96. $this->out('By using <info>`cake bake [name]`</info> you can invoke a specific bake task.');
  97. return false;
  98. }
  99. /**
  100. * Locate the tasks bake will use.
  101. *
  102. * Scans the following paths for tasks that are subclasses of
  103. * Cake\Shell\Task\BakeTask:
  104. *
  105. * - Cake/Shell/Task/
  106. * - App/Shell/Task/
  107. * - Shell/Task for each loaded plugin
  108. *
  109. * @return void
  110. */
  111. public function loadTasks()
  112. {
  113. $tasks = [];
  114. $tasks = $this->_findTasks($tasks, APP, Configure::read('App.namespace'));
  115. foreach (Plugin::loaded() as $plugin) {
  116. $tasks = $this->_findTasks(
  117. $tasks,
  118. Plugin::classPath($plugin),
  119. $plugin,
  120. $plugin
  121. );
  122. }
  123. $this->tasks = array_values($tasks);
  124. parent::loadTasks();
  125. }
  126. /**
  127. * Append matching tasks in $path to the $tasks array.
  128. *
  129. * @param array $tasks The task list to modify and return.
  130. * @param string $path The base path to look in.
  131. * @param string $namespace The base namespace.
  132. * @param string|null $prefix The prefix to append.
  133. * @return array Updated tasks.
  134. */
  135. protected function _findTasks($tasks, $path, $namespace, $prefix = null)
  136. {
  137. $path .= 'Shell/Task';
  138. if (!is_dir($path)) {
  139. return $tasks;
  140. }
  141. $candidates = $this->_findClassFiles($path, $namespace);
  142. $classes = $this->_findTaskClasses($candidates);
  143. foreach ($classes as $class) {
  144. list(, $name) = namespaceSplit($class);
  145. $name = substr($name, 0, -4);
  146. $fullName = ($prefix ? $prefix . '.' : '') . $name;
  147. $tasks[$name] = $fullName;
  148. }
  149. return $tasks;
  150. }
  151. /**
  152. * Find task classes in a given path.
  153. *
  154. * @param string $path The path to scan.
  155. * @param string $namespace Namespace.
  156. * @return array An array of files that may contain bake tasks.
  157. */
  158. protected function _findClassFiles($path, $namespace)
  159. {
  160. $iterator = new \DirectoryIterator($path);
  161. $candidates = [];
  162. foreach ($iterator as $item) {
  163. if ($item->isDot() || $item->isDir()) {
  164. continue;
  165. }
  166. $name = $item->getBasename('.php');
  167. $candidates[] = $namespace . '\Shell\Task\\' . $name;
  168. }
  169. return $candidates;
  170. }
  171. /**
  172. * Find bake tasks in a given set of files.
  173. *
  174. * @param array $files The array of files.
  175. * @return array An array of matching classes.
  176. */
  177. protected function _findTaskClasses($files)
  178. {
  179. $classes = [];
  180. foreach ($files as $className) {
  181. if (!class_exists($className)) {
  182. continue;
  183. }
  184. $reflect = new \ReflectionClass($className);
  185. if (!$reflect->isInstantiable()) {
  186. continue;
  187. }
  188. if (!$reflect->isSubclassOf('Bake\Shell\Task\BakeTask')) {
  189. continue;
  190. }
  191. $classes[] = $className;
  192. }
  193. return $classes;
  194. }
  195. /**
  196. * Quickly bake the MVC
  197. *
  198. * @param string|null $name Name.
  199. * @return bool
  200. */
  201. public function all($name = null)
  202. {
  203. $this->out('Bake All');
  204. $this->hr();
  205. if (!empty($this->params['connection'])) {
  206. $this->connection = $this->params['connection'];
  207. }
  208. if (empty($name) && !$this->param('everything')) {
  209. $this->Model->connection = $this->connection;
  210. $this->out('Possible model names based on your database:');
  211. foreach ($this->Model->listUnskipped() as $table) {
  212. $this->out('- ' . $table);
  213. }
  214. $this->out('Run <info>`cake bake all [name]`</info> to generate skeleton files.');
  215. return false;
  216. }
  217. $allTables = collection([$name]);
  218. $filteredTables = $allTables;
  219. if ($this->param('everything')) {
  220. $this->Model->connection = $this->connection;
  221. $filteredTables = collection($this->Model->listUnskipped());
  222. }
  223. $filteredTables->each(function ($tableName) {
  224. foreach (['Model', 'Controller', 'Template'] as $task) {
  225. $this->{$task}->connection = $this->connection;
  226. }
  227. $tableName = $this->_camelize($tableName);
  228. $this->Model->main($tableName);
  229. $this->Controller->main($tableName);
  230. $this->Template->main($tableName);
  231. });
  232. $this->out('<success>Bake All complete.</success>', 1, Shell::QUIET);
  233. return true;
  234. }
  235. /**
  236. * Gets the option parser instance and configures it.
  237. *
  238. * @return \Cake\Console\ConsoleOptionParser
  239. */
  240. public function getOptionParser()
  241. {
  242. $parser = parent::getOptionParser();
  243. $bakeThemes = [];
  244. foreach (Plugin::loaded() as $plugin) {
  245. $path = Plugin::classPath($plugin);
  246. if (is_dir($path . 'Template' . DS . 'Bake')) {
  247. $bakeThemes[] = $plugin;
  248. }
  249. }
  250. $parser->description(
  251. 'The Bake script generates controllers, models and template files for your application.' .
  252. ' If run with no command line arguments, Bake guides the user through the class creation process.' .
  253. ' You can customize the generation process by telling Bake where different parts of your application' .
  254. ' are using command line arguments.'
  255. )->addSubcommand('all', [
  256. 'help' => 'Bake a complete MVC skeleton.',
  257. ])->addOption('everything', [
  258. 'help' => 'Bake a complete MVC skeleton, using all the available tables. ' .
  259. 'Usage: "bake all --everything"',
  260. 'default' => false,
  261. 'boolean' => true,
  262. ])->addOption('connection', [
  263. 'help' => 'Database connection to use in conjunction with `bake all`.',
  264. 'short' => 'c',
  265. 'default' => 'default'
  266. ])->addOption('force', [
  267. 'short' => 'f',
  268. 'boolean' => true,
  269. 'help' => 'Force overwriting existing files without prompting.'
  270. ])->addOption('plugin', [
  271. 'short' => 'p',
  272. 'help' => 'Plugin to bake into.'
  273. ])->addOption('prefix', [
  274. 'help' => 'Prefix to bake controllers and templates into.'
  275. ])->addOption('theme', [
  276. 'short' => 't',
  277. 'help' => 'The theme to use when baking code.',
  278. 'choices' => $bakeThemes
  279. ]);
  280. foreach ($this->_taskMap as $task => $config) {
  281. $taskParser = $this->{$task}->getOptionParser();
  282. $parser->addSubcommand(Inflector::underscore($task), [
  283. 'help' => $taskParser->description(),
  284. 'parser' => $taskParser
  285. ]);
  286. }
  287. return $parser;
  288. }
  289. }