PluginTask.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * The Plugin Task handles creating an empty plugin, ready to be used
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 1.2
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. App::uses('AppShell', 'Console/Command');
  18. App::uses('File', 'Utility');
  19. App::uses('Folder', 'Utility');
  20. /**
  21. * The Plugin Task handles creating an empty plugin, ready to be used
  22. *
  23. * @package Cake.Console.Command.Task
  24. */
  25. class PluginTask extends AppShell {
  26. /**
  27. * path to plugins directory
  28. *
  29. * @var array
  30. */
  31. public $path = null;
  32. /**
  33. * Path to the bootstrap file. Changed in tests.
  34. *
  35. * @var string
  36. */
  37. public $bootstrap = null;
  38. /**
  39. * initialize
  40. *
  41. * @return void
  42. */
  43. public function initialize() {
  44. $this->path = current(App::path('plugins'));
  45. $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
  46. }
  47. /**
  48. * Execution method always used for tasks
  49. *
  50. * @return void
  51. */
  52. public function execute() {
  53. if (isset($this->args[0])) {
  54. $plugin = Inflector::camelize($this->args[0]);
  55. $pluginPath = $this->_pluginPath($plugin);
  56. if (is_dir($pluginPath)) {
  57. $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
  58. $this->out(__d('cake_console', 'Path: %s', $pluginPath));
  59. return false;
  60. }
  61. $this->_interactive($plugin);
  62. } else {
  63. return $this->_interactive();
  64. }
  65. }
  66. /**
  67. * Interactive interface
  68. *
  69. * @param string $plugin
  70. * @return void
  71. */
  72. protected function _interactive($plugin = null) {
  73. while ($plugin === null) {
  74. $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
  75. }
  76. if (!$this->bake($plugin)) {
  77. $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
  78. }
  79. }
  80. /**
  81. * Bake the plugin, create directories and files
  82. *
  83. * @param string $plugin Name of the plugin in CamelCased format
  84. * @return boolean
  85. */
  86. public function bake($plugin) {
  87. $pathOptions = App::path('plugins');
  88. if (count($pathOptions) > 1) {
  89. $this->findPath($pathOptions);
  90. }
  91. $this->hr();
  92. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  93. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
  94. $this->hr();
  95. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  96. if (strtolower($looksGood) === 'y') {
  97. $Folder = new Folder($this->path . $plugin);
  98. $directories = array(
  99. 'Config' . DS . 'Schema',
  100. 'Model' . DS . 'Behavior',
  101. 'Model' . DS . 'Datasource',
  102. 'Console' . DS . 'Command' . DS . 'Task',
  103. 'Controller' . DS . 'Component',
  104. 'Lib',
  105. 'View' . DS . 'Helper',
  106. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  107. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  108. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  109. 'Test' . DS . 'Fixture',
  110. 'Vendor',
  111. 'webroot'
  112. );
  113. foreach ($directories as $directory) {
  114. $dirPath = $this->path . $plugin . DS . $directory;
  115. $Folder->create($dirPath);
  116. new File($dirPath . DS . 'empty', true);
  117. }
  118. foreach ($Folder->messages() as $message) {
  119. $this->out($message, 1, Shell::VERBOSE);
  120. }
  121. $errors = $Folder->errors();
  122. if (!empty($errors)) {
  123. foreach ($errors as $message) {
  124. $this->error($message);
  125. }
  126. return false;
  127. }
  128. $controllerFileName = $plugin . 'AppController.php';
  129. $out = "<?php\n\n";
  130. $out .= "App::uses('AppController', 'Controller');\n\n";
  131. $out .= "class {$plugin}AppController extends AppController {\n\n";
  132. $out .= "}\n";
  133. $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
  134. $modelFileName = $plugin . 'AppModel.php';
  135. $out = "<?php\n\n";
  136. $out .= "App::uses('AppModel', 'Model');\n\n";
  137. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  138. $out .= "}\n";
  139. $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
  140. $this->_modifyBootstrap($plugin);
  141. $this->hr();
  142. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
  143. }
  144. return true;
  145. }
  146. /**
  147. * Update the app's bootstrap.php file.
  148. *
  149. * @param string $plugin Name of plugin
  150. * @return void
  151. */
  152. protected function _modifyBootstrap($plugin) {
  153. $bootstrap = new File($this->bootstrap, false);
  154. $contents = $bootstrap->read();
  155. if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
  156. $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
  157. $this->out('');
  158. $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
  159. }
  160. }
  161. /**
  162. * find and change $this->path to the user selection
  163. *
  164. * @param array $pathOptions
  165. * @return void
  166. */
  167. public function findPath($pathOptions) {
  168. $valid = false;
  169. foreach ($pathOptions as $i => $path) {
  170. if (!is_dir($path)) {
  171. unset($pathOptions[$i]);
  172. }
  173. }
  174. $pathOptions = array_values($pathOptions);
  175. $max = count($pathOptions);
  176. while (!$valid) {
  177. foreach ($pathOptions as $i => $option) {
  178. $this->out($i + 1 . '. ' . $option);
  179. }
  180. $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
  181. $choice = $this->in($prompt, null, 1);
  182. if (intval($choice) > 0 && intval($choice) <= $max) {
  183. $valid = true;
  184. }
  185. }
  186. $this->path = $pathOptions[$choice - 1];
  187. }
  188. /**
  189. * Gets the option parser instance and configures it.
  190. *
  191. * @return ConsoleOptionParser
  192. */
  193. public function getOptionParser() {
  194. $parser = parent::getOptionParser();
  195. $parser->description(
  196. __d('cake_console', 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
  197. 'Can create plugins in any of your bootstrapped plugin paths.')
  198. )->addArgument('name', array(
  199. 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
  200. ));
  201. return $parser;
  202. }
  203. }