PluginTask.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.console.shells.tasks
  16. * @since CakePHP(tm) v 1.2
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('File', 'Utility');
  20. App::uses('Folder', 'Utility');
  21. /**
  22. * Task class for creating a plugin
  23. *
  24. * @package cake.console.shells.tasks
  25. */
  26. class PluginTask extends Shell {
  27. /**
  28. * path to plugins directory
  29. *
  30. * @var array
  31. * @access public
  32. */
  33. public $path = null;
  34. /**
  35. * initialize
  36. *
  37. * @return void
  38. */
  39. public function initialize() {
  40. $this->path = current(App::path('plugins'));
  41. }
  42. /**
  43. * Execution method always used for tasks
  44. *
  45. * @return void
  46. */
  47. public function execute() {
  48. if (isset($this->args[0])) {
  49. $plugin = Inflector::camelize($this->args[0]);
  50. $pluginPath = $this->_pluginPath($plugin);
  51. if (is_dir($pluginPath)) {
  52. $this->out(__d('cake_console', 'Plugin: %s', $plugin));
  53. $this->out(__d('cake_console', 'Path: %s', $pluginPath));
  54. } else {
  55. $this->_interactive($plugin);
  56. }
  57. } else {
  58. return $this->_interactive();
  59. }
  60. }
  61. /**
  62. * Interactive interface
  63. *
  64. * @access private
  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 occured trying to bake: %s in %s", $plugin, $this->path . $plugin));
  73. }
  74. }
  75. /**
  76. * Bake the plugin, create directories and files
  77. *
  78. * @params $plugin name of the plugin in CamelCased format
  79. * @access public
  80. * @return bool
  81. */
  82. public function bake($plugin) {
  83. $pathOptions = App::path('plugins');
  84. if (count($pathOptions) > 1) {
  85. $this->findPath($pathOptions);
  86. }
  87. $this->hr();
  88. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  89. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
  90. $this->hr();
  91. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  92. if (strtolower($looksGood) == 'y') {
  93. $Folder = new Folder($this->path . $plugin);
  94. $directories = array(
  95. 'Config' . DS . 'schema',
  96. 'Model' . DS . 'Behavior',
  97. 'Model' . DS . 'Datasource',
  98. 'Console' . DS . 'Command' . DS . 'Task',
  99. 'Controller' . DS . 'Component',
  100. 'Lib',
  101. 'View' . DS . 'Helper',
  102. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  103. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  104. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  105. 'Test' . DS . 'Fixture',
  106. 'Vendor',
  107. 'webroot'
  108. );
  109. foreach ($directories as $directory) {
  110. $dirPath = $this->path . $plugin . DS . $directory;
  111. $Folder->create($dirPath);
  112. $File = new File($dirPath . DS . 'empty', true);
  113. }
  114. foreach ($Folder->messages() as $message) {
  115. $this->out($message, 1, Shell::VERBOSE);
  116. }
  117. $errors = $Folder->errors();
  118. if (!empty($errors)) {
  119. return false;
  120. }
  121. $controllerFileName = $plugin . 'AppController.php';
  122. $out = "<?php\n\n";
  123. $out .= "class {$plugin}AppController extends AppController {\n\n";
  124. $out .= "}\n\n";
  125. $out .= "?>";
  126. $this->createFile($this->path . $plugin. DS . 'Controller' . DS . $controllerFileName, $out);
  127. $modelFileName = $plugin . 'AppModel.php';
  128. $out = "<?php\n\n";
  129. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  130. $out .= "}\n\n";
  131. $out .= "?>";
  132. $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
  133. $this->hr();
  134. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
  135. }
  136. return true;
  137. }
  138. /**
  139. * find and change $this->path to the user selection
  140. *
  141. * @return string plugin path
  142. */
  143. public function findPath($pathOptions) {
  144. $valid = false;
  145. foreach ($pathOptions as $i =>$path) {
  146. if(!is_dir($path)) {
  147. array_splice($pathOptions, $i, 1);
  148. }
  149. }
  150. $max = count($pathOptions);
  151. while (!$valid) {
  152. foreach ($pathOptions as $i => $option) {
  153. $this->out($i + 1 .'. ' . $option);
  154. }
  155. $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
  156. $choice = $this->in($prompt);
  157. if (intval($choice) > 0 && intval($choice) <= $max) {
  158. $valid = true;
  159. }
  160. }
  161. $this->path = $pathOptions[$choice - 1];
  162. }
  163. /**
  164. * get the option parser for the plugin task
  165. *
  166. * @return void
  167. */
  168. public function getOptionParser() {
  169. $parser = parent::getOptionParser();
  170. return $parser->description(__d('cake_console',
  171. 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
  172. 'Can create plugins in any of your bootstrapped plugin paths.'
  173. ))->addArgument('name', array(
  174. 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
  175. ));
  176. }
  177. }