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

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 237 lines | 157 code | 22 blank | 58 comment | 34 complexity | 99722a9803d2fed0c6687d6f43df73a7 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('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. if (count($this->args) == 1) {
  69. $file = $type;
  70. $class = Inflector::camelize($type);
  71. } elseif (count($this->args) > 1) {
  72. $file = Inflector::underscore($this->args[1]);
  73. $class = Inflector::camelize($this->args[1]);
  74. }
  75. $objects = App::objects('class', $path);
  76. if (in_array($class, $objects)) {
  77. if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) {
  78. if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) {
  79. $class .= Inflector::camelize($type);
  80. }
  81. }
  82. } else {
  83. $this->error(__d('cake_console', '%s not found', $class));
  84. }
  85. $parsed = $this->_parseClass($path . $class .'.php', $class);
  86. if (!empty($parsed)) {
  87. if (isset($this->params['method'])) {
  88. if (!isset($parsed[$this->params['method']])) {
  89. $this->err(__d('cake_console', '%s::%s() could not be found', $class, $this->params['method']));
  90. $this->_stop();
  91. }
  92. $method = $parsed[$this->params['method']];
  93. $this->out($class . '::' . $method['method'] . $method['parameters']);
  94. $this->hr();
  95. $this->out($method['comment'], true);
  96. } else {
  97. $this->out(ucwords($class));
  98. $this->hr();
  99. $i = 0;
  100. foreach ($parsed as $method) {
  101. $list[] = ++$i . ". " . $method['method'] . $method['parameters'];
  102. }
  103. $this->out($list);
  104. $methods = array_keys($parsed);
  105. 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'))) {
  106. if ($number === 'q') {
  107. $this->out(__d('cake_console', 'Done'));
  108. return $this->_stop();
  109. }
  110. if ($number === 'l') {
  111. $this->out($list);
  112. }
  113. if (isset($methods[--$number])) {
  114. $method = $parsed[$methods[$number]];
  115. $this->hr();
  116. $this->out($class . '::' . $method['method'] . $method['parameters']);
  117. $this->hr();
  118. $this->out($method['comment'], true);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * Get and configure the optionparser.
  126. *
  127. * @return ConsoleOptionParser
  128. */
  129. public function getOptionParser() {
  130. $parser = parent::getOptionParser();
  131. $parser->addArgument('type', array(
  132. 'help' => __d('cake_console', 'Either a full path or type of class (model, behavior, controller, component, view, helper)')
  133. ))->addArgument('className', array(
  134. 'help' => __d('cake_console', 'A CakePHP core class name (e.g: Component, HtmlHelper).')
  135. ))->addOption('method', array(
  136. 'short' => 'm',
  137. 'help' => __d('cake_console', 'The specific method you want help on.')
  138. ))->description(__d('cake_console', 'Lookup doc block comments for classes in CakePHP.'));
  139. return $parser;
  140. }
  141. /**
  142. * Show help for this shell.
  143. *
  144. * @return void
  145. */
  146. public function help() {
  147. $head = "Usage: cake api [<type>] <className> [-m <method>]\n";
  148. $head .= "-----------------------------------------------\n";
  149. $head .= "Parameters:\n\n";
  150. $commands = array(
  151. 'path' => "\t<type>\n" .
  152. "\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
  153. "\t\tAvailable values:\n\n".
  154. "\t\tbehavior\tLook for class in CakePHP behavior path\n".
  155. "\t\tcache\tLook for class in CakePHP cache path\n".
  156. "\t\tcontroller\tLook for class in CakePHP controller path\n".
  157. "\t\tcomponent\tLook for class in CakePHP component path\n".
  158. "\t\thelper\tLook for class in CakePHP helper path\n".
  159. "\t\tmodel\tLook for class in CakePHP model path\n".
  160. "\t\tview\tLook for class in CakePHP view path\n",
  161. 'className' => "\t<className>\n" .
  162. "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
  163. );
  164. $this->out($head);
  165. if (!isset($this->args[1])) {
  166. foreach ($commands as $cmd) {
  167. $this->out("{$cmd}\n\n");
  168. }
  169. } elseif (isset($commands[strtolower($this->args[1])])) {
  170. $this->out($commands[strtolower($this->args[1])] . "\n\n");
  171. } else {
  172. $this->out(__d('cake_console', 'Command %s not found', $this->args[1]));
  173. }
  174. }
  175. /**
  176. * Parse a given class (located on given file) and get public methods and their
  177. * signatures.
  178. *
  179. * @param string $path File path
  180. * @param string $class Class name
  181. * @return array Methods and signatures indexed by method name
  182. */
  183. protected function _parseClass($path, $class) {
  184. $parsed = array();
  185. if (!class_exists($class)) {
  186. if (!include_once($path)) {
  187. $this->err(__d('cake_console', '%s could not be found', $path));
  188. }
  189. }
  190. $reflection = new ReflectionClass($class);
  191. foreach ($reflection->getMethods() as $method) {
  192. if (!$method->isPublic() || strpos($method->getName(), '_') === 0) {
  193. continue;
  194. }
  195. if ($method->getDeclaringClass()->getName() != $class) {
  196. continue;
  197. }
  198. $args = array();
  199. foreach ($method->getParameters() as $param) {
  200. $paramString = '$' . $param->getName();
  201. if ($param->isDefaultValueAvailable()) {
  202. $paramString .= ' = ' . str_replace("\n", '', var_export($param->getDefaultValue(), true));
  203. }
  204. $args[] = $paramString;
  205. }
  206. $parsed[$method->getName()] = array(
  207. 'comment' => str_replace(array('/*', '*/', '*'), '', $method->getDocComment()),
  208. 'method' => $method->getName(),
  209. 'parameters' => '(' . implode(', ', $args) . ')'
  210. );
  211. }
  212. ksort($parsed);
  213. return $parsed;
  214. }
  215. }