ApiShell.php 6.5 KB

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