ApiShell.php 7.1 KB

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