PluginTask.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * initialize
  35. *
  36. * @return void
  37. */
  38. public function initialize() {
  39. $this->path = current(App::path('plugins'));
  40. }
  41. /**
  42. * Execution method always used for tasks
  43. *
  44. * @return void
  45. */
  46. public function execute() {
  47. if (isset($this->args[0])) {
  48. $plugin = Inflector::camelize($this->args[0]);
  49. $pluginPath = $this->_pluginPath($plugin);
  50. if (is_dir($pluginPath)) {
  51. $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
  52. $this->out(__d('cake_console', 'Path: %s', $pluginPath));
  53. return false;
  54. } else {
  55. $this->_interactive($plugin);
  56. }
  57. } else {
  58. return $this->_interactive();
  59. }
  60. }
  61. /**
  62. * Interactive interface
  63. *
  64. * @param string $plugin
  65. * @return void
  66. */
  67. protected function _interactive($plugin = null) {
  68. while ($plugin === null) {
  69. $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
  70. }
  71. if (!$this->bake($plugin)) {
  72. $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
  73. }
  74. }
  75. /**
  76. * Bake the plugin, create directories and files
  77. *
  78. * @param string $plugin Name of the plugin in CamelCased format
  79. * @return boolean
  80. */
  81. public function bake($plugin) {
  82. $pathOptions = App::path('plugins');
  83. if (count($pathOptions) > 1) {
  84. $this->findPath($pathOptions);
  85. }
  86. $this->hr();
  87. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  88. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
  89. $this->hr();
  90. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  91. if (strtolower($looksGood) == 'y') {
  92. $Folder = new Folder($this->path . $plugin);
  93. $directories = array(
  94. 'Config' . DS . 'Schema',
  95. 'Model' . DS . 'Behavior',
  96. 'Model' . DS . 'Datasource',
  97. 'Console' . DS . 'Command' . DS . 'Task',
  98. 'Controller' . DS . 'Component',
  99. 'Lib',
  100. 'View' . DS . 'Helper',
  101. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  102. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  103. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  104. 'Test' . DS . 'Fixture',
  105. 'Vendor',
  106. 'webroot'
  107. );
  108. foreach ($directories as $directory) {
  109. $dirPath = $this->path . $plugin . DS . $directory;
  110. $Folder->create($dirPath);
  111. $File = new File($dirPath . DS . 'empty', true);
  112. }
  113. foreach ($Folder->messages() as $message) {
  114. $this->out($message, 1, Shell::VERBOSE);
  115. }
  116. $errors = $Folder->errors();
  117. if (!empty($errors)) {
  118. foreach ($errors as $message) {
  119. $this->error($message);
  120. }
  121. return false;
  122. }
  123. $controllerFileName = $plugin . 'AppController.php';
  124. $out = "<?php\n\n";
  125. $out .= "class {$plugin}AppController extends AppController {\n\n";
  126. $out .= "}\n\n";
  127. $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
  128. $modelFileName = $plugin . 'AppModel.php';
  129. $out = "<?php\n\n";
  130. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  131. $out .= "}\n\n";
  132. $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
  133. $bootstrap = new File(APP . 'Config' . DS . 'bootstrap.php', false);
  134. $contents = $bootstrap->read();
  135. if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
  136. $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
  137. $this->out('');
  138. $this->out(__d('cake_dev', '%s modified', APP . 'Config' . DS . 'bootstrap.php'));
  139. }
  140. $this->hr();
  141. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
  142. }
  143. return true;
  144. }
  145. /**
  146. * find and change $this->path to the user selection
  147. *
  148. * @param array $pathOptions
  149. * @return string plugin path
  150. */
  151. public function findPath($pathOptions) {
  152. $valid = false;
  153. foreach ($pathOptions as $i => $path) {
  154. if (!is_dir($path)) {
  155. array_splice($pathOptions, $i, 1);
  156. }
  157. }
  158. $max = count($pathOptions);
  159. while (!$valid) {
  160. foreach ($pathOptions as $i => $option) {
  161. $this->out($i + 1 . '. ' . $option);
  162. }
  163. $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
  164. $choice = $this->in($prompt, null, 1);
  165. if (intval($choice) > 0 && intval($choice) <= $max) {
  166. $valid = true;
  167. }
  168. }
  169. $this->path = $pathOptions[$choice - 1];
  170. }
  171. /**
  172. * get the option parser for the plugin task
  173. *
  174. * @return void
  175. */
  176. public function getOptionParser() {
  177. $parser = parent::getOptionParser();
  178. return $parser->description(__d('cake_console',
  179. 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
  180. 'Can create plugins in any of your bootstrapped plugin paths.'
  181. ))->addArgument('name', array(
  182. 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
  183. ));
  184. }
  185. }