From ae2b105f7259f39cbc240f557960c11e52feea72 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 5 Feb 2020 10:53:31 +0100 Subject: [PATCH 1/4] Improve __FILE__/__DIR__ regex pattern --- src/EvalFile_Command.php | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/EvalFile_Command.php b/src/EvalFile_Command.php index 07c10c425..e8667f165 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,34 +53,36 @@ 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 ); + self::execute_eval( $file ); } - private static function execute_eval( $file, $args ) { + /** + * Evaluate a provided file. + * + * @param string $file Filepath to execute, or - for STDIN. + */ + private static function execute_eval( $file ) { if ( '-' === $file ) { eval( '?>' . file_get_contents( 'php://stdin' ) ); } else { $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::SHE_BANG_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}'"; } From 37a97adb34c1e09e851f30903cfb1756c10f5db7 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 5 Feb 2020 15:03:44 +0100 Subject: [PATCH 2/4] Improve const wording --- src/EvalFile_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EvalFile_Command.php b/src/EvalFile_Command.php index e8667f165..3eb797588 100644 --- a/src/EvalFile_Command.php +++ b/src/EvalFile_Command.php @@ -9,7 +9,7 @@ class EvalFile_Command extends WP_CLI_Command { * * @var string */ - const SHE_BANG_PATTERN = '/^(#!.*)$/m'; + const SHEBANG_PATTERN = '/^(#!.*)$/m'; /** * Regular expression pattern to match __FILE__ and __DIR__ constants. @@ -73,7 +73,7 @@ private static function execute_eval( $file ) { // Check for and remove she-bang. if ( 0 === strncmp( $file_contents, '#!', 2 ) ) { - $file_contents = preg_replace( static::SHE_BANG_PATTERN, '', $file_contents ); + $file_contents = preg_replace( static::SHEBANG_PATTERN, '', $file_contents ); } $file = realpath( $file ); From 8e94899a37d36e3f4cb6f066e2fa36e441dcb4dd Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 5 Feb 2020 15:06:54 +0100 Subject: [PATCH 3/4] Do not strip $args, as it is needed --- src/EvalFile_Command.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/EvalFile_Command.php b/src/EvalFile_Command.php index 3eb797588..d9557002c 100644 --- a/src/EvalFile_Command.php +++ b/src/EvalFile_Command.php @@ -57,15 +57,16 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::get_runner()->load_wordpress(); } - self::execute_eval( $file ); + self::execute_eval( $file, $args ); } /** * Evaluate a provided file. * * @param string $file Filepath to execute, or - for STDIN. + * @param mixed $args One or more arguments to pass to the file. */ - private static function execute_eval( $file ) { + private static function execute_eval( $file, $args ) { if ( '-' === $file ) { eval( '?>' . file_get_contents( 'php://stdin' ) ); } else { From a865193ef8a3930ff1307362318aab53cc376d01 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 5 Feb 2020 15:08:02 +0100 Subject: [PATCH 4/4] Improve wording of $args description --- src/EvalFile_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EvalFile_Command.php b/src/EvalFile_Command.php index d9557002c..a4eb3cc8d 100644 --- a/src/EvalFile_Command.php +++ b/src/EvalFile_Command.php @@ -64,7 +64,7 @@ public function __invoke( $args, $assoc_args ) { * Evaluate a provided file. * * @param string $file Filepath to execute, or - for STDIN. - * @param mixed $args One or more arguments to pass to the file. + * @param mixed $args Array of positional arguments to pass to the file. */ private static function execute_eval( $file, $args ) { if ( '-' === $file ) {