plugin.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * The Plugin Task handles creating an empty plugin, ready to be used
  4. *
  5. * Long description for file
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @filesource
  16. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  17. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  18. * @package cake
  19. * @subpackage cake.cake.console.libs.tasks
  20. * @since CakePHP(tm) v 1.2
  21. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  22. */
  23. /**
  24. * Task class for creating a plugin
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.console.libs.tasks
  28. */
  29. class PluginTask extends Shell {
  30. /**
  31. * Tasks
  32. *
  33. */
  34. var $tasks = array('Model', 'Controller', 'View');
  35. /**
  36. * path to CONTROLLERS directory
  37. *
  38. * @var array
  39. * @access public
  40. */
  41. var $path = null;
  42. /**
  43. * initialize
  44. *
  45. * @return void
  46. */
  47. function initialize() {
  48. $this->path = APP . 'plugins' . DS;
  49. }
  50. /**
  51. * Execution method always used for tasks
  52. *
  53. * @return void
  54. */
  55. function execute() {
  56. if (empty($this->params['skel'])) {
  57. $this->params['skel'] = '';
  58. if (is_dir(CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel') === true) {
  59. $this->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel';
  60. }
  61. }
  62. $plugin = null;
  63. if (isset($this->args[0])) {
  64. $plugin = Inflector::camelize($this->args[0]);
  65. $pluginPath = $this->_pluginPath($plugin);
  66. $this->Dispatch->shiftArgs();
  67. if (is_dir($pluginPath)) {
  68. $this->out(sprintf(__('Plugin: %s', true), $plugin));
  69. $this->out(sprintf(__('Path: %s', true), $pluginPath));
  70. } elseif (isset($this->args[0])) {
  71. $this->err(sprintf(__('%s in path %s not found.', true), $plugin, $pluginPath));
  72. $this->_stop();
  73. } else {
  74. $this->__interactive($plugin);
  75. }
  76. } else {
  77. return $this->__interactive();
  78. }
  79. if (isset($this->args[0])) {
  80. $task = Inflector::classify($this->args[0]);
  81. $this->Dispatch->shiftArgs();
  82. if (in_array($task, $this->tasks)) {
  83. $this->{$task}->plugin = $plugin;
  84. $this->{$task}->path = $pluginPath . Inflector::underscore(Inflector::pluralize($task)) . DS;
  85. if (!is_dir($this->{$task}->path)) {
  86. $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
  87. }
  88. $this->{$task}->loadTasks();
  89. return $this->{$task}->execute();
  90. }
  91. }
  92. }
  93. /**
  94. * Interactive interface
  95. *
  96. * @access private
  97. * @return void
  98. */
  99. function __interactive($plugin = null) {
  100. while ($plugin === null) {
  101. $plugin = $this->in(__('Enter the name of the plugin in CamelCase format', true));
  102. }
  103. if (!$this->bake($plugin)) {
  104. $this->err(sprintf(__("An error occured trying to bake: %s in %s", true), $plugin, $this->path . $pluginPath));
  105. }
  106. }
  107. /**
  108. * Bake the plugin, create directories and files
  109. *
  110. * @params $plugin name of the plugin in CamelCased format
  111. * @access public
  112. * @return bool
  113. */
  114. function bake($plugin) {
  115. $pluginPath = Inflector::underscore($plugin);
  116. $pathOptions = App::path('plugins');
  117. if (count($pathOptions) > 1) {
  118. $this->findPath($pathOptions);
  119. }
  120. $this->hr();
  121. $this->out(sprintf(__("Plugin Name: %s", true), $plugin));
  122. $this->out(sprintf(__("Plugin Directory: %s", true), $this->path . $pluginPath));
  123. $this->hr();
  124. $looksGood = $this->in(__('Look okay?', true), array('y', 'n', 'q'), 'y');
  125. if (strtolower($looksGood) == 'y') {
  126. $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
  127. $Folder =& new Folder($this->path . $pluginPath);
  128. $directories = array(
  129. 'config' . DS . 'schema',
  130. 'models' . DS . 'behaviors',
  131. 'models' . DS . 'datasources',
  132. 'controllers' . DS . 'components',
  133. 'views' . DS . 'helpers',
  134. 'tests' . DS . 'cases' . DS . 'components',
  135. 'tests' . DS . 'cases' . DS . 'helpers',
  136. 'tests' . DS . 'cases' . DS . 'behaviors',
  137. 'tests' . DS . 'cases' . DS . 'controllers',
  138. 'tests' . DS . 'cases' . DS . 'models',
  139. 'tests' . DS . 'groups',
  140. 'tests' . DS . 'fixtures',
  141. 'vendors' . DS . 'img',
  142. 'vendors' . DS . 'js',
  143. 'vendors' . DS . 'css',
  144. 'vendors' . DS . 'shells' . DS . 'tasks'
  145. );
  146. foreach ($directories as $directory) {
  147. $dirPath = $this->path . $pluginPath . DS . $directory;
  148. $Folder->create($dirPath);
  149. $File =& new File($dirPath . DS . 'empty', true);
  150. }
  151. if (strtolower($verbose) == 'y') {
  152. foreach ($Folder->messages() as $message) {
  153. $this->out($message);
  154. }
  155. }
  156. $errors = $Folder->errors();
  157. if (!empty($errors)) {
  158. return false;
  159. }
  160. $controllerFileName = $pluginPath . '_app_controller.php';
  161. $out = "<?php\n\n";
  162. $out .= "class {$plugin}AppController extends AppController {\n\n";
  163. $out .= "}\n\n";
  164. $out .= "?>";
  165. $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
  166. $modelFileName = $pluginPath . '_app_model.php';
  167. $out = "<?php\n\n";
  168. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  169. $out .= "}\n\n";
  170. $out .= "?>";
  171. $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
  172. $this->hr();
  173. $this->out(sprintf(__("Created: %s in %s", true), $plugin, $this->path . $pluginPath));
  174. $this->hr();
  175. }
  176. return true;
  177. }
  178. /**
  179. * find and change $this->path to the user selection
  180. *
  181. * @return void
  182. **/
  183. function findPath($pathOptions) {
  184. $valid = false;
  185. $max = count($pathOptions);
  186. while (!$valid) {
  187. foreach ($pathOptions as $i => $option) {
  188. $this->out($i + 1 .'. ' . $option);
  189. }
  190. $prompt = __('Choose a plugin path from the paths above.', true);
  191. $choice = $this->in($prompt);
  192. if (intval($choice) > 0 && intval($choice) <= $max) {
  193. $valid = true;
  194. }
  195. }
  196. $this->path = $pathOptions[$choice - 1];
  197. }
  198. /**
  199. * Help
  200. *
  201. * @return void
  202. * @access public
  203. */
  204. function help() {
  205. $this->hr();
  206. $this->out("Usage: cake bake plugin <arg1> <arg2>...");
  207. $this->hr();
  208. $this->out('Commands:');
  209. $this->out('');
  210. $this->out("plugin <name>");
  211. $this->out("\tbakes plugin directory structure");
  212. $this->out('');
  213. $this->out("plugin <name> model");
  214. $this->out("\tbakes model. Run 'cake bake model help' for more info.");
  215. $this->out('');
  216. $this->out("plugin <name> controller");
  217. $this->out("\tbakes controller. Run 'cake bake controller help' for more info.");
  218. $this->out('');
  219. $this->out("plugin <name> view");
  220. $this->out("\tbakes view. Run 'cake bake view help' for more info.");
  221. $this->out("");
  222. $this->_stop();
  223. }
  224. }
  225. ?>