PluginTask.php 6.1 KB

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