PluginTask.php 5.2 KB

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