OSDN Git Service

extract FetcherTheme class
authorscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:50:14 +0000 (00:50 +0200)
committerscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:50:14 +0000 (00:50 +0200)
php/WP_CLI/FetcherTheme.php [new file with mode: 0644]
php/commands/theme.php

diff --git a/php/WP_CLI/FetcherTheme.php b/php/WP_CLI/FetcherTheme.php
new file mode 100644 (file)
index 0000000..0327763
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace WP_CLI;
+
+class FetcherTheme extends Fetcher {
+
+       public function get( $name ) {
+               $theme = wp_get_theme( $name );
+
+               if ( !$theme->exists() ) {
+                       return false;
+               }
+
+               return $theme;
+       }
+}
+
index b5dccf3..8a8d6dd 100644 (file)
@@ -18,6 +18,12 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
                'version'
        );
 
+       function __construct() {
+               parent::__construct();
+
+               $this->fetcher = new \WP_CLI\FetcherTheme( "The '%s' theme could not be found." );
+       }
+
        protected function get_upgrader_class( $force ) {
                return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : 'Theme_Upgrader';
        }
@@ -76,7 +82,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function status_single( $args ) {
-               $theme = $this->parse_name( $args[0] );
+               $theme = $this->fetcher->get_check( $args[0] );
 
                $status = $this->format_status( $this->get_status( $theme ), 'long' );
 
@@ -110,7 +116,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
         * : The theme to activate.
         */
        public function activate( $args = array() ) {
-               $theme = $this->parse_name( $args[0] );
+               $theme = $this->fetcher->get_check( $args[0] );
 
                switch_theme( $theme->get_template(), $theme->get_stylesheet() );
 
@@ -148,7 +154,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
                if ( empty( $args ) ) {
                        $path = WP_CONTENT_DIR . '/themes';
                } else {
-                       $theme = $this->parse_name( $args[0] );
+                       $theme = $this->fetcher->get_check( $args[0] );
 
                        $path = $theme->get_stylesheet_directory();
 
@@ -208,7 +214,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
        protected function filter_item_list( $items, $args ) {
                $theme_files = array();
                foreach ( $args as $arg ) {
-                       $theme_files[] = $this->parse_name( $arg )->get_stylesheet_directory();
+                       $theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet_directory();
                }
 
                return \WP_CLI\Utils\pick_fields( $items, $theme_files );
@@ -267,7 +273,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp theme get twentytwelve --format=json
         */
        public function get( $args, $assoc_args ) {
-               $theme = $this->parse_name( $args[0] );
+               $theme = $this->fetcher->get_check( $args[0] );
 
                // WP_Theme object employs magic getter, unfortunately
                $theme_vars = array( 'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet', 'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
@@ -348,7 +354,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
         *     wp theme delete twentyeleven
         */
        function delete( $args ) {
-               foreach ( $this->validate_theme_names( $args ) as $theme ) {
+               foreach ( $this->fetcher->get_many( $args ) as $theme ) {
                        $theme_slug = $theme->get_stylesheet();
 
                        if ( $this->is_active_theme( $theme ) ) {
@@ -388,39 +394,6 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
        function _list( $_, $assoc_args ) {
                parent::_list( $_, $assoc_args );
        }
-
-       /**
-        * Parse the name of a plugin to a filename; check if it exists.
-        *
-        * @param string name
-        * @return object
-        */
-       private function parse_name( $name ) {
-               $theme = wp_get_theme( $name );
-
-               if ( !$theme->exists() ) {
-                       WP_CLI::error( "The theme '$name' could not be found." );
-                       exit;
-               }
-
-               return $theme;
-       }
-
-       private function validate_theme_names( $args ) {
-               $themes = array();
-
-               foreach ( $args as $name ) {
-                       $theme = wp_get_theme( $name );
-
-                       if ( !$theme->exists() ) {
-                               WP_CLI::warning( "The '$name' theme could not be found." );
-                       } else {
-                               $themes[] = $theme;
-                       }
-               }
-
-               return $themes;
-       }
 }
 
 WP_CLI::add_command( 'theme', 'Theme_Command' );