OSDN Git Service

make load_command() and load_all_commands() standalone utilities
authorscribu <mail@scribu.net>
Sun, 26 May 2013 02:22:23 +0000 (05:22 +0300)
committerscribu <mail@scribu.net>
Fri, 31 May 2013 18:41:52 +0000 (21:41 +0300)
php/WP_CLI/Dispatcher/RootCommand.php
php/utils.php

index 4c74f82..e47721a 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace WP_CLI\Dispatcher;
 
+use \WP_CLI\Utils;
+
 class RootCommand extends AbstractCommandContainer implements Documentable {
 
        function get_name() {
@@ -93,54 +95,19 @@ EOB
                if ( isset( $aliases[ $command ] ) )
                        $command = $aliases[ $command ];
 
-               return $this->load_command( $command );
-       }
-
-       function get_subcommands() {
-               $this->load_all_commands();
-
-               return parent::get_subcommands();
-       }
-
-       protected function load_all_commands() {
-               $cmd_dir = WP_CLI_ROOT . "commands";
+               Utils\load_command( $command );
 
-               $iterator = new \DirectoryIterator( $cmd_dir );
-
-               foreach ( $iterator as $filename ) {
-                       if ( '.php' != substr( $filename, -4 ) )
-                               continue;
-
-                       $command = substr( $filename, 0, -4 );
-
-                       if ( isset( $this->subcommands[ $command ] ) )
-                               continue;
-
-                       include "$cmd_dir/$filename";
-               }
-       }
-
-       protected static function get_command_file( $command ) {
-               $path = WP_CLI_ROOT . "/commands/$command.php";
-
-               if ( !is_readable( $path ) ) {
+               if ( !isset( $this->subcommands[ $command ] ) ) {
                        return false;
                }
 
-               return $path;
+               return $this->subcommands[ $command ];
        }
 
-       protected function load_command( $command ) {
-               if ( !isset( $this->subcommands[ $command ] ) ) {
-                       if ( $path = self::get_command_file( $command ) )
-                               include $path;
-               }
-
-               if ( !isset( $this->subcommands[ $command ] ) ) {
-                       return false;
-               }
+       function get_subcommands() {
+               Utils\load_all_commands();
 
-               return $this->subcommands[ $command ];
+               return parent::get_subcommands();
        }
 }
 
index e9cad0b..5c4da77 100644 (file)
@@ -30,6 +30,27 @@ function load_dependencies() {
        include WP_CLI_ROOT . 'Spyc.php';
 }
 
+function load_command( $name ) {
+       $path = WP_CLI_ROOT . "/commands/$name.php";
+
+       if ( is_readable( $path ) ) {
+               include_once $path;
+       }
+}
+
+function load_all_commands() {
+       $cmd_dir = WP_CLI_ROOT . "commands";
+
+       $iterator = new \DirectoryIterator( $cmd_dir );
+
+       foreach ( $iterator as $filename ) {
+               if ( '.php' != substr( $filename, -4 ) )
+                       continue;
+
+               include_once "$cmd_dir/$filename";
+       }
+}
+
 function get_config_spec() {
        $spec = include __DIR__ . '/config-spec.php';