PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/code/web/public_php/webtt/cake/console/libs/api.php

https://bitbucket.org/dfighter1985/ryzomcore
PHP | 213 lines | 135 code | 22 blank | 56 comment | 32 complexity | aae433e9b7270ec8a2c30abebb341a5c MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0
  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 versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.console.libs
  19. * @since CakePHP(tm) v 1.2.0.5012
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * API shell to show method signatures of CakePHP core classes.
  24. *
  25. * @package cake
  26. * @subpackage cake.cake.console.libs
  27. */
  28. class ApiShell extends Shell {
  29. /**
  30. * Map between short name for paths and real paths.
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. var $paths = array();
  36. /**
  37. * Override intialize of the Shell
  38. *
  39. * @access public
  40. */
  41. function initialize() {
  42. $this->paths = array_merge($this->paths, array(
  43. 'behavior' => LIBS . 'model' . DS . 'behaviors' . DS,
  44. 'cache' => LIBS . 'cache' . DS,
  45. 'controller' => LIBS . 'controller' . DS,
  46. 'component' => LIBS . 'controller' . DS . 'components' . DS,
  47. 'helper' => LIBS . 'view' . DS . 'helpers' . DS,
  48. 'model' => LIBS . 'model' . DS,
  49. 'view' => LIBS . 'view' . DS,
  50. 'core' => LIBS
  51. ));
  52. }
  53. /**
  54. * Override main() to handle action
  55. *
  56. * @access public
  57. */
  58. function main() {
  59. if (empty($this->args)) {
  60. return $this->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($file);
  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->err(sprintf(__("%s not found", true), $class));
  84. $this->_stop();
  85. }
  86. $parsed = $this->__parseClass($path . $file .'.php');
  87. if (!empty($parsed)) {
  88. if (isset($this->params['m'])) {
  89. if (!isset($parsed[$this->params['m']])) {
  90. $this->err(sprintf(__("%s::%s() could not be found", true), $class, $this->params['m']));
  91. $this->_stop();
  92. }
  93. $method = $parsed[$this->params['m']];
  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(__('Select a number to see the more information about a specific method. q to quit. l to list.', true), null, 'q'))) {
  107. if ($number === 'q') {
  108. $this->out(__('Done', true));
  109. $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. * Show help for this shell.
  127. *
  128. * @access public
  129. */
  130. function help() {
  131. $head = "Usage: cake api [<type>] <className> [-m <method>]\n";
  132. $head .= "-----------------------------------------------\n";
  133. $head .= "Parameters:\n\n";
  134. $commands = array(
  135. 'path' => "\t<type>\n" .
  136. "\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
  137. "\t\tAvailable values:\n\n".
  138. "\t\tbehavior\tLook for class in CakePHP behavior path\n".
  139. "\t\tcache\tLook for class in CakePHP cache path\n".
  140. "\t\tcontroller\tLook for class in CakePHP controller path\n".
  141. "\t\tcomponent\tLook for class in CakePHP component path\n".
  142. "\t\thelper\tLook for class in CakePHP helper path\n".
  143. "\t\tmodel\tLook for class in CakePHP model path\n".
  144. "\t\tview\tLook for class in CakePHP view path\n",
  145. 'className' => "\t<className>\n" .
  146. "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
  147. );
  148. $this->out($head);
  149. if (!isset($this->args[1])) {
  150. foreach ($commands as $cmd) {
  151. $this->out("{$cmd}\n\n");
  152. }
  153. } elseif (isset($commands[strtolower($this->args[1])])) {
  154. $this->out($commands[strtolower($this->args[1])] . "\n\n");
  155. } else {
  156. $this->out("Command '" . $this->args[1] . "' not found");
  157. }
  158. }
  159. /**
  160. * Parse a given class (located on given file) and get public methods and their
  161. * signatures.
  162. *
  163. * @param object $File File object
  164. * @param string $class Class name
  165. * @return array Methods and signatures indexed by method name
  166. * @access private
  167. */
  168. function __parseClass($path) {
  169. $parsed = array();
  170. $File = new File($path);
  171. if (!$File->exists()) {
  172. $this->err(sprintf(__("%s could not be found", true), $File->name));
  173. $this->_stop();
  174. }
  175. $contents = $File->read();
  176. if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.*\\))%', $contents, $result, PREG_PATTERN_ORDER)) {
  177. foreach ($result[2] as $key => $method) {
  178. $method = str_replace('function ', '', trim($method));
  179. if (strpos($method, '__') === false && $method[0] != '_') {
  180. $parsed[$method] = array(
  181. 'comment' => str_replace(array('/*', '*/', '*'), '', trim($result[1][$key])),
  182. 'method' => $method,
  183. 'parameters' => trim($result[3][$key])
  184. );
  185. }
  186. }
  187. }
  188. ksort($parsed);
  189. return $parsed;
  190. }
  191. }