Skip to content

Commit 02593d3

Browse files
Copilotswissspidy
andcommitted
Add phar_safe_path override to fix template path resolution
Created php/utils-override.php with fixed phar_safe_path() that replaces full paths with alias 'phar://wp-cli.phar/' instead of bare 'phar://'. Include this override in boot-phar.php after wp-cli.php is loaded. This is a temporary fix until wp-cli/wp-cli#6242 is merged. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent bf3c0cc commit 02593d3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

php/boot-phar.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111

1212
// Store the filesystem path for `Utils\phar_safe_path()` function.
1313
// Using Phar::running(false) returns just the filesystem path without phar:// protocol.
14-
// This prevents phar_safe_path() from attempting to replace "phar://phar://..." incorrectly.
1514
define( 'WP_CLI_PHAR_PATH', Phar::running( false ) );
1615

1716
// Use the phar alias 'wp-cli.phar' which is set during phar creation and works
1817
// regardless of the actual filename. This ensures template paths resolve correctly.
1918
if ( file_exists( 'phar://wp-cli.phar/php/wp-cli.php' ) ) {
2019
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar' );
2120
include WP_CLI_ROOT . '/php/wp-cli.php';
21+
// Override phar_safe_path() to fix template resolution (temporary until wp-cli/wp-cli#6242 is merged)
22+
include __DIR__ . '/utils-override.php';
2223
} elseif ( file_exists( 'phar://wp-cli.phar/vendor/wp-cli/wp-cli/php/wp-cli.php' ) ) {
2324
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar/vendor/wp-cli/wp-cli' );
2425
include WP_CLI_ROOT . '/php/wp-cli.php';
26+
// Override phar_safe_path() to fix template resolution (temporary until wp-cli/wp-cli#6242 is merged)
27+
include __DIR__ . '/utils-override.php';
2528
} else {
2629
echo "Couldn't find 'php/wp-cli.php'. Was this Phar built correctly?";
2730
exit( 1 );

php/utils-override.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Override for Utils\phar_safe_path() to fix template path resolution
4+
* when phar is renamed.
5+
*
6+
* This fix will be removed once wp-cli/wp-cli#6242 is merged and integrated.
7+
*/
8+
9+
namespace WP_CLI\Utils;
10+
11+
/**
12+
* Get a Phar-safe version of a path.
13+
*
14+
* For paths inside a Phar, this strips the outer filesystem's location to
15+
* reduce the path to what it needs to be within the Phar archive.
16+
*
17+
* Use the __FILE__ or __DIR__ constants as a starting point.
18+
*
19+
* @param string $path An absolute path that might be within a Phar.
20+
* @return string A Phar-safe version of the path.
21+
*/
22+
function phar_safe_path( $path ) {
23+
24+
if ( ! inside_phar() ) {
25+
return $path;
26+
}
27+
28+
return str_replace(
29+
PHAR_STREAM_PREFIX . rtrim( WP_CLI_PHAR_PATH, '/' ) . '/',
30+
'phar://wp-cli.phar/',
31+
$path
32+
);
33+
}

0 commit comments

Comments
 (0)