PluginTask.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 plugins 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 = 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 . Inflector::camelize($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. $pluginPath = Inflector::camelize($plugin);
  84. $pathOptions = App::path('plugins');
  85. var_dump($pathOptions);
  86. if (count($pathOptions) > 1) {
  87. $this->findPath($pathOptions);
  88. }
  89. $this->hr();
  90. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  91. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $pluginPath));
  92. $this->hr();
  93. $looksGood = $this->in(__d('cake_console', '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. 'Model' . DS . 'Behavior',
  99. 'Model' . DS . 'Datasource',
  100. 'Console' . DS . 'Command' . DS . 'Task',
  101. 'Controller' . DS . 'Component',
  102. 'Lib',
  103. 'View' . DS . 'Helper',
  104. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  105. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  106. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  107. 'Test' . DS . 'Fixture',
  108. 'Vendor',
  109. 'webroot'
  110. );
  111. foreach ($directories as $directory) {
  112. $dirPath = $this->path . $pluginPath . DS . $directory;
  113. $Folder->create($dirPath);
  114. $File = new File($dirPath . DS . 'empty', true);
  115. }
  116. foreach ($Folder->messages() as $message) {
  117. $this->out($message, 1, Shell::VERBOSE);
  118. }
  119. $errors = $Folder->errors();
  120. if (!empty($errors)) {
  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. $out .= "?>";
  128. $this->createFile($this->path . $pluginPath. DS . 'Controller' . DS . $controllerFileName, $out);
  129. $modelFileName = $plugin . 'AppModel.php';
  130. $out = "<?php\n\n";
  131. $out .= "class {$plugin}AppModel extends AppModel {\n\n";
  132. $out .= "}\n\n";
  133. $out .= "?>";
  134. $this->createFile($this->path . $pluginPath . DS . 'Model' . DS . $modelFileName, $out);
  135. $this->hr();
  136. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $pluginPath), 2);
  137. }
  138. return true;
  139. }
  140. /**
  141. * find and change $this->path to the user selection
  142. *
  143. * @return string plugin path
  144. */
  145. public function findPath($pathOptions) {
  146. $valid = false;
  147. $max = count($pathOptions);
  148. while (!$valid) {
  149. foreach ($pathOptions as $i => $option) {
  150. $this->out($i + 1 .'. ' . $option);
  151. }
  152. $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
  153. $choice = $this->in($prompt);
  154. if (intval($choice) > 0 && intval($choice) <= $max) {
  155. $valid = true;
  156. }
  157. }
  158. $this->path = $pathOptions[$choice - 1];
  159. }
  160. /**
  161. * get the option parser for the plugin task
  162. *
  163. * @return void
  164. */
  165. public function getOptionParser() {
  166. $parser = parent::getOptionParser();
  167. return $parser->description(__d('cake_console',
  168. 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
  169. 'Can create plugins in any of your bootstrapped plugin paths.'
  170. ))->addArgument('name', array(
  171. 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
  172. ));
  173. }
  174. }