From 87a91609259700d243d0e5a49f852fd5eec4ba09 Mon Sep 17 00:00:00 2001 From: scribu Date: Sun, 10 Nov 2013 00:41:55 +0200 Subject: [PATCH] convert Fetcher to abstract class --- php/WP_CLI/Fetcher.php | 55 ++++++++++++++++++++++++++++++++++++++------ php/WP_CLI/FetcherPlugin.php | 39 ++++--------------------------- php/WP_CLI/FetcherUser.php | 27 +--------------------- php/commands/plugin.php | 8 ++++--- php/commands/user.php | 2 +- 5 files changed, 60 insertions(+), 71 deletions(-) diff --git a/php/WP_CLI/Fetcher.php b/php/WP_CLI/Fetcher.php index 58cf01f9..bf244651 100644 --- a/php/WP_CLI/Fetcher.php +++ b/php/WP_CLI/Fetcher.php @@ -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; + } } diff --git a/php/WP_CLI/FetcherPlugin.php b/php/WP_CLI/FetcherPlugin.php index 03c65690..3c8893a6 100644 --- a/php/WP_CLI/FetcherPlugin.php +++ b/php/WP_CLI/FetcherPlugin.php @@ -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 + ); } } diff --git a/php/WP_CLI/FetcherUser.php b/php/WP_CLI/FetcherUser.php index 1ed29bfc..aea6ffbe 100644 --- a/php/WP_CLI/FetcherUser.php +++ b/php/WP_CLI/FetcherUser.php @@ -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; - } } diff --git a/php/commands/plugin.php b/php/commands/plugin.php index 948fa054..ab0bd6be 100644 --- a/php/commands/plugin.php +++ b/php/commands/plugin.php @@ -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 ); diff --git a/php/commands/user.php b/php/commands/user.php index cf18fcb0..df0c65e0 100644 --- a/php/commands/user.php +++ b/php/commands/user.php @@ -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'" ); } /** -- 2.11.0