OSDN Git Service

move utilities for validating plugin names to FetcherPlugin class
authorscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:15:47 +0000 (00:15 +0200)
committerscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:15:47 +0000 (00:15 +0200)
php/WP_CLI/Fetcher.php
php/WP_CLI/FetcherPlugin.php [new file with mode: 0644]
php/commands/plugin.php

index f1de345..58cf01f 100644 (file)
@@ -4,13 +4,13 @@ namespace WP_CLI;
 
 interface Fetcher {
 
-       // Returns the object if found; otherwise returns false
+       // Returns the item if found; otherwise returns false
        function get( $id );
 
-       // Returns the object if found; otherwise calls WP_CLI::error()
+       // Returns the item if found; otherwise calls WP_CLI::error()
        function get_check( $id );
 
-       // Returns the list of found objects
+       // Returns the list of found items
        function get_many( $ids );
 }
 
diff --git a/php/WP_CLI/FetcherPlugin.php b/php/WP_CLI/FetcherPlugin.php
new file mode 100644 (file)
index 0000000..03c6569
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace WP_CLI;
+
+class FetcherPlugin implements Fetcher {
+
+       /**
+        * @param string $name The plugin slug
+        * @return string|false The plugin filename
+        */
+       public function get( $name ) {
+               $plugins = get_plugins( '/' . $name );
+
+               if ( !empty( $plugins ) ) {
+                       $file = $name . '/' . key( $plugins );
+               } else {
+                       $file = $name . '.php';
+
+                       $plugins = get_plugins();
+
+                       if ( !isset( $plugins[$file] ) ) {
+                               return false;
+                       }
+               }
+
+               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;
+       }
+}
+
index a9a2e83..948fa05 100644 (file)
@@ -23,6 +23,8 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                require_once ABSPATH.'wp-admin/includes/plugin-install.php';
 
                parent::__construct();
+
+               $this->fetcher = new \WP_CLI\FetcherPlugin;
        }
 
        protected function get_upgrader_class( $force ) {
@@ -87,23 +89,19 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function status_single( $args ) {
-               $plugins = $this->validate_plugin_names( $args );
-               if ( empty( $plugins ) )
-                       exit(1);
-
-               list( $plugin ) = $plugins;
+               $file = $this->fetcher->get_check( $args[0] );
 
-               $details = $this->get_details( $plugin );
+               $details = $this->get_details( $file );
 
-               $status = $this->format_status( $this->get_status( $plugin->file ), 'long' );
+               $status = $this->format_status( $this->get_status( $file ), 'long' );
 
                $version = $details['Version'];
 
-               if ( $this->has_update( $plugin->file ) )
+               if ( $this->has_update( $file ) )
                        $version .= ' (%gUpdate available%n)';
 
                echo WP_CLI::colorize( \WP_CLI\Utils\mustache_render( 'plugin-status.mustache', array(
-                       'slug' => $plugin->name,
+                       'slug' => $this->get_name( $file ),
                        'status' => $status,
                        'version' => $version,
                        'name' => $details['Name'],
@@ -140,7 +138,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function activate( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+               foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                        activate_plugin( $plugin->file, '', $network_wide );
 
                        if ( $this->check_active( $plugin->file, $network_wide ) ) {
@@ -168,7 +166,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function deactivate( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+               foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                        deactivate_plugins( $plugin->file, false, $network_wide );
 
                        if ( ! $this->check_active( $plugin->file, $network_wide ) ) {
@@ -196,7 +194,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function toggle( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+               foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                        if ( $this->check_active( $plugin->file, $network_wide ) ) {
                                $this->deactivate( array( $plugin->name ), $assoc_args );
                        } else {
@@ -226,7 +224,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                $path = untrailingslashit( WP_PLUGIN_DIR );
 
                if ( !empty( $args ) ) {
-                       $plugins = $this->validate_plugin_names( $args );
+                       $plugins = $this->fetcher->get_many( $args );
                        if ( empty( $plugins ) )
                                return;
 
@@ -294,7 +292,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         */
        function update( $args, $assoc_args ) {
                if ( isset( $assoc_args['version'] ) && 'dev' == $assoc_args['version'] ) {
-                       foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                                $this->_delete( $plugin );
                                $this->install( array( $plugin->name ), $assoc_args );
                        }
@@ -324,7 +322,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function filter_item_list( $items, $args ) {
-               $basenames = wp_list_pluck( $this->validate_plugin_names( $args ), 'file' );
+               $basenames = wp_list_pluck( $this->fetcher->get_many( $args ), 'file' );
                return \WP_CLI\Utils\pick_fields( $items, $basenames );
        }
 
@@ -384,10 +382,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp plugin get bbpress --format=json
         */
        public function get( $args, $assoc_args ) {
-               $file = $this->_parse_name( $args[0] );
-               if ( !$file ) {
-                       WP_CLI::error( "The '{$args[0]}' plugin could not be found." );
-               }
+               $file = $this->get_check( $args[0] );
 
                $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
 
@@ -422,7 +417,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp plugin uninstall hello
         */
        function uninstall( $args, $assoc_args = array() ) {
-               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+               foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                        if ( is_plugin_active( $plugin->file ) ) {
                                WP_CLI::warning( "The '{$plugin->name}' plugin is active." );
                                continue;
@@ -451,7 +446,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @subcommand is-installed
         */
        function is_installed( $args, $assoc_args = array() ) {
-               if ( $this->_parse_name( $args[0] ) ) {
+               if ( $this->fetcher->get( $args[0] ) ) {
                        exit( 0 );
                } else {
                        exit( 1 );
@@ -471,7 +466,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp plugin delete hello
         */
        function delete( $args, $assoc_args = array() ) {
-               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+               foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
                        if ( $this->_delete( $plugin ) ) {
                                WP_CLI::success( "Deleted '{$plugin->name}' plugin." );
                        }
@@ -526,9 +521,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @param object
         * @return array
         */
-       private function get_details( $plugin ) {
-               $file = $plugin->file;
-
+       private function get_details( $file ) {
                $plugin_folder = get_plugins(  '/' . plugin_basename( dirname( $file ) ) );
                $plugin_file = basename( $file );
 
@@ -536,49 +529,6 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        /**
-        * Parse the name of a plugin to a filename; check if it exists.
-        *
-        * @param string name
-        * @return string
-        */
-       private function _parse_name( $name ) {
-               $plugins = get_plugins( '/' . $name );
-
-               if ( !empty( $plugins ) ) {
-                       $file = $name . '/' . key( $plugins );
-               }
-               else {
-                       $file = $name . '.php';
-
-                       $plugins = get_plugins();
-
-                       if ( !isset( $plugins[$file] ) ) {
-                               return false;
-                       }
-               }
-
-               return $file;
-       }
-
-       private function validate_plugin_names( $args ) {
-               $plugins = array();
-
-               foreach ( $args as $name ) {
-                       $file = $this->_parse_name( $name );
-                       if ( $file ) {
-                               $plugins[] = (object) array(
-                                       'name' => $name,
-                                       'file' => $file
-                               );
-                       } else {
-                               WP_CLI::warning( "The '$name' plugin could not be found." );
-                       }
-               }
-
-               return $plugins;
-       }
-
-       /**
         * Converts a plugin basename back into a friendly slug.
         */
        private function get_name( $file ) {