Skip to content

Commit

Permalink
feat: Update to PHP-Scoper 0.18.5 (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 4, 2023
1 parent 0c76068 commit 1e10a1e
Show file tree
Hide file tree
Showing 13 changed files with 846 additions and 312 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"composer/xdebug-handler": "^3.0.3",
"fidry/console": "^0.5.3 || ^0.6.0",
"fidry/filesystem": "^1.1",
"humbug/php-scoper": "^0.18.3",
"humbug/php-scoper": "^0.18.6",
"justinrainbow/json-schema": "^5.2.12",
"laravel/serializable-closure": "^1.2.2",
"nikic/iter": "^2.2",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function endBuffering(?callable $dumpAutoload): void
$dumpAutoload(
$this->scoper->getSymbolsRegistry(),
$this->scoper->getPrefix(),
$this->scoper->getExcludedFilePaths(),
);
}

Expand Down
68 changes: 60 additions & 8 deletions src/Composer/AutoloadDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('<?php', '', $autoloadContents);

$autoloadContents = preg_replace(
return preg_replace(
'/return (ComposerAutoloaderInit.+::getLoader\(\));/',
'\$loader = $1;',
$autoloadContents,
);
}

$scoperStatements = (new ScoperAutoloadGenerator($symbolsRegistry))->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('/(?<indent> *)\\$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,
);
}
}
2 changes: 2 additions & 0 deletions src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function dumpAutoload(
SymbolsRegistry $symbolsRegistry,
string $prefix,
bool $excludeDevFiles,
array $excludedComposerAutoloadFileHashes,
): void {
$this->dumpAutoloader(true === $excludeDevFiles);

Expand All @@ -123,6 +124,7 @@ public function dumpAutoload(

$autoloadContents = AutoloadDumper::generateAutoloadStatements(
$symbolsRegistry,
$excludedComposerAutoloadFileHashes,
$this->fileSystem->getFileContents($autoloadFile),
);

Expand Down
3 changes: 2 additions & 1 deletion src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
5 changes: 5 additions & 0 deletions src/PhpScoper/NullScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function getPrefix(): string
{
return '';
}

public function getExcludedFilePaths(): array
{
return [];
}
}
7 changes: 6 additions & 1 deletion src/PhpScoper/SerializableScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class SerializableScoper implements Scoper
/**
* @var list<string>
*/
private array $excludedFilePaths;
public array $excludedFilePaths;

public function __construct(
PhpScoperConfiguration $scoperConfig,
Expand Down Expand Up @@ -114,4 +114,9 @@ private function createScoper(): PhpScoperScoper
...$this->excludedFilePaths,
);
}

public function getExcludedFilePaths(): array
{
return $this->excludedFilePaths;
}
}
Loading

0 comments on commit 1e10a1e

Please sign in to comment.