api.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: api.php 8253 2009-07-23 21:04:40Z DarkAngelBGE $ */
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. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.console.libs
00021  * @since         CakePHP(tm) v 1.2.0.5012
00022  * @version       $Revision: 8253 $
00023  * @modifiedby    $LastChangedBy: DarkAngelBGE $
00024  * @lastmodified  $Date: 2009-07-23 17:04:40 -0400 (Thu, 23 Jul 2009) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 
00028 /**
00029  * API shell to show method signatures of CakePHP core classes.
00030  *
00031  * @package       cake
00032  * @subpackage    cake.cake.console.libs
00033  */
00034 class ApiShell extends Shell {
00035 /**
00036  * Map between short name for paths and real paths.
00037  *
00038  * @var array
00039  * @access public
00040  */
00041     var $paths = array();
00042 /**
00043  * Override intialize of the Shell
00044  *
00045  * @access public
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  * Override main() to handle action
00061  *
00062  * @access public
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  * Show help for this shell.
00144  *
00145  * @access public
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  * Parse a given class (located on given file) and get public methods and their
00181  * signatures.
00182  *
00183  * @param object $File File object
00184  * @param string $class Class name
00185  * @return array Methods and signatures indexed by method name
00186  * @access private
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 ?>

Generated on Sun Nov 22 00:30:51 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7