PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/Command/Task/PluginTask.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 197 lines | 115 code | 23 blank | 59 comment | 14 complexity | e33378b59fe46addb644f6a4ec4140d9 MD5 | raw file
  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('AppShell', 'Console/Command');
  19. App::uses('File', 'Utility');
  20. App::uses('Folder', 'Utility');
  21. /**
  22. * The Plugin Task handles creating an empty plugin, ready to be used
  23. *
  24. * @package Cake.Console.Command.Task
  25. */
  26. class PluginTask extends AppShell {
  27. /**
  28. * path to plugins directory
  29. *
  30. * @var array
  31. */
  32. public $path = null;
  33. /**
  34. * initialize
  35. *
  36. * @return void
  37. */
  38. public function initialize() {
  39. $this->path = current(App::path('plugins'));
  40. }
  41. /**
  42. * Execution method always used for tasks
  43. *
  44. * @return void
  45. */
  46. public function execute() {
  47. if (isset($this->args[0])) {
  48. $plugin = Inflector::camelize($this->args[0]);
  49. $pluginPath = $this->_pluginPath($plugin);
  50. if (is_dir($pluginPath)) {
  51. $this->out(__d('cake_console', 'Plugin: %s', $plugin));
  52. $this->out(__d('cake_console', 'Path: %s', $pluginPath));
  53. } else {
  54. $this->_interactive($plugin);
  55. }
  56. } else {
  57. return $this->_interactive();
  58. }
  59. }
  60. /**
  61. * Interactive interface
  62. *
  63. * @param string $plugin
  64. * @return void
  65. */
  66. protected function _interactive($plugin = null) {
  67. while ($plugin === null) {
  68. $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
  69. }
  70. if (!$this->bake($plugin)) {
  71. $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
  72. }
  73. }
  74. /**
  75. * Bake the plugin, create directories and files
  76. *
  77. * @param string $plugin Name of the plugin in CamelCased format
  78. * @return boolean
  79. */
  80. public function bake($plugin) {
  81. $pathOptions = App::path('plugins');
  82. if (count($pathOptions) > 1) {
  83. $this->findPath($pathOptions);
  84. }
  85. $this->hr();
  86. $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
  87. $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
  88. $this->hr();
  89. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  90. if (strtolower($looksGood) == 'y') {
  91. $Folder = new Folder($this->path . $plugin);
  92. $directories = array(
  93. 'Config' . DS . 'Schema',
  94. 'Model' . DS . 'Behavior',
  95. 'Model' . DS . 'Datasource',
  96. 'Console' . DS . 'Command' . DS . 'Task',
  97. 'Controller' . DS . 'Component',
  98. 'Lib',
  99. 'View' . DS . 'Helper',
  100. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  101. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  102. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  103. 'Test' . DS . 'Fixture',
  104. 'Vendor',
  105. 'webroot'
  106. );
  107. foreach ($directories as $directory) {
  108. $dirPath = $this->path . $plugin . DS . $directory;
  109. $Folder->create($dirPath);
  110. $File = new File($dirPath . DS . 'empty', true);
  111. }
  112. foreach ($Folder->messages() as $message) {
  113. $this->out($message, 1, Shell::VERBOSE);
  114. }
  115. $errors = $Folder->errors();
  116. if (!empty($errors)) {
  117. return false;
  118. }
  119. $controllerFileName = $plugin . 'AppController.php';
  120. $out = "<?php\n\n";
  121. $out .= "class {$plugin}AppController extends AppController {\n\n";
  122. $out .= "}\n\n";
  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. $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
  129. $this->hr();
  130. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
  131. }
  132. return true;
  133. }
  134. /**
  135. * find and change $this->path to the user selection
  136. *
  137. * @param array $pathOptions
  138. * @return string plugin path
  139. */
  140. public function findPath($pathOptions) {
  141. $valid = false;
  142. foreach ($pathOptions as $i => $path) {
  143. if (!is_dir($path)) {
  144. array_splice($pathOptions, $i, 1);
  145. }
  146. }
  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. }