00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class ApiShell extends Shell {
00037
00038
00039
00040
00041
00042
00043 var $paths = array();
00044
00045
00046
00047
00048
00049 function initialize () {
00050 $this->paths = array_merge($this->paths, array(
00051 'behavior' => LIBS . 'model' . DS . 'behaviors' . DS,
00052 'cache' => LIBS . 'cache' . DS,
00053 'controller' => LIBS . 'controller' . DS,
00054 'component' => LIBS . 'controller' . DS . 'components' . DS,
00055 'helper' => LIBS . 'view' . DS . 'helpers' . DS,
00056 'model' => LIBS . 'model' . DS,
00057 'view' => LIBS . 'view' . DS,
00058 'core' => LIBS
00059 ));
00060 }
00061
00062
00063
00064
00065
00066 function main() {
00067 if (empty($this->args)) {
00068 return $this->help();
00069 }
00070
00071 $type = low($this->args[0]);
00072
00073 if (isset($this->paths[$type])) {
00074 $path = $this->paths[$type];
00075 } else {
00076 $path = $this->paths['core'];
00077 }
00078
00079 if (count($this->args) == 1) {
00080 $file = $type;
00081 $class = Inflector::camelize($type);
00082 } elseif (count($this->args) > 1) {
00083 $file = Inflector::underscore($this->args[1]);
00084 $class = Inflector::camelize($file);
00085 }
00086
00087 $objects = Configure::listObjects('class', $path);
00088 if (in_array($class, $objects)) {
00089 if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) {
00090 if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) {
00091 $class .= Inflector::camelize($type);
00092 }
00093 }
00094
00095 } else {
00096 $this->err(sprintf(__("%s not found", true), $class));
00097 $this->_stop();
00098 }
00099
00100 $parsed = $this->__parseClass($path . $file .'.php');
00101
00102 if (!empty($parsed)) {
00103 if (isset($this->params['m'])) {
00104 if (!isset($parsed[$this->params['m']])) {
00105 $this->err(sprintf(__("%s::%s() could not be found", true), $class, $this->params['m']));
00106 $this->_stop();
00107 }
00108 $method = $parsed[$this->params['m']];
00109 $this->out($class .'::'.$method['method'] . $method['parameters']);
00110 $this->hr();
00111 $this->out($method['comment'], true);
00112 } else {
00113 $this->out(ucwords($class));
00114 $this->hr();
00115 $i = 0;
00116 foreach ($parsed as $method) {
00117 $list[] = ++$i . ". " . $method['method'] . $method['parameters'];
00118 }
00119 $this->out($list);
00120
00121 $methods = array_keys($parsed);
00122 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')) {
00123 if ($number === 'q') {
00124 $this->out(__('Done', true));
00125 $this->_stop();
00126 }
00127
00128 if ($number === 'l') {
00129 $this->out($list);
00130 }
00131
00132 if (isset($methods[--$number])) {
00133 $method = $parsed[$methods[$number]];
00134 $this->hr();
00135 $this->out($class .'::'.$method['method'] . $method['parameters']);
00136 $this->hr();
00137 $this->out($method['comment'], true);
00138 }
00139 }
00140 }
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149 function help() {
00150 $head = "Usage: cake api [<type>] <className> [-m <method>]\n";
00151 $head .= "-----------------------------------------------\n";
00152 $head .= "Parameters:\n\n";
00153
00154 $commands = array(
00155 'path' => "\t<type>\n" .
00156 "\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
00157 "\t\tAvailable values:\n\n".
00158 "\t\tbehavior\tLook for class in CakePHP behavior path\n".
00159 "\t\tcache\tLook for class in CakePHP cache path\n".
00160 "\t\tcontroller\tLook for class in CakePHP controller path\n".
00161 "\t\tcomponent\tLook for class in CakePHP component path\n".
00162 "\t\thelper\tLook for class in CakePHP helper path\n".
00163 "\t\tmodel\tLook for class in CakePHP model path\n".
00164 "\t\tview\tLook for class in CakePHP view path\n",
00165 'className' => "\t<className>\n" .
00166 "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
00167 );
00168
00169 $this->out($head);
00170 if (!isset($this->args[1])) {
00171 foreach ($commands as $cmd) {
00172 $this->out("{$cmd}\n\n");
00173 }
00174 } elseif (isset($commands[low($this->args[1])])) {
00175 $this->out($commands[low($this->args[1])] . "\n\n");
00176 } else {
00177 $this->out("Command '" . $this->args[1] . "' not found");
00178 }
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 function __parseClass($path) {
00191 $parsed = array();
00192
00193 $File = new File($path);
00194 if (!$File->exists()) {
00195 $this->err(sprintf(__("%s could not be found", true), $File->name));
00196 $this->_stop();
00197 }
00198
00199 $contents = $File->read();
00200
00201 if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.+\\))%', $contents, $result, PREG_PATTERN_ORDER)) {
00202 foreach($result[2] as $key => $method) {
00203 $method = str_replace('function ', '', trim($method));
00204
00205 if (strpos($method, '__') === false && $method[0] != '_') {
00206 $parsed[$method] = array(
00207 'comment' => r(array('', '*'), '', trim($result[1][$key])),
00208 'method' => $method,
00209 'parameters' => trim($result[3][$key]),
00210 );
00211 }
00212 }
00213 }
00214 ksort($parsed);
00215 return $parsed;
00216 }
00217 }
00218 ?>