OSDN Git Service

convert Fetcher to abstract class
authorscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:41:55 +0000 (00:41 +0200)
committerscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:44:09 +0000 (00:44 +0200)
php/WP_CLI/Fetcher.php
php/WP_CLI/FetcherPlugin.php
php/WP_CLI/FetcherUser.php
php/commands/plugin.php
php/commands/user.php

index 58cf01f..bf24465 100644 (file)
@@ -2,15 +2,56 @@
 
 namespace WP_CLI;
 
-interface Fetcher {
+abstract class Fetcher {
 
-       // Returns the item if found; otherwise returns false
-       function get( $id );
+       protected $msg;
 
-       // Returns the item if found; otherwise calls WP_CLI::error()
-       function get_check( $id );
+       /**
+        * @param string $msg The message to display when an item is not found
+        */
+       function __construct( $msg ) {
+               $this->msg = $msg;
+       }
 
-       // Returns the list of found items
-       function get_many( $ids );
+       /**
+        * @param string $arg The raw CLI argument
+        * @return mixed|false The item if found; false otherwise
+        */
+       abstract public function get( $arg );
+
+       /**
+        * Like get(), but calls WP_CLI::error() instead of returning false.
+        *
+        * @param string $arg The raw CLI argument
+        */
+       public function get_check( $arg ) {
+               $item = $this->get( $arg );
+
+               if ( ! $item ) {
+                       \WP_CLI::error( sprintf( $this->msg, $arg ) );
+               }
+
+               return $item;
+       }
+
+       /**
+        * @param array The raw CLI arguments
+        * @return array The list of found items
+        */
+       public function get_many( $args ) {
+               $items = array();
+
+               foreach ( $args as $arg ) {
+                       $item = $this->get( $arg );
+
+                       if ( $item ) {
+                               $items[] = $item;
+                       } else {
+                               \WP_CLI::warning( sprintf( $this->msg, $arg ) );
+                       }
+               }
+
+               return $items;
+       }
 }
 
index 03c6569..3c8893a 100644 (file)
@@ -2,12 +2,8 @@
 
 namespace WP_CLI;
 
-class FetcherPlugin implements Fetcher {
+class FetcherPlugin extends Fetcher {
 
-       /**
-        * @param string $name The plugin slug
-        * @return string|false The plugin filename
-        */
        public function get( $name ) {
                $plugins = get_plugins( '/' . $name );
 
@@ -23,35 +19,10 @@ class FetcherPlugin implements Fetcher {
                        }
                }
 
-               return $file;
-       }
-
-       public function get_check( $name ) {
-               $file = $this->get( $name );
-
-               if ( ! $file ) {
-                       \WP_CLI::error( "The '$name' plugin could not be found." );
-               }
-
-               return $file;
-       }
-
-       public function get_many( $args ) {
-               $plugins = array();
-
-               foreach ( $args as $name ) {
-                       $file = $this->get( $name );
-                       if ( $file ) {
-                               $plugins[] = (object) array(
-                                       'name' => $name,
-                                       'file' => $file
-                               );
-                       } else {
-                               \WP_CLI::warning( "The '$name' plugin could not be found." );
-                       }
-               }
-
-               return $plugins;
+               return (object) array(
+                       'name' => $name,
+                       'file' => $file
+               );
        }
 }
 
index 1ed29bf..aea6ffb 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace WP_CLI;
 
-class FetcherUser implements Fetcher {
+class FetcherUser extends Fetcher {
 
        public function get( $id_or_login ) {
                if ( is_numeric( $id_or_login ) )
@@ -12,30 +12,5 @@ class FetcherUser implements Fetcher {
 
                return $user;
        }
-
-       public function get_check( $id_or_login ) {
-               $user = $this->get( $id_or_login );
-
-               if ( ! $user ) {
-                       \WP_CLI::error( "Invalid user ID or login: $id_or_login" );
-               }
-
-               return $user;
-       }
-
-       public function get_many( $ids_or_logins ) {
-               $users = array();
-
-               foreach ( $ids_or_logins as $id_or_login ) {
-                       $user = $this->get( $id_or_login );
-                       if ( $user ) {
-                               $users[] = $user;
-                       } else {
-                               \WP_CLI::warning( "Invalid user ID or login: $id_or_login" );
-                       }
-               }
-
-               return $users;
-       }
 }
 
index 948fa05..ab0bd6b 100644 (file)
@@ -24,7 +24,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
 
                parent::__construct();
 
-               $this->fetcher = new \WP_CLI\FetcherPlugin;
+               $this->fetcher = new \WP_CLI\FetcherPlugin( "The '%s' plugin could not be found." );
        }
 
        protected function get_upgrader_class( $force ) {
@@ -89,7 +89,8 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function status_single( $args ) {
-               $file = $this->fetcher->get_check( $args[0] );
+               $plugin = $this->fetcher->get_check( $args[0] );
+               $file = $plugin->file;
 
                $details = $this->get_details( $file );
 
@@ -382,7 +383,8 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp plugin get bbpress --format=json
         */
        public function get( $args, $assoc_args ) {
-               $file = $this->get_check( $args[0] );
+               $plugin = $this->fetcher->get_check( $args[0] );
+               $file = $plugin->file;
 
                $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
 
index cf18fcb..df0c65e 100644 (file)
@@ -18,7 +18,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
        );
 
        public function __construct() {
-               $this->fetcher = new \WP_CLI\FetcherUser;
+               $this->fetcher = new \WP_CLI\FetcherUser( "Invalid user ID or login: '%s'" );
        }
 
        /**