From 13abce20ad70fc027b5fed915d86a436dee224c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 8 Mar 2024 14:24:18 +0100 Subject: [PATCH 1/3] Draft: fix: Fix the autoloading of the excluded files This fixes the integration of https://github.com/humbug/php-scoper/pull/864 within Box which was done in #1142. --- src/Box.php | 2 +- src/Composer/AutoloadDumper.php | 36 ++++++++++++++++++++++++++- src/Composer/ComposerOrchestrator.php | 20 ++++++++++++--- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/Box.php b/src/Box.php index 96e5e9143..232c8f105 100644 --- a/src/Box.php +++ b/src/Box.php @@ -113,7 +113,7 @@ public function startBuffering(): void } /** - * @param callable(SymbolsRegistry, string): void $dumpAutoload + * @param callable(SymbolsRegistry, string, string[]): void $dumpAutoload */ public function endBuffering(?callable $dumpAutoload): void { diff --git a/src/Composer/AutoloadDumper.php b/src/Composer/AutoloadDumper.php index 6a4133c88..651a16f00 100644 --- a/src/Composer/AutoloadDumper.php +++ b/src/Composer/AutoloadDumper.php @@ -14,15 +14,18 @@ namespace KevinGH\Box\Composer; +use Humbug\PhpScoper\Autoload\ComposerFileHasher; use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator; use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\NotInstantiable; use UnexpectedValueException; +use function array_column; use function array_map; use function explode; use function implode; use function preg_match; use function preg_replace; +use function sprintf; use function str_replace; use const PHP_EOL; @@ -30,15 +33,26 @@ final class AutoloadDumper { use NotInstantiable; + private const PACKAGE_PATH_REGEX = '~^%s/(?[^/]+?/[^/]+?)/(?.+?)$~'; + + /** + * @param string[] $excludedComposerAutoloadFiles + */ public static function generateAutoloadStatements( SymbolsRegistry $symbolsRegistry, - array $excludedComposerAutoloadFileHashes, + string $vendorDir, + array $excludedComposerAutoloadFiles, string $autoloadContents, ): string { if (0 === $symbolsRegistry->count()) { return $autoloadContents; } + $excludedComposerAutoloadFileHashes = self::getExcludedComposerAutoloadFileHashes( + $vendorDir, + $excludedComposerAutoloadFiles, + ); + $autoloadContents = self::extractInlinedAutoloadContents($autoloadContents); $scoperStatements = self::getOriginalScoperAutoloaderContents( $symbolsRegistry, @@ -59,6 +73,26 @@ public static function generateAutoloadStatements( return self::cleanupDuplicateLineReturns($mergedAutoloadContents); } + /** + * @param string[] $excludedComposerAutoloadFiles + */ + private static function getExcludedComposerAutoloadFileHashes( + string $vendorDir, + array $excludedComposerAutoloadFiles, + ): array + { + $fileHashGenerator = new ComposerFileHasher( + '', + $excludedComposerAutoloadFiles, + sprintf( + self::PACKAGE_PATH_REGEX, + $vendorDir, + ), + ); + + return $fileHashGenerator->generateHashes(); + } + private static function extractInlinedAutoloadContents(string $autoloadContents): string { $autoloadContents = str_replace('dumpAutoloader(true === $excludeDevFiles); @@ -122,17 +131,22 @@ public function dumpAutoload( return; } - $autoloadFile = $this->getVendorDir().'/autoload.php'; + $vendorDir = $this->getVendorDir(); + $autoloadFile = $vendorDir .'/autoload.php'; $autoloadContents = AutoloadDumper::generateAutoloadStatements( $symbolsRegistry, - $excludedComposerAutoloadFileHashes, + $vendorDir, + $excludedComposerAutoloadFiles, $this->fileSystem->getFileContents($autoloadFile), ); $this->fileSystem->dumpFile($autoloadFile, $autoloadContents); } + /** + * @return string The vendor-dir directory path relative to its composer.json. + */ public function getVendorDir(): string { $vendorDirProcess = $this->processFactory->getVendorDirProcess(); From c34a04ac03c3935335e7253ba03557fd0c839271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 8 Mar 2024 15:06:12 +0100 Subject: [PATCH 2/3] fix cs --- src/Composer/AutoloadDumper.php | 4 +--- src/Composer/ComposerOrchestrator.php | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Composer/AutoloadDumper.php b/src/Composer/AutoloadDumper.php index 651a16f00..f96b951fc 100644 --- a/src/Composer/AutoloadDumper.php +++ b/src/Composer/AutoloadDumper.php @@ -19,7 +19,6 @@ use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\NotInstantiable; use UnexpectedValueException; -use function array_column; use function array_map; use function explode; use function implode; @@ -79,8 +78,7 @@ public static function generateAutoloadStatements( private static function getExcludedComposerAutoloadFileHashes( string $vendorDir, array $excludedComposerAutoloadFiles, - ): array - { + ): array { $fileHashGenerator = new ComposerFileHasher( '', $excludedComposerAutoloadFiles, diff --git a/src/Composer/ComposerOrchestrator.php b/src/Composer/ComposerOrchestrator.php index f3ecf2eb9..a9a2fc6e7 100644 --- a/src/Composer/ComposerOrchestrator.php +++ b/src/Composer/ComposerOrchestrator.php @@ -111,13 +111,9 @@ public function checkVersion(): void } /** - * @param SymbolsRegistry $symbolsRegistry - * @param string $prefix - * @param bool $excludeDevFiles * @param string[] $excludedComposerAutoloadFiles Relative paths of the files that were not scoped hence which need * to be configured as loaded to Composer as otherwise they would be * autoloaded twice. - * @return void */ public function dumpAutoload( SymbolsRegistry $symbolsRegistry, @@ -132,7 +128,7 @@ public function dumpAutoload( } $vendorDir = $this->getVendorDir(); - $autoloadFile = $vendorDir .'/autoload.php'; + $autoloadFile = $vendorDir.'/autoload.php'; $autoloadContents = AutoloadDumper::generateAutoloadStatements( $symbolsRegistry, From 814b4fe21690eb6d7c9fa4d97d044e6267363f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 8 Mar 2024 15:14:06 +0100 Subject: [PATCH 3/3] fix --- tests/Composer/AutoloadDumperTest.php | 50 +++++++++++++++++---------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/tests/Composer/AutoloadDumperTest.php b/tests/Composer/AutoloadDumperTest.php index 11e100b7a..802ba8b58 100644 --- a/tests/Composer/AutoloadDumperTest.php +++ b/tests/Composer/AutoloadDumperTest.php @@ -19,6 +19,7 @@ use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use function md5; /** * @internal @@ -29,12 +30,14 @@ final class AutoloadDumperTest extends TestCase #[DataProvider('autoloadProvider')] public function test_it_can_generate_the_autoload( SymbolsRegistry $symbolsRegistry, + string $vendorDir, array $excludedComposerAutoloadFileHashes, string $autoloadContents, string $expected, ): void { $actual = AutoloadDumper::generateAutoloadStatements( $symbolsRegistry, + $vendorDir, $excludedComposerAutoloadFileHashes, $autoloadContents, ); @@ -44,8 +47,16 @@ public function test_it_can_generate_the_autoload( public static function autoloadProvider(): iterable { + $defaultVendorDir = 'vendor'; + + $excludedFile1 = 'vendor/phpstorm/stubs/stub1.php'; + $excludedFile1Hash = md5('phpstorm/stubs:stub1.php'); + $excludedFile2 = 'vendor/phpstorm/stubs/stub2.php'; + $excludedFile2Hash = md5('phpstorm/stubs:stub2.php'); + yield 'no symbols' => [ new SymbolsRegistry(), + $defaultVendorDir, [], <<<'PHP'