@@ -11,18 +11,6 @@ class EvalFile_Command extends WP_CLI_Command {
1111 */
1212 const SHEBANG_PATTERN = '/^(#!.*)$/m ' ;
1313
14- /**
15- * Regular expression pattern to match __FILE__ and __DIR__ constants.
16- *
17- * We try to be smart and only replace the constants when they are not within quotes.
18- * Regular expressions being stateless, this is probably not 100% correct for edge cases.
19- *
20- * @see https://regex101.com/r/9hXp5d/4/
21- *
22- * @var string
23- */
24- const FILE_DIR_PATTERN = '/(?> \'[^ \']*? \')|(?>"[^"]*?")|(?<file>\b__FILE__\b)|(?<dir>\b__DIR__\b)/m ' ;
25-
2614 /**
2715 * Loads and executes a PHP file.
2816 *
@@ -72,31 +60,14 @@ private static function execute_eval( $file, $args ) {
7260 } else {
7361 $ file_contents = file_get_contents ( $ file );
7462
63+ // Adjust for __FILE__ and __DIR__ magic constants.
64+ $ file_contents = Utils \replace_path_consts ( $ file_contents , $ file );
65+
7566 // Check for and remove she-bang.
7667 if ( 0 === strncmp ( $ file_contents , '#! ' , 2 ) ) {
7768 $ file_contents = preg_replace ( static ::SHEBANG_PATTERN , '' , $ file_contents );
7869 }
7970
80- $ file = realpath ( $ file );
81- $ dir = dirname ( $ file );
82-
83- // Replace __FILE__ and __DIR__ constants with value of $file or $dir.
84- $ file_contents = preg_replace_callback (
85- static ::FILE_DIR_PATTERN ,
86- static function ( $ matches ) use ( $ file , $ dir ) {
87- if ( ! empty ( $ matches ['file ' ] ) ) {
88- return "' {$ file }' " ;
89- }
90-
91- if ( ! empty ( $ matches ['dir ' ] ) ) {
92- return "' {$ dir }' " ;
93- }
94-
95- return $ matches [0 ];
96- },
97- $ file_contents
98- );
99-
10071 eval ( '?> ' . $ file_contents );
10172 }
10273 }
0 commit comments