Skip to content

Commit 464ac60

Browse files
committed
Dump a new autoloader when generating a Phar
1 parent f81af57 commit 464ac60

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Context/FeatureContext.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,23 @@ 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->proc(
929+
Utils\esc_cmd(
930+
'composer config autoloader-suffix "WpCliTestsPhar" --working-dir=%s',
931+
dirname( self::get_vendor_dir() )
932+
)
933+
)->run_check();
934+
$this->proc(
935+
Utils\esc_cmd(
936+
'composer dump-autoload --working-dir=%s',
937+
dirname( self::get_vendor_dir() )
938+
)
939+
)->run_check();
940+
924941
$this->proc(
925942
Utils\esc_cmd(
926943
'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s',
@@ -929,6 +946,23 @@ public function build_phar( $version = 'same' ) {
929946
$version
930947
)
931948
)->run_check();
949+
950+
// Revert the composer.json modification again
951+
$this->proc(
952+
Utils\esc_cmd(
953+
'composer config --unset autoloader-suffix --working-dir=%s',
954+
dirname( self::get_vendor_dir() )
955+
)
956+
)->run_check();
957+
// Prevent Composer from simply reusing the existing suffix.
958+
unlink( self::get_vendor_dir() . '/autoload.php' );
959+
// Now it will use the content hash again.
960+
$this->proc(
961+
Utils\esc_cmd(
962+
'composer dump-autoload -d %s',
963+
dirname( self::get_vendor_dir() )
964+
)
965+
)->run_check();
932966
}
933967

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

0 commit comments

Comments
 (0)