diff --git a/src/EvalFile_Command.php b/src/EvalFile_Command.php index 07c10c425..a4eb3cc8d 100644 --- a/src/EvalFile_Command.php +++ b/src/EvalFile_Command.php @@ -1,7 +1,28 @@ \'[^\']*?\')|(?>"[^"]*?")|(?\b__FILE__\b)|(?\b__DIR__\b)/m'; + /** * Loads and executes a PHP file. * @@ -32,13 +53,19 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( "'$file' does not exist." ); } - if ( null === \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) { + if ( null === Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) { WP_CLI::get_runner()->load_wordpress(); } self::execute_eval( $file, $args ); } + /** + * Evaluate a provided file. + * + * @param string $file Filepath to execute, or - for STDIN. + * @param mixed $args Array of positional arguments to pass to the file. + */ private static function execute_eval( $file, $args ) { if ( '-' === $file ) { eval( '?>' . file_get_contents( 'php://stdin' ) ); @@ -46,20 +73,17 @@ private static function execute_eval( $file, $args ) { $file_contents = file_get_contents( $file ); // Check for and remove she-bang. - if ( 0 === strpos( $file_contents, '#!' ) ) { - $file_contents = preg_replace( '/^(#!.*)$/im', '', $file_contents ); + if ( 0 === strncmp( $file_contents, '#!', 2 ) ) { + $file_contents = preg_replace( static::SHEBANG_PATTERN, '', $file_contents ); } $file = realpath( $file ); $dir = dirname( $file ); // Replace __FILE__ and __DIR__ constants with value of $file or $dir. - // We try to be smart and only replace the constants when they are not within quotes. - // Regular expressions being stateless, this is probably not 100% correct for edge cases. - // See https://regex101.com/r/9hXp5d/2/ $file_contents = preg_replace_callback( - '/(?>\'[^\']*?\')|(?>"[^"]*?")|(?__FILE__)|(?__DIR__)/m', - function ( $matches ) use ( $file, $dir ) { + static::FILE_DIR_PATTERN, + static function ( $matches ) use ( $file, $dir ) { if ( ! empty( $matches['file'] ) ) { return "'{$file}'"; }