PluginTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\Utility\File;
  20. use Cake\Utility\Folder;
  21. use Cake\Utility\Inflector;
  22. /**
  23. * The Plugin Task handles creating an empty plugin, ready to be used
  24. *
  25. */
  26. class PluginTask extends Shell {
  27. /**
  28. * path to plugins directory
  29. *
  30. * @var array
  31. */
  32. public $path = null;
  33. /**
  34. * Path to the bootstrap file. Changed in tests.
  35. *
  36. * @var string
  37. */
  38. public $bootstrap = null;
  39. /**
  40. * Tasks this task uses.
  41. *
  42. * @var array
  43. */
  44. public $tasks = ['Template'];
  45. /**
  46. * initialize
  47. *
  48. * @return void
  49. */
  50. public function initialize() {
  51. $this->path = current(App::path('Plugin'));
  52. $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
  53. }
  54. /**
  55. * Execution method always used for tasks
  56. *
  57. * @return void
  58. */
  59. public function execute() {
  60. if (isset($this->args[0])) {
  61. $plugin = Inflector::camelize($this->args[0]);
  62. $pluginPath = $this->_pluginPath($plugin);
  63. if (is_dir($pluginPath)) {
  64. $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
  65. $this->out(__d('cake_console', 'Path: %s', $pluginPath));
  66. return false;
  67. }
  68. $this->_interactive($plugin);
  69. } else {
  70. return $this->_interactive();
  71. }
  72. }
  73. /**
  74. * Interactive interface
  75. *
  76. * @param string $plugin
  77. * @return void
  78. */
  79. protected function _interactive($plugin = null) {
  80. while ($plugin === null) {
  81. $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
  82. }
  83. if (!$this->bake($plugin)) {
  84. $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
  85. }
  86. }
  87. /**
  88. * Bake the plugin, create directories and files
  89. *
  90. * @param string $plugin Name of the plugin in CamelCased format
  91. * @return boolean
  92. */
  93. public function bake($plugin) {
  94. $pathOptions = App::path('Plugin');
  95. if (count($pathOptions) > 1) {
  96. $this->findPath($pathOptions);
  97. }
  98. $this->hr();
  99. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  100. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
  101. $this->hr();
  102. $looksGood = $this->in(__d('cake_console', 'Look okay?'), ['y', 'n', 'q'], 'y');
  103. if (strtolower($looksGood) === 'y') {
  104. $Folder = new Folder($this->path . $plugin);
  105. $directories = [
  106. 'Config' . DS . 'Schema',
  107. 'Model' . DS . 'Behavior',
  108. 'Model' . DS . 'Table',
  109. 'Model' . DS . 'Entity',
  110. 'Console' . DS . 'Command' . DS . 'Task',
  111. 'Controller' . DS . 'Component',
  112. 'Lib',
  113. 'View' . DS . 'Helper',
  114. 'Template',
  115. 'Test' . DS . 'TestCase' . DS . 'Controller' . DS . 'Component',
  116. 'Test' . DS . 'TestCase' . DS . 'View' . DS . 'Helper',
  117. 'Test' . DS . 'TestCase' . DS . 'Model' . DS . 'Behavior',
  118. 'Test' . DS . 'Fixture',
  119. 'webroot'
  120. ];
  121. foreach ($directories as $directory) {
  122. $dirPath = $this->path . $plugin . DS . $directory;
  123. $Folder->create($dirPath);
  124. new File($dirPath . DS . 'empty', true);
  125. }
  126. foreach ($Folder->messages() as $message) {
  127. $this->out($message, 1, Shell::VERBOSE);
  128. }
  129. $errors = $Folder->errors();
  130. if (!empty($errors)) {
  131. foreach ($errors as $message) {
  132. $this->error($message);
  133. }
  134. return false;
  135. }
  136. $controllerFileName = $plugin . 'AppController.php';
  137. $out = "<?php\n\n";
  138. $out .= "namespace {$plugin}\\Controller;\n\n";
  139. $out .= "use App\\Controller\\AppController;\n\n";
  140. $out .= "class {$plugin}AppController extends AppController {\n\n";
  141. $out .= "}\n";
  142. $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
  143. $this->_modifyBootstrap($plugin);
  144. $this->_generatePhpunitXml($plugin, $this->path);
  145. $this->_generateTestBootstrap($plugin, $this->path);
  146. $this->hr();
  147. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
  148. }
  149. return true;
  150. }
  151. /**
  152. * Update the app's bootstrap.php file.
  153. *
  154. * @param string $plugin Name of plugin
  155. * @return void
  156. */
  157. protected function _modifyBootstrap($plugin) {
  158. $bootstrap = new File($this->bootstrap, false);
  159. $contents = $bootstrap->read();
  160. if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
  161. $bootstrap->append("\nPlugin::load('$plugin', ['bootstrap' => false, 'routes' => false]);\n");
  162. $this->out('');
  163. $this->out(sprintf('%s modified', $this->bootstrap));
  164. }
  165. }
  166. /**
  167. * Generate a phpunit.xml stub for the plugin.
  168. *
  169. * @param string $plugin Name of plugin
  170. * @param string $path The path to save the phpunit.xml file to.
  171. * @return void
  172. */
  173. protected function _generatePhpunitXml($plugin, $path) {
  174. $this->Template->set([
  175. 'plugin' => $plugin,
  176. 'path' => $path
  177. ]);
  178. $this->out( __d('cake_console', 'Generating phpunit.xml file...'));
  179. $out = $this->Template->generate('test', 'phpunit.xml');
  180. $file = $path . $plugin . DS . 'phpunit.xml';
  181. $this->createFile($file, $out);
  182. }
  183. /**
  184. * Generate a Test/bootstrap.php stub for the plugin.
  185. *
  186. * @param string $plugin Name of plugin
  187. * @param string $path The path to save the phpunit.xml file to.
  188. * @return void
  189. */
  190. protected function _generateTestBootstrap($plugin, $path) {
  191. $this->Template->set([
  192. 'plugin' => $plugin,
  193. 'path' => $path,
  194. 'root' => ROOT
  195. ]);
  196. $this->out( __d('cake_console', 'Generating Test/bootstrap.php file...'));
  197. $out = $this->Template->generate('test', 'bootstrap');
  198. $file = $path . $plugin . DS . 'Test' . DS . 'bootstrap.php';
  199. $this->createFile($file, $out);
  200. }
  201. /**
  202. * find and change $this->path to the user selection
  203. *
  204. * @param array $pathOptions
  205. * @return void
  206. */
  207. public function findPath($pathOptions) {
  208. $valid = false;
  209. foreach ($pathOptions as $i => $path) {
  210. if (!is_dir($path)) {
  211. unset($pathOptions[$i]);
  212. }
  213. }
  214. $pathOptions = array_values($pathOptions);
  215. $max = count($pathOptions);
  216. while (!$valid) {
  217. foreach ($pathOptions as $i => $option) {
  218. $this->out($i + 1 . '. ' . $option);
  219. }
  220. $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
  221. $choice = $this->in($prompt, null, 1);
  222. if (intval($choice) > 0 && intval($choice) <= $max) {
  223. $valid = true;
  224. }
  225. }
  226. $this->path = $pathOptions[$choice - 1];
  227. }
  228. /**
  229. * Gets the option parser instance and configures it.
  230. *
  231. * @return \Cake\Console\ConsoleOptionParser
  232. */
  233. public function getOptionParser() {
  234. $parser = parent::getOptionParser();
  235. $parser->description(__d('cake_console',
  236. 'Create the directory structure, AppController class and testing setup for a new plugin. ' .
  237. 'Can create plugins in any of your bootstrapped plugin paths.'
  238. ))->addArgument('name', [
  239. 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
  240. ]);
  241. return $parser;
  242. }
  243. }