OSDN Git Service

compute the history file path at the beginning
authorscribu <mail@scribu.net>
Wed, 3 Apr 2013 22:54:56 +0000 (01:54 +0300)
committerscribu <mail@scribu.net>
Wed, 3 Apr 2013 22:54:59 +0000 (01:54 +0300)
this avoids problems if the user calls chdir()

php/commands/shell.php

index 3f0eac0..fd32cb6 100644 (file)
@@ -10,8 +10,10 @@ class Shell_Command extends \WP_CLI_Command {
        public function __invoke() {
                \WP_CLI::line( 'Type "exit" to close session.' );
 
+               $this->set_history_file();
+
                while ( true ) {
-                       $line = self::prompt();
+                       $line = $this->prompt();
 
                        switch ( $line ) {
                                case '': {
@@ -49,11 +51,11 @@ class Shell_Command extends \WP_CLI_Command {
                ) );
        }
 
-       private static function prompt() {
+       private function prompt() {
                static $cmd;
 
                if ( !$cmd ) {
-                       $cmd = self::create_prompt_cmd( 'wp> ', self::get_history_path() );
+                       $cmd = self::create_prompt_cmd( 'wp> ', $this->history_file );
                }
 
                $fp = popen( $cmd, 'r' );
@@ -87,13 +89,11 @@ BASH;
                return '/bin/bash -c ' . escapeshellarg( $cmd );
        }
 
-       private static function print_history() {
-               $history_file = self::get_history_path();
-
-               if ( !is_readable( $history_file ) )
+       private function print_history() {
+               if ( !is_readable( $this->history_file ) )
                        return;
 
-               $lines = array_filter( explode( "\n", file_get_contents( $history_file ) ) );
+               $lines = array_filter( explode( "\n", file_get_contents( $this->history_file ) ) );
 
                foreach ( $lines as $line ) {
                        if ( 'history' == $line )
@@ -105,10 +105,10 @@ BASH;
                }
        }
 
-       private static function get_history_path() {
+       private function set_history_file() {
                $data = getcwd() . get_current_user();
 
-               return sys_get_temp_dir() . '/wp-cli-history-' . md5( $data );
+               $this->history_file = sys_get_temp_dir() . '/wp-cli-history-' . md5( $data );
        }
 
        private static function starts_with( $tokens, $line ) {