PluginTask.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-2010, 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-2010, 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 CONTROLLERS directory
  29. *
  30. * @var array
  31. * @access public
  32. */
  33. public $path = null;
  34. /**
  35. * initialize
  36. *
  37. * @return void
  38. */
  39. function initialize() {
  40. $this->path = APP . 'plugins' . DS;
  41. }
  42. /**
  43. * Execution method always used for tasks
  44. *
  45. * @return void
  46. */
  47. public function execute() {
  48. $plugin = null;
  49. if (isset($this->args[0])) {
  50. $plugin = Inflector::camelize($this->args[0]);
  51. $pluginPath = $this->_pluginPath($plugin);
  52. if (is_dir($pluginPath)) {
  53. $this->out(__('Plugin: %s', $plugin));
  54. $this->out(__('Path: %s', $pluginPath));
  55. } else {
  56. $this->_interactive($plugin);
  57. }
  58. } else {
  59. return $this->_interactive();
  60. }
  61. }
  62. /**
  63. * Interactive interface
  64. *
  65. * @access private
  66. * @return void
  67. */
  68. protected function _interactive($plugin = null) {
  69. while ($plugin === null) {
  70. $plugin = $this->in(__('Enter the name of the plugin in CamelCase format'));
  71. }
  72. if (!$this->bake($plugin)) {
  73. $this->error(__("An error occured trying to bake: %s in %s", $plugin, $this->path . Inflector::underscore($pluginPath)));
  74. }
  75. }
  76. /**
  77. * Bake the plugin, create directories and files
  78. *
  79. * @params $plugin name of the plugin in CamelCased format
  80. * @access public
  81. * @return bool
  82. */
  83. public function bake($plugin) {
  84. $pluginPath = Inflector::underscore($plugin);
  85. $pathOptions = App::path('plugins');
  86. if (count($pathOptions) > 1) {
  87. $this->findPath($pathOptions);
  88. }
  89. $this->hr();
  90. $this->out(__("<info>Plugin Name:</info> %s", $plugin));
  91. $this->out(__("<info>Plugin Directory:</info> %s", $this->path . $pluginPath));
  92. $this->hr();
  93. $looksGood = $this->in(__('Look okay?'), array('y', 'n', 'q'), 'y');
  94. if (strtolower($looksGood) == 'y') {
  95. $Folder = new Folder($this->path . $pluginPath);
  96. $directories = array(
  97. 'config' . DS . 'schema',
  98. 'models' . DS . 'behaviors',
  99. 'models' . DS . 'datasources',
  100. 'console' . DS . 'shells' . DS . 'tasks',
  101. 'controllers' . DS . 'components',
  102. 'libs',
  103. 'views' . DS . 'helpers',
  104. 'tests' . DS . 'cases' . DS . 'components',
  105. 'tests' . DS . 'cases' . DS . 'helpers',
  106. 'tests' . DS . 'cases' . DS . 'behaviors',
  107. 'tests' . DS . 'cases' . DS . 'controllers',
  108. 'tests' . DS . 'cases' . DS . 'models',
  109. 'tests' . DS . 'groups',
  110. 'tests' . DS . 'fixtures',
  111. 'vendors',
  112. 'webroot'
  113. );
  114. foreach ($directories as $directory) {
  115. $dirPath = $this->path . $pluginPath . DS . $directory;
  116. $Folder->create($dirPath);
  117. $File = new File($dirPath . DS . 'empty', true);
  118. }
  119. foreach ($Folder->messages() as $message) {
  120. $this->out($message, 1, Shell::VERBOSE);
  121. }
  122. $errors = $Folder->errors();
  123. if (!empty($errors)) {
  124. return false;
  125. }
  126. $controllerFileName = $pluginPath . '_app_controller.php';
  127. $out = "<?php\n\n";
  128. $out .= "class {$plugin}AppController extends AppController {\n\n";
  129. $out .= "}\n\n";
  130. $out .= "?>";
  131. $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
  132. $modelFileName = $pluginPath . '_app_model.php';
  133. $out = "<?php\n\n";
  134. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  135. $out .= "}\n\n";
  136. $out .= "?>";
  137. $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
  138. $this->hr();
  139. $this->out(__('<success>Created:</success> %s in %s', $plugin, $this->path . $pluginPath), 2);
  140. }
  141. return true;
  142. }
  143. /**
  144. * find and change $this->path to the user selection
  145. *
  146. * @return string plugin path
  147. */
  148. public function findPath($pathOptions) {
  149. $valid = false;
  150. $max = count($pathOptions);
  151. while (!$valid) {
  152. foreach ($pathOptions as $i => $option) {
  153. $this->out($i + 1 .'. ' . $option);
  154. }
  155. $prompt = __('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(
  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' => __('CamelCased name of the plugin to create.')
  175. ));
  176. }
  177. }