api.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: api_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * API shell to get CakePHP core method signatures.
00005  *
00006  * Implementation of a Cake Shell to show CakePHP core method signatures.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.console.libs
00023  * @since           CakePHP(tm) v 1.2.0.5012
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 
00030 /**
00031  * API shell to show method signatures of CakePHP core classes.
00032  *
00033  * @package     cake
00034  * @subpackage  cake.cake.console.libs
00035  */
00036 class ApiShell extends Shell {
00037 /**
00038  * Map between short name for paths and real paths.
00039  *
00040  * @var array
00041  * @access public
00042  */
00043     var $paths = array();
00044 /**
00045  * Override intialize of the Shell
00046  *
00047  * @access public
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  * Override main() to handle action
00063  *
00064  * @access public
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  * Show help for this shell.
00146  *
00147  * @access public
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  * Parse a given class (located on given file) and get public methods and their
00183  * signatures.
00184  *
00185  * @param object $File File object
00186  * @param string $class Class name
00187  * @return array Methods and signatures indexed by method name
00188  * @access private
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 ?>