ApiShell.php 7.0 KB

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