Skip to content

Commit 7058e4a

Browse files
committed
Dump a new autoloader when generating a Phar
1 parent f81af57 commit 7058e4a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Context/FeatureContext.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,13 @@ public function build_phar( $version = 'same' ) {
921921
$make_phar_path = self::get_vendor_dir() . '/../utils/make-phar.php';
922922
}
923923

924+
// Temporarily modify the Composer autoloader used within the Phar
925+
// so that it doesn't clash if autoloading is already happening outside of it,
926+
// for example when generating code coverage.
927+
// This modifies composer.json.
928+
$this->composer_command( 'config autoloader-suffix "WpCliTestsPhar" --working-dir=' . dirname( self::get_vendor_dir() ) );
929+
$this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) );
930+
924931
$this->proc(
925932
Utils\esc_cmd(
926933
'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s',
@@ -929,6 +936,13 @@ public function build_phar( $version = 'same' ) {
929936
$version
930937
)
931938
)->run_check();
939+
940+
// Revert the composer.json modification again
941+
$this->composer_command( 'config --unset autoloader-suffix --working-dir=' . dirname( self::get_vendor_dir() ) );
942+
// Prevent Composer from simply reusing the existing suffix.
943+
$this->remove_dir( self::get_vendor_dir() . '/autoload.php' );
944+
// Now it will use the content hash again.
945+
$this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) );
932946
}
933947

934948
public function download_phar( $version = 'same' ) {

0 commit comments

Comments
 (0)