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