PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/console/libs/api.php

https://github.com/tanepiper/pastemonkey
PHP | 216 lines | 136 code | 19 blank | 61 comment | 32 complexity | 4b53f173f7940beafda4354db35495ce MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id: api.php 7945 2008-12-19 02:16:01Z gwoo $ */
  3. /**
  4. * API shell to get CakePHP core method signatures.
  5. *
  6. * Implementation of a Cake Shell to show CakePHP core method signatures.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.console.libs
  21. * @since CakePHP(tm) v 1.2.0.5012
  22. * @version $Revision: 7945 $
  23. * @modifiedby $LastChangedBy: gwoo $
  24. * @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. /**
  28. * API shell to show method signatures of CakePHP core classes.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.console.libs
  32. */
  33. class ApiShell extends Shell {
  34. /**
  35. * Map between short name for paths and real paths.
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. var $paths = array();
  41. /**
  42. * Override intialize of the Shell
  43. *
  44. * @access public
  45. */
  46. function initialize () {
  47. $this->paths = array_merge($this->paths, array(
  48. 'behavior' => LIBS . 'model' . DS . 'behaviors' . DS,
  49. 'cache' => LIBS . 'cache' . DS,
  50. 'controller' => LIBS . 'controller' . DS,
  51. 'component' => LIBS . 'controller' . DS . 'components' . DS,
  52. 'helper' => LIBS . 'view' . DS . 'helpers' . DS,
  53. 'model' => LIBS . 'model' . DS,
  54. 'view' => LIBS . 'view' . DS,
  55. 'core' => LIBS
  56. ));
  57. }
  58. /**
  59. * Override main() to handle action
  60. *
  61. * @access public
  62. */
  63. function main() {
  64. if (empty($this->args)) {
  65. return $this->help();
  66. }
  67. $type = low($this->args[0]);
  68. if (isset($this->paths[$type])) {
  69. $path = $this->paths[$type];
  70. } else {
  71. $path = $this->paths['core'];
  72. }
  73. if (count($this->args) == 1) {
  74. $file = $type;
  75. $class = Inflector::camelize($type);
  76. } elseif (count($this->args) > 1) {
  77. $file = Inflector::underscore($this->args[1]);
  78. $class = Inflector::camelize($file);
  79. }
  80. $objects = Configure::listObjects('class', $path);
  81. if (in_array($class, $objects)) {
  82. if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) {
  83. if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) {
  84. $class .= Inflector::camelize($type);
  85. }
  86. }
  87. } else {
  88. $this->err(sprintf(__("%s not found", true), $class));
  89. $this->_stop();
  90. }
  91. $parsed = $this->__parseClass($path . $file .'.php');
  92. if (!empty($parsed)) {
  93. if (isset($this->params['m'])) {
  94. if (!isset($parsed[$this->params['m']])) {
  95. $this->err(sprintf(__("%s::%s() could not be found", true), $class, $this->params['m']));
  96. $this->_stop();
  97. }
  98. $method = $parsed[$this->params['m']];
  99. $this->out($class .'::'.$method['method'] . $method['parameters']);
  100. $this->hr();
  101. $this->out($method['comment'], true);
  102. } else {
  103. $this->out(ucwords($class));
  104. $this->hr();
  105. $i = 0;
  106. foreach ($parsed as $method) {
  107. $list[] = ++$i . ". " . $method['method'] . $method['parameters'];
  108. }
  109. $this->out($list);
  110. $methods = array_keys($parsed);
  111. while ($number = $this->in(__('Select a number to see the more information about a specific method. q to quit. l to list.', true), null, 'q')) {
  112. if ($number === 'q') {
  113. $this->out(__('Done', true));
  114. $this->_stop();
  115. }
  116. if ($number === 'l') {
  117. $this->out($list);
  118. }
  119. if (isset($methods[--$number])) {
  120. $method = $parsed[$methods[$number]];
  121. $this->hr();
  122. $this->out($class .'::'.$method['method'] . $method['parameters']);
  123. $this->hr();
  124. $this->out($method['comment'], true);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * Show help for this shell.
  132. *
  133. * @access public
  134. */
  135. function help() {
  136. $head = "Usage: cake api [<type>] <className> [-m <method>]\n";
  137. $head .= "-----------------------------------------------\n";
  138. $head .= "Parameters:\n\n";
  139. $commands = array(
  140. 'path' => "\t<type>\n" .
  141. "\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
  142. "\t\tAvailable values:\n\n".
  143. "\t\tbehavior\tLook for class in CakePHP behavior path\n".
  144. "\t\tcache\tLook for class in CakePHP cache path\n".
  145. "\t\tcontroller\tLook for class in CakePHP controller path\n".
  146. "\t\tcomponent\tLook for class in CakePHP component path\n".
  147. "\t\thelper\tLook for class in CakePHP helper path\n".
  148. "\t\tmodel\tLook for class in CakePHP model path\n".
  149. "\t\tview\tLook for class in CakePHP view path\n",
  150. 'className' => "\t<className>\n" .
  151. "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
  152. );
  153. $this->out($head);
  154. if (!isset($this->args[1])) {
  155. foreach ($commands as $cmd) {
  156. $this->out("{$cmd}\n\n");
  157. }
  158. } elseif (isset($commands[low($this->args[1])])) {
  159. $this->out($commands[low($this->args[1])] . "\n\n");
  160. } else {
  161. $this->out("Command '" . $this->args[1] . "' not found");
  162. }
  163. }
  164. /**
  165. * Parse a given class (located on given file) and get public methods and their
  166. * signatures.
  167. *
  168. * @param object $File File object
  169. * @param string $class Class name
  170. * @return array Methods and signatures indexed by method name
  171. * @access private
  172. */
  173. function __parseClass($path) {
  174. $parsed = array();
  175. $File = new File($path);
  176. if (!$File->exists()) {
  177. $this->err(sprintf(__("%s could not be found", true), $File->name));
  178. $this->_stop();
  179. }
  180. $contents = $File->read();
  181. if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.+\\))%', $contents, $result, PREG_PATTERN_ORDER)) {
  182. foreach ($result[2] as $key => $method) {
  183. $method = str_replace('function ', '', trim($method));
  184. if (strpos($method, '__') === false && $method[0] != '_') {
  185. $parsed[$method] = array(
  186. 'comment' => r(array('/*', '*/', '*'), '', trim($result[1][$key])),
  187. 'method' => $method,
  188. 'parameters' => trim($result[3][$key]),
  189. );
  190. }
  191. }
  192. }
  193. ksort($parsed);
  194. return $parsed;
  195. }
  196. }
  197. ?>