+++ /dev/null
-<?php
-
-namespace WP_CLI\Dispatcher;
-
-class MethodSubcommand extends Subcommand {
-
- function __construct( CommandContainer $parent, $class, \ReflectionMethod $method ) {
- $docparser = new \WP_CLI\DocParser( $method );
-
- $name = $docparser->get_tag( 'subcommand' );
- if ( !$name )
- $name = $method->name;
-
- $callable = new CallableMethod( $class, $method->name );
-
- parent::__construct( $parent, $name, $callable, $docparser );
- }
-
- function get_alias() {
- return $this->docparser->get_tag( 'alias' );
- }
-}
-
class Subcommand implements Command, AtomicCommand, Documentable {
- function __construct( CommandContainer $parent, $name, $callable, $docparser ) {
+ private $parent, $name, $method, $docparser;
+
+ function __construct( CommandContainer $parent, \ReflectionMethod $method, $name = false ) {
+ $docparser = new \WP_CLI\DocParser( $method );
+
+ if ( !$name )
+ $name = $docparser->get_tag( 'subcommand' );
+
+ if ( !$name )
+ $name = $method->name;
+
$this->parent = $parent;
$this->name = $name;
- $this->callable = $callable;
-
+ $this->method = $method;
$this->docparser = $docparser;
}
+ function get_alias() {
+ return $this->docparser->get_tag( 'alias' );
+ }
+
function show_usage( $prefix = 'usage: ' ) {
\WP_CLI::line( $prefix . $this->get_full_synopsis() );
}
array( $this, 'show_usage' ) );
}
- call_user_func( $this->callable, $args, $assoc_args );
+ $instance = new $this->method->class;
+
+ call_user_func( array( $instance, $this->method->name ), $args, $assoc_args );
}
function get_name() {
$reflection = new \ReflectionClass( $class );
if ( $reflection->hasMethod( '__invoke' ) ) {
- $command = new Dispatcher\MethodSubcommand( self::$root, $reflection->name, $reflection->getMethod( '__invoke' ) );
+ $command = new Dispatcher\Subcommand( self::$root,
+ $reflection->getMethod( '__invoke' ), $name );
} else {
$command = self::create_composite_command( $name, $reflection );
}
if ( !self::_is_good_method( $method ) )
continue;
- $subcommand = new Dispatcher\MethodSubcommand( $container, $reflection->name, $method );
+ $subcommand = new Dispatcher\Subcommand( $container, $method );
$subcommand_name = $subcommand->get_name();
$full_name = self::get_full_name( $subcommand );
function get_full_synopsis();
}
-
-class CallableMethod {
-
- function __construct( $class, $method ) {
- $this->class = $class;
- $this->method = $method;
- }
-
- function __invoke( $args, $assoc_args ) {
- $instance = new $this->class;
-
- call_user_func( array( $instance, $this->method ), $args, $assoc_args );
- }
-}
-