PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/Command/ApiShell.php

https://bitbucket.org/luobailiang/cake
PHP | 232 lines | 156 code | 20 blank | 56 comment | 34 complexity | 4fa4543a7d7461556d525ec58b524d13 MD5 | raw file
  1. <?php
  2. /**
  3. * API shell to get CakePHP core method signatures.
  4. *
  5. * Implementation of a Cake Shell to show CakePHP core method signatures.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @since CakePHP(tm) v 1.2.0.5012
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('File', 'Utility');
  21. /**
  22. * API shell to show method signatures of CakePHP core classes.
  23. *
  24. * @package Cake.Console.Command
  25. */
  26. class ApiShell extends Shell {
  27. /**
  28. * Map between short name for paths and real paths.
  29. *
  30. * @var array
  31. */
  32. public $paths = array();
  33. /**
  34. * Override initialize of the Shell
  35. *
  36. * @return void
  37. */
  38. public function initialize() {
  39. $this->paths = array_merge($this->paths, array(
  40. 'behavior' => CAKE . 'Model' . DS . 'Behavior' . DS,
  41. 'cache' => CAKE . 'Cache' . DS,
  42. 'controller' => CAKE . 'Controller' . DS,
  43. 'component' => CAKE . 'Controller' . DS . 'Component' . DS,
  44. 'helper' => CAKE . 'View' . DS . 'Helper' . DS,
  45. 'model' => CAKE . 'Model' . DS,
  46. 'view' => CAKE . 'View' . DS,
  47. 'core' => CAKE
  48. ));
  49. }
  50. /**
  51. * Override main() to handle action
  52. *
  53. * @return void
  54. */
  55. public function main() {
  56. if (empty($this->args)) {
  57. return $this->out($this->OptionParser->help());
  58. }
  59. $type = strtolower($this->args[0]);
  60. if (isset($this->paths[$type])) {
  61. $path = $this->paths[$type];
  62. } else {
  63. $path = $this->paths['core'];
  64. }
  65. if (count($this->args) == 1) {
  66. $file = $type;
  67. $class = Inflector::camelize($type);
  68. } elseif (count($this->args) > 1) {
  69. $file = Inflector::underscore($this->args[1]);
  70. $class = Inflector::camelize($this->args[1]);
  71. }
  72. $objects = App::objects('class', $path);
  73. if (in_array($class, $objects)) {
  74. if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) {
  75. if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) {
  76. $class .= Inflector::camelize($type);
  77. }
  78. }
  79. } else {
  80. $this->error(__d('cake_console', '%s not found', $class));
  81. }
  82. $parsed = $this->_parseClass($path . $class .'.php', $class);
  83. if (!empty($parsed)) {
  84. if (isset($this->params['method'])) {
  85. if (!isset($parsed[$this->params['method']])) {
  86. $this->err(__d('cake_console', '%s::%s() could not be found', $class, $this->params['method']));
  87. $this->_stop();
  88. }
  89. $method = $parsed[$this->params['method']];
  90. $this->out($class .'::'.$method['method'] . $method['parameters']);
  91. $this->hr();
  92. $this->out($method['comment'], true);
  93. } else {
  94. $this->out(ucwords($class));
  95. $this->hr();
  96. $i = 0;
  97. foreach ($parsed as $method) {
  98. $list[] = ++$i . ". " . $method['method'] . $method['parameters'];
  99. }
  100. $this->out($list);
  101. $methods = array_keys($parsed);
  102. while ($number = strtolower($this->in(__d('cake_console', 'Select a number to see the more information about a specific method. q to quit. l to list.'), null, 'q'))) {
  103. if ($number === 'q') {
  104. $this->out(__d('cake_console', 'Done'));
  105. return $this->_stop();
  106. }
  107. if ($number === 'l') {
  108. $this->out($list);
  109. }
  110. if (isset($methods[--$number])) {
  111. $method = $parsed[$methods[$number]];
  112. $this->hr();
  113. $this->out($class .'::'.$method['method'] . $method['parameters']);
  114. $this->hr();
  115. $this->out($method['comment'], true);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Get and configure the optionparser.
  123. *
  124. * @return ConsoleOptionParser
  125. */
  126. public function getOptionParser() {
  127. $parser = parent::getOptionParser();
  128. $parser->addArgument('type', array(
  129. 'help' => __d('cake_console', 'Either a full path or type of class (model, behavior, controller, component, view, helper)')
  130. ))->addArgument('className', array(
  131. 'help' => __d('cake_console', 'A CakePHP core class name (e.g: Component, HtmlHelper).')
  132. ))->addOption('method', array(
  133. 'short' => 'm',
  134. 'help' => __d('cake_console', 'The specific method you want help on.')
  135. ))->description(__d('cake_console', 'Lookup doc block comments for classes in CakePHP.'));
  136. return $parser;
  137. }
  138. /**
  139. * Show help for this shell.
  140. *
  141. * @return void
  142. */
  143. public function help() {
  144. $head = "Usage: cake api [<type>] <className> [-m <method>]\n";
  145. $head .= "-----------------------------------------------\n";
  146. $head .= "Parameters:\n\n";
  147. $commands = array(
  148. 'path' => "\t<type>\n" .
  149. "\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
  150. "\t\tAvailable values:\n\n".
  151. "\t\tbehavior\tLook for class in CakePHP behavior path\n".
  152. "\t\tcache\tLook for class in CakePHP cache path\n".
  153. "\t\tcontroller\tLook for class in CakePHP controller path\n".
  154. "\t\tcomponent\tLook for class in CakePHP component path\n".
  155. "\t\thelper\tLook for class in CakePHP helper path\n".
  156. "\t\tmodel\tLook for class in CakePHP model path\n".
  157. "\t\tview\tLook for class in CakePHP view path\n",
  158. 'className' => "\t<className>\n" .
  159. "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
  160. );
  161. $this->out($head);
  162. if (!isset($this->args[1])) {
  163. foreach ($commands as $cmd) {
  164. $this->out("{$cmd}\n\n");
  165. }
  166. } elseif (isset($commands[strtolower($this->args[1])])) {
  167. $this->out($commands[strtolower($this->args[1])] . "\n\n");
  168. } else {
  169. $this->out(__d('cake_console', 'Command %s not found', $this->args[1]));
  170. }
  171. }
  172. /**
  173. * Parse a given class (located on given file) and get public methods and their
  174. * signatures.
  175. *
  176. * @param string $path File path
  177. * @param string $class Class name
  178. * @return array Methods and signatures indexed by method name
  179. */
  180. protected function _parseClass($path, $class) {
  181. $parsed = array();
  182. if (!class_exists($class)) {
  183. if (!include_once($path)) {
  184. $this->err(__d('cake_console', '%s could not be found', $path));
  185. }
  186. }
  187. $reflection = new ReflectionClass($class);
  188. foreach ($reflection->getMethods() as $method) {
  189. if (!$method->isPublic() || strpos($method->getName(), '_') === 0) {
  190. continue;
  191. }
  192. if ($method->getDeclaringClass()->getName() != $class) {
  193. continue;
  194. }
  195. $args = array();
  196. foreach ($method->getParameters() as $param) {
  197. $paramString = '$' . $param->getName();
  198. if ($param->isDefaultValueAvailable()) {
  199. $paramString .= ' = ' . str_replace("\n", '', var_export($param->getDefaultValue(), true));
  200. }
  201. $args[] = $paramString;
  202. }
  203. $parsed[$method->getName()] = array(
  204. 'comment' => str_replace(array('/*', '*/', '*'), '', $method->getDocComment()),
  205. 'method' => $method->getName(),
  206. 'parameters' => '(' . implode(', ', $args) . ')'
  207. );
  208. }
  209. ksort($parsed);
  210. return $parsed;
  211. }
  212. }