OSDN Git Service

extract replace_path_consts() utility
authorscribu <mail@scribu.net>
Sat, 25 Jan 2014 14:41:09 +0000 (16:41 +0200)
committerscribu <mail@scribu.net>
Sat, 25 Jan 2014 14:42:18 +0000 (16:42 +0200)
php/WP_CLI/Runner.php
php/utils.php

index 57f000a..578d9bf 100644 (file)
@@ -225,14 +225,6 @@ class Runner {
        public function get_wp_config_code() {
                $wp_config_path = Utils\locate_wp_config();
 
-               $replacements = array(
-                       '__FILE__' => "'$wp_config_path'",
-                       '__DIR__'  => "'" . dirname( $wp_config_path ) . "'"
-               );
-
-               $old = array_keys( $replacements );
-               $new = array_values( $replacements );
-
                $wp_config_code = explode( "\n", file_get_contents( $wp_config_path ) );
 
                $lines_to_run = array();
@@ -241,10 +233,12 @@ class Runner {
                        if ( preg_match( '/^\s*require.+wp-settings\.php/', $line ) )
                                continue;
 
-                       $lines_to_run[] = str_replace( $old, $new, $line );
+                       $lines_to_run[] = $line;
                }
 
-               return preg_replace( '|^\s*\<\?php\s*|', '', implode( "\n", $lines_to_run ) );
+               $source = implode( "\n", $lines_to_run );
+               $source = Utils\replace_path_consts( $source, $wp_config_path );
+               return preg_replace( '|^\s*\<\?php\s*|', '', $source );
        }
 
        // Transparently convert old syntaxes
index a281bd1..7ce26e8 100644 (file)
@@ -382,3 +382,21 @@ function is_windows() {
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
 }
 
+/**
+ * Replace magic constants in some PHP source code.
+ *
+ * @param string $source The PHP code to manipulate.
+ * @param string $path The path to use instead of the magic constants
+ */
+function replace_path_consts( $source, $path ) {
+       $replacements = array(
+               '__FILE__' => "'$path'",
+               '__DIR__'  => "'" . dirname( $path ) . "'"
+       );
+
+       $old = array_keys( $replacements );
+       $new = array_values( $replacements );
+
+       return str_replace( $old, $new, $source );
+}
+