PluginTask.php 6.2 KB

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