Skip to content

Commit 5d16f0f

Browse files
authored
Merge pull request #44 from wp-cli/improve-file-dir-regex-pattern
Improve `__FILE__`/`__DIR__` regex pattern
2 parents a5fb761 + 59d5d8e commit 5d16f0f

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

src/EvalFile_Command.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
class EvalFile_Command extends WP_CLI_Command {
46

7+
/**
8+
* Regular expression pattern to match the shell shebang.
9+
*
10+
* @var string
11+
*/
12+
const SHEBANG_PATTERN = '/^(#!.*)$/m';
13+
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+
526
/**
627
* Loads and executes a PHP file.
728
*
@@ -32,34 +53,37 @@ public function __invoke( $args, $assoc_args ) {
3253
WP_CLI::error( "'$file' does not exist." );
3354
}
3455

35-
if ( null === \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) {
56+
if ( null === Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) {
3657
WP_CLI::get_runner()->load_wordpress();
3758
}
3859

3960
self::execute_eval( $file, $args );
4061
}
4162

63+
/**
64+
* Evaluate a provided file.
65+
*
66+
* @param string $file Filepath to execute, or - for STDIN.
67+
* @param mixed $args Array of positional arguments to pass to the file.
68+
*/
4269
private static function execute_eval( $file, $args ) {
4370
if ( '-' === $file ) {
4471
eval( '?>' . file_get_contents( 'php://stdin' ) );
4572
} else {
4673
$file_contents = file_get_contents( $file );
4774

4875
// Check for and remove she-bang.
49-
if ( 0 === strpos( $file_contents, '#!' ) ) {
50-
$file_contents = preg_replace( '/^(#!.*)$/im', '', $file_contents );
76+
if ( 0 === strncmp( $file_contents, '#!', 2 ) ) {
77+
$file_contents = preg_replace( static::SHEBANG_PATTERN, '', $file_contents );
5178
}
5279

5380
$file = realpath( $file );
5481
$dir = dirname( $file );
5582

5683
// Replace __FILE__ and __DIR__ constants with value of $file or $dir.
57-
// We try to be smart and only replace the constants when they are not within quotes.
58-
// Regular expressions being stateless, this is probably not 100% correct for edge cases.
59-
// See https://regex101.com/r/9hXp5d/2/
6084
$file_contents = preg_replace_callback(
61-
'/(?>\'[^\']*?\')|(?>"[^"]*?")|(?<file>__FILE__)|(?<dir>__DIR__)/m',
62-
function ( $matches ) use ( $file, $dir ) {
85+
static::FILE_DIR_PATTERN,
86+
static function ( $matches ) use ( $file, $dir ) {
6387
if ( ! empty( $matches['file'] ) ) {
6488
return "'{$file}'";
6589
}

0 commit comments

Comments
 (0)