From 260f2c835bb78789a9ab9ca4d94595f953257fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sat, 4 Nov 2023 11:38:19 +0100 Subject: [PATCH] feat: Update to PHP-Scoper 0.18.5 --- composer.lock | 14 +++--- src/Box.php | 1 + src/Composer/AutoloadDumper.php | 68 +++++++++++++++++++++++---- src/Composer/ComposerOrchestrator.php | 2 + src/Console/Command/Compile.php | 3 +- src/PhpScoper/NullScoper.php | 5 ++ src/PhpScoper/SerializableScoper.php | 7 ++- 7 files changed, 83 insertions(+), 17 deletions(-) diff --git a/composer.lock b/composer.lock index 076bd9b63..2319fc201 100644 --- a/composer.lock +++ b/composer.lock @@ -977,16 +977,16 @@ }, { "name": "humbug/php-scoper", - "version": "0.18.4", + "version": "0.18.5", "source": { "type": "git", "url": "https://github.com/humbug/php-scoper.git", - "reference": "d79c1486537280c21c907e9a8a610eceb391407f" + "reference": "cbef87b93065e40172d458b42480c9caf9b0a474" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/humbug/php-scoper/zipball/d79c1486537280c21c907e9a8a610eceb391407f", - "reference": "d79c1486537280c21c907e9a8a610eceb391407f", + "url": "https://api.github.com/repos/humbug/php-scoper/zipball/cbef87b93065e40172d458b42480c9caf9b0a474", + "reference": "cbef87b93065e40172d458b42480c9caf9b0a474", "shasum": "" }, "require": { @@ -1054,9 +1054,9 @@ "description": "Prefixes all PHP namespaces in a file or directory.", "support": { "issues": "https://github.com/humbug/php-scoper/issues", - "source": "https://github.com/humbug/php-scoper/tree/0.18.4" + "source": "https://github.com/humbug/php-scoper/tree/0.18.5" }, - "time": "2023-10-20T17:14:04+00:00" + "time": "2023-11-04T10:04:25+00:00" }, { "name": "jetbrains/phpstorm-stubs", @@ -5785,5 +5785,5 @@ "platform-overrides": { "php": "8.1" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Box.php b/src/Box.php index 5b0f4ba88..710466bf1 100644 --- a/src/Box.php +++ b/src/Box.php @@ -132,6 +132,7 @@ public function endBuffering(?callable $dumpAutoload): void $dumpAutoload( $this->scoper->getSymbolsRegistry(), $this->scoper->getPrefix(), + $this->scoper->getExcludedFilePaths(), ); } diff --git a/src/Composer/AutoloadDumper.php b/src/Composer/AutoloadDumper.php index dc8468914..6a4133c88 100644 --- a/src/Composer/AutoloadDumper.php +++ b/src/Composer/AutoloadDumper.php @@ -17,6 +17,11 @@ use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator; use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\NotInstantiable; +use UnexpectedValueException; +use function array_map; +use function explode; +use function implode; +use function preg_match; use function preg_replace; use function str_replace; use const PHP_EOL; @@ -27,38 +32,85 @@ final class AutoloadDumper public static function generateAutoloadStatements( SymbolsRegistry $symbolsRegistry, + array $excludedComposerAutoloadFileHashes, string $autoloadContents, ): string { if (0 === $symbolsRegistry->count()) { return $autoloadContents; } + $autoloadContents = self::extractInlinedAutoloadContents($autoloadContents); + $scoperStatements = self::getOriginalScoperAutoloaderContents( + $symbolsRegistry, + $excludedComposerAutoloadFileHashes, + ); + + $indentedAutoloadContents = self::fixInlinedAutoloadIndent( + $autoloadContents, + self::getLoaderStatementIndent($scoperStatements), + ); + + $mergedAutoloadContents = preg_replace( + '/(\s*\\$loader \= .*autoload\.php.*)/', + $indentedAutoloadContents, + $scoperStatements, + ); + + return self::cleanupDuplicateLineReturns($mergedAutoloadContents); + } + + private static function extractInlinedAutoloadContents(string $autoloadContents): string + { $autoloadContents = str_replace('dump(); + private static function getOriginalScoperAutoloaderContents( + SymbolsRegistry $symbolsRegistry, + array $excludedComposerAutoloadFileHashes, + ): string { + $generator = new ScoperAutoloadGenerator($symbolsRegistry, $excludedComposerAutoloadFileHashes); + $scoperStatements = $generator->dump(); - $scoperStatements = preg_replace( + return preg_replace( '/scoper\-autoload\.php \@generated by PhpScoper/', '@generated by Humbug Box', $scoperStatements, ); + } - $scoperStatements = preg_replace( - '/(\s*\\$loader \= .*)/', - $autoloadContents, - $scoperStatements, + private static function getLoaderStatementIndent(string $scoperStatements): string + { + if (1 !== preg_match('/(? *)\\$loader \= .*autoload\.php.*/', $scoperStatements, $matches)) { + throw new UnexpectedValueException('Could not process the scoper autoloader statements'); + } + + return $matches['indent']; + } + + private static function fixInlinedAutoloadIndent(string $autoloadContents, string $indent): string + { + $lines = explode(PHP_EOL, $autoloadContents); + + $indentedLines = array_map( + static fn (string $line) => '' === $line ? $line : $indent.$line, + $lines, ); + return implode(PHP_EOL, $indentedLines); + } + + private static function cleanupDuplicateLineReturns(string $value): string + { return preg_replace( '/\n{2,}/m', PHP_EOL.PHP_EOL, - $scoperStatements, + $value, ); } } diff --git a/src/Composer/ComposerOrchestrator.php b/src/Composer/ComposerOrchestrator.php index d0eeee288..db259dd7c 100644 --- a/src/Composer/ComposerOrchestrator.php +++ b/src/Composer/ComposerOrchestrator.php @@ -112,6 +112,7 @@ public function dumpAutoload( SymbolsRegistry $symbolsRegistry, string $prefix, bool $excludeDevFiles, + array $excludedComposerAutoloadFileHashes, ): void { $this->dumpAutoloader(true === $excludeDevFiles); @@ -123,6 +124,7 @@ public function dumpAutoload( $autoloadContents = AutoloadDumper::generateAutoloadStatements( $symbolsRegistry, + $excludedComposerAutoloadFileHashes, $this->fileSystem->getFileContents($autoloadFile), ); diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 5958b249a..7a0f9e9e8 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -668,10 +668,11 @@ private static function commit( $box->endBuffering( $config->dumpAutoload() - ? static fn (SymbolsRegistry $symbolsRegistry, string $prefix) => $composerOrchestrator->dumpAutoload( + ? static fn (SymbolsRegistry $symbolsRegistry, string $prefix, array $excludeScoperFiles) => $composerOrchestrator->dumpAutoload( $symbolsRegistry, $prefix, $excludeDevFiles, + $excludeScoperFiles, ) : null, ); diff --git a/src/PhpScoper/NullScoper.php b/src/PhpScoper/NullScoper.php index 812246447..affe05f6f 100644 --- a/src/PhpScoper/NullScoper.php +++ b/src/PhpScoper/NullScoper.php @@ -45,4 +45,9 @@ public function getPrefix(): string { return ''; } + + public function getExcludedFilePaths(): array + { + return []; + } } diff --git a/src/PhpScoper/SerializableScoper.php b/src/PhpScoper/SerializableScoper.php index 88143b62d..152c00e87 100644 --- a/src/PhpScoper/SerializableScoper.php +++ b/src/PhpScoper/SerializableScoper.php @@ -33,7 +33,7 @@ final class SerializableScoper implements Scoper /** * @var list */ - private array $excludedFilePaths; + public array $excludedFilePaths; public function __construct( PhpScoperConfiguration $scoperConfig, @@ -114,4 +114,9 @@ private function createScoper(): PhpScoperScoper ...$this->excludedFilePaths, ); } + + public function getExcludedFilePaths(): array + { + return $this->excludedFilePaths; + } }