OSDN Git Service

make WP_CLI::add_command() work when called from a Composer package's autoloaded...
authorscribu <mail@scribu.net>
Sun, 30 Jun 2013 20:53:41 +0000 (22:53 +0200)
committerscribu <mail@scribu.net>
Sun, 30 Jun 2013 20:54:34 +0000 (22:54 +0200)
php/class-wp-cli.php

index 9a7ac59..f8d1f36 100644 (file)
@@ -15,6 +15,7 @@ class WP_CLI {
        private static $logger;
 
        private static $hooks = array(), $hooks_passed = array();
+       private static $commands_in_packages = array();
 
        private static $man_dirs = array();
 
@@ -27,6 +28,10 @@ class WP_CLI {
                self::$configurator = new WP_CLI\Configurator( WP_CLI_ROOT . '/php/config-spec.php' );
                self::$root = new Dispatcher\RootCommand;
                self::$runner = new WP_CLI\Runner;
+
+               foreach ( self::$commands_in_packages as $args ) {
+                       call_user_func_array( array( __CLASS__, 'add_command' ), $args );
+               }
        }
 
        /**
@@ -73,6 +78,14 @@ class WP_CLI {
         *   'before_invoke' => callback to execute before invoking the command
         */
        static function add_command( $name, $class, $args = array() ) {
+               if ( !self::$root ) {  // Still loading Composer autoloader
+                       self::$commands_in_packages[] = func_get_args();
+               } else {
+                       self::_add_command( $name, $class, $args );
+               }
+       }
+
+       private static function _add_command( $name, $class, $args ) {
                $command = Dispatcher\CommandFactory::create( $name, $class, self::$root );
 
                if ( isset( $args['before_invoke'] ) ) {