2008-05-30 11:40:08 +00:00
< ? php
/**
* API shell to get CakePHP core method signatures .
*
* Implementation of a Cake Shell to show CakePHP core method signatures .
*
2010-10-03 16:38:58 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 11:59:49 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2008-05-30 11:40:08 +00:00
* Redistributions of files must retain the above copyright notice .
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.5012
2013-05-30 22:11:14 +00:00
* @ license http :// www . opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2011-11-25 21:05:00 +00:00
App :: uses ( 'AppShell' , 'Console/Command' );
2010-12-09 03:45:18 +00:00
App :: uses ( 'File' , 'Utility' );
2008-05-30 11:40:08 +00:00
/**
* API shell to show method signatures of CakePHP core classes .
*
2011-12-08 15:35:02 +00:00
* Implementation of a Cake Shell to show CakePHP core method signatures .
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Console . Command
2008-05-30 11:40:08 +00:00
*/
2011-11-25 21:05:00 +00:00
class ApiShell extends AppShell {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Map between short name for paths and real paths .
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $paths = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2011-04-25 19:17:59 +00:00
* Override initialize of the Shell
2008-05-30 11:40:08 +00:00
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function initialize () {
2008-05-30 11:40:08 +00:00
$this -> paths = array_merge ( $this -> paths , array (
2011-04-17 10:35:21 +00:00
'behavior' => CAKE . 'Model' . DS . 'Behavior' . DS ,
'cache' => CAKE . 'Cache' . DS ,
'controller' => CAKE . 'Controller' . DS ,
'component' => CAKE . 'Controller' . DS . 'Component' . DS ,
'helper' => CAKE . 'View' . DS . 'Helper' . DS ,
'model' => CAKE . 'Model' . DS ,
'view' => CAKE . 'View' . DS ,
'core' => CAKE
2008-05-30 11:40:08 +00:00
));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Override main () to handle action
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function main () {
2008-05-30 11:40:08 +00:00
if ( empty ( $this -> args )) {
2010-10-10 04:41:10 +00:00
return $this -> out ( $this -> OptionParser -> help ());
2008-05-30 11:40:08 +00:00
}
2009-07-23 21:04:40 +00:00
$type = strtolower ( $this -> args [ 0 ]);
2008-05-30 11:40:08 +00:00
if ( isset ( $this -> paths [ $type ])) {
$path = $this -> paths [ $type ];
} else {
$path = $this -> paths [ 'core' ];
}
2012-09-14 18:26:00 +00:00
$count = count ( $this -> args );
if ( $count > 1 ) {
2008-05-30 11:40:08 +00:00
$file = Inflector :: underscore ( $this -> args [ 1 ]);
2011-03-08 20:07:15 +00:00
$class = Inflector :: camelize ( $this -> args [ 1 ]);
2012-09-14 18:57:51 +00:00
} elseif ( $count ) {
2012-09-14 18:26:00 +00:00
$file = $type ;
$class = Inflector :: camelize ( $type );
2008-05-30 11:40:08 +00:00
}
2009-06-10 23:13:39 +00:00
$objects = App :: objects ( 'class' , $path );
2008-05-30 11:40:08 +00:00
if ( in_array ( $class , $objects )) {
if ( in_array ( $type , array ( 'behavior' , 'component' , 'helper' )) && $type !== $file ) {
if ( ! preg_match ( '/' . Inflector :: camelize ( $type ) . '$/' , $class )) {
$class .= Inflector :: camelize ( $type );
}
}
} else {
2011-03-19 17:32:35 +00:00
$this -> error ( __d ( 'cake_console' , '%s not found' , $class ));
2008-05-30 11:40:08 +00:00
}
2012-03-03 23:55:07 +00:00
$parsed = $this -> _parseClass ( $path . $class . '.php' , $class );
2008-05-30 11:40:08 +00:00
if ( ! empty ( $parsed )) {
2010-10-10 04:41:10 +00:00
if ( isset ( $this -> params [ 'method' ])) {
if ( ! isset ( $parsed [ $this -> params [ 'method' ]])) {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , '%s::%s() could not be found' , $class , $this -> params [ 'method' ]));
2013-07-01 12:15:54 +00:00
return $this -> _stop ();
2008-05-30 11:40:08 +00:00
}
2010-10-10 04:41:10 +00:00
$method = $parsed [ $this -> params [ 'method' ]];
2011-12-16 06:52:07 +00:00
$this -> out ( $class . '::' . $method [ 'method' ] . $method [ 'parameters' ]);
2008-05-30 11:40:08 +00:00
$this -> hr ();
$this -> out ( $method [ 'comment' ], true );
} else {
$this -> out ( ucwords ( $class ));
$this -> hr ();
$i = 0 ;
foreach ( $parsed as $method ) {
$list [] = ++ $i . " . " . $method [ 'method' ] . $method [ 'parameters' ];
}
$this -> out ( $list );
$methods = array_keys ( $parsed );
2011-03-19 17:32:35 +00:00
while ( $number = strtolower ( $this -> in ( __d ( 'cake_console' , 'Select a number to see the more information about a specific method. q to quit. l to list.' ), null , 'q' ))) {
2008-05-30 11:40:08 +00:00
if ( $number === 'q' ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Done' ));
2010-05-23 02:09:07 +00:00
return $this -> _stop ();
2008-05-30 11:40:08 +00:00
}
if ( $number === 'l' ) {
$this -> out ( $list );
}
if ( isset ( $methods [ -- $number ])) {
$method = $parsed [ $methods [ $number ]];
$this -> hr ();
2011-12-16 06:52:07 +00:00
$this -> out ( $class . '::' . $method [ 'method' ] . $method [ 'parameters' ]);
2008-05-30 11:40:08 +00:00
$this -> hr ();
$this -> out ( $method [ 'comment' ], true );
}
}
}
}
}
2010-10-10 04:41:10 +00:00
/**
* Get and configure the optionparser .
*
* @ return ConsoleOptionParser
*/
2010-10-10 05:56:23 +00:00
public function getOptionParser () {
$parser = parent :: getOptionParser ();
2010-10-10 04:41:10 +00:00
$parser -> addArgument ( 'type' , array (
2011-04-25 19:17:59 +00:00
'help' => __d ( 'cake_console' , 'Either a full path or type of class (model, behavior, controller, component, view, helper)' )
2010-10-10 04:41:10 +00:00
)) -> addArgument ( 'className' , array (
2011-04-25 19:17:59 +00:00
'help' => __d ( 'cake_console' , 'A CakePHP core class name (e.g: Component, HtmlHelper).' )
2010-10-10 04:41:10 +00:00
)) -> addOption ( 'method' , array (
'short' => 'm' ,
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'The specific method you want help on.' )
)) -> description ( __d ( 'cake_console' , 'Lookup doc block comments for classes in CakePHP.' ));
2010-10-10 04:41:10 +00:00
return $parser ;
}
2011-12-06 20:52:48 +00:00
2008-05-30 11:40:08 +00:00
/**
* Show help for this shell .
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function help () {
2012-11-21 14:39:03 +00:00
$head = " Usage: cake api [<type>] <className> [-m <method>] \n " ;
2008-05-30 11:40:08 +00:00
$head .= " ----------------------------------------------- \n " ;
$head .= " Parameters: \n \n " ;
$commands = array (
'path' => " \t <type> \n " .
2012-03-03 23:55:07 +00:00
" \t \t Either a full path or type of class (model, behavior, controller, component, view, helper). \n " .
" \t \t Available values: \n \n " .
" \t \t behavior \t Look for class in CakePHP behavior path \n " .
" \t \t cache \t Look for class in CakePHP cache path \n " .
" \t \t controller \t Look for class in CakePHP controller path \n " .
" \t \t component \t Look for class in CakePHP component path \n " .
" \t \t helper \t Look for class in CakePHP helper path \n " .
" \t \t model \t Look for class in CakePHP model path \n " .
2009-06-25 13:34:28 +00:00
" \t \t view \t Look for class in CakePHP view path \n " ,
2008-05-30 11:40:08 +00:00
'className' => " \t <className> \n " .
2009-06-25 13:34:28 +00:00
" \t \t A CakePHP core class name (e.g: Component, HtmlHelper). \n "
2008-05-30 11:40:08 +00:00
);
$this -> out ( $head );
if ( ! isset ( $this -> args [ 1 ])) {
foreach ( $commands as $cmd ) {
$this -> out ( " { $cmd } \n \n " );
}
2009-08-28 02:46:00 +00:00
} elseif ( isset ( $commands [ strtolower ( $this -> args [ 1 ])])) {
$this -> out ( $commands [ strtolower ( $this -> args [ 1 ])] . " \n \n " );
2008-05-30 11:40:08 +00:00
} else {
2011-04-25 19:17:59 +00:00
$this -> out ( __d ( 'cake_console' , 'Command %s not found' , $this -> args [ 1 ]));
2008-05-30 11:40:08 +00:00
}
}
/**
* Parse a given class ( located on given file ) and get public methods and their
* signatures .
*
2011-07-29 02:03:44 +00:00
* @ param string $path File path
2008-05-30 11:40:08 +00:00
* @ param string $class Class name
* @ return array Methods and signatures indexed by method name
*/
2011-08-20 04:43:34 +00:00
protected function _parseClass ( $path , $class ) {
2008-05-30 11:40:08 +00:00
$parsed = array ();
2011-03-08 20:07:15 +00:00
if ( ! class_exists ( $class )) {
2012-03-03 23:55:07 +00:00
if ( ! include_once $path ) {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , '%s could not be found' , $path ));
2011-03-08 20:07:15 +00:00
}
2008-05-30 11:40:08 +00:00
}
2011-03-08 20:07:15 +00:00
2010-05-23 02:09:07 +00:00
$reflection = new ReflectionClass ( $class );
2008-05-30 11:40:08 +00:00
2010-05-23 02:09:07 +00:00
foreach ( $reflection -> getMethods () as $method ) {
if ( ! $method -> isPublic () || strpos ( $method -> getName (), '_' ) === 0 ) {
continue ;
}
if ( $method -> getDeclaringClass () -> getName () != $class ) {
continue ;
}
$args = array ();
foreach ( $method -> getParameters () as $param ) {
$paramString = '$' . $param -> getName ();
if ( $param -> isDefaultValueAvailable ()) {
$paramString .= ' = ' . str_replace ( " \n " , '' , var_export ( $param -> getDefaultValue (), true ));
2008-05-30 11:40:08 +00:00
}
2010-05-23 02:09:07 +00:00
$args [] = $paramString ;
2008-05-30 11:40:08 +00:00
}
2010-05-23 02:09:07 +00:00
$parsed [ $method -> getName ()] = array (
'comment' => str_replace ( array ( '/*' , '*/' , '*' ), '' , $method -> getDocComment ()),
'method' => $method -> getName (),
'parameters' => '(' . implode ( ', ' , $args ) . ')'
);
2008-05-30 11:40:08 +00:00
}
ksort ( $parsed );
return $parsed ;
}
2012-03-03 23:55:07 +00:00
2008-05-30 11:40:08 +00:00
}