PluginTask.php 5.2 KB

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