diff --git a/.gitignore b/.gitignore index 75301742a..8d8f5afbc 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ /fixtures/build/dir010/*.phar /fixtures/build/dir011/*.phar /fixtures/build/dir011/*output +/fixtures/build/dir011/phar-Y.php /fixtures/check-requirements/*/actual-output /fixtures/check-requirements/*/expected-output-* /fixtures/check-requirements/*/*.phar diff --git a/Makefile b/Makefile index e2fa207c0..8f607df36 100644 --- a/Makefile +++ b/Makefile @@ -95,9 +95,12 @@ e2e_scoper_whitelist: ## Runs the end-to-end tests to check that the PHP-Scope e2e_scoper_whitelist: box fixtures/build/dir011/vendor php fixtures/build/dir011/index.php > fixtures/build/dir011/expected-output ./box compile --working-dir fixtures/build/dir011 + php fixtures/build/dir011/index.phar > fixtures/build/dir011/output + cd fixtures/build/dir011 && php -r "file_put_contents('phar-Y.php', file_get_contents((new Phar('index.phar'))['src/Y.php']));" diff fixtures/build/dir011/expected-output fixtures/build/dir011/output + diff fixtures/build/dir011/phar-Y.php fixtures/build/dir011/src/Y.php .PHONY: e2e_check_requirements DOCKER=docker run -i --rm -w /opt/box diff --git a/fixtures/build/dir011/scoper.inc.php b/fixtures/build/dir011/scoper.inc.php index 719f4b21a..33b72d55f 100644 --- a/fixtures/build/dir011/scoper.inc.php +++ b/fixtures/build/dir011/scoper.inc.php @@ -14,4 +14,5 @@ return [ 'whitelist' => ['Foo\back'], + 'files-whitelist' => ['src/Y.php'], ]; diff --git a/fixtures/build/dir011/src/Y.php b/fixtures/build/dir011/src/Y.php new file mode 100644 index 000000000..9534d0f9c --- /dev/null +++ b/fixtures/build/dir011/src/Y.php @@ -0,0 +1,21 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +final class Y +{ + public static function salute(): void + { + echo 'World'; + } +} diff --git a/scoper.inc.php b/scoper.inc.php index ab47bbf29..5dd34748a 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -14,8 +14,6 @@ use Isolated\Symfony\Component\Finder\Finder; -$classLoaderContents = file_get_contents(__DIR__.'/vendor/composer/composer/src/Composer/Autoload/ClassLoader.php'); - return [ 'patchers' => [ function (string $filePath, string $prefix, string $contents): string { @@ -65,9 +63,6 @@ function (string $filePath, string $prefix, string $contents): string { $contents ); }, - function (string $filePath, string $prefix, string $contents) use ($classLoaderContents): string { - return 'vendor/composer/composer/src/Composer/Autoload/ClassLoader.php' === $filePath ? $classLoaderContents : $contents; - }, function (string $filePath, string $prefix, string $contents): string { if ('vendor/paragonie/sodium_compat/autoload.php' !== $filePath) { return $contents; @@ -105,6 +100,9 @@ function (string $filePath, string $prefix, string $contents): string { ); }, ], + 'files-whitelist' => [ + __DIR__.'/vendor/composer/composer/src/Composer/Autoload/ClassLoader.php', + ], 'whitelist' => [ \Composer\Autoload\ClassLoader::class, diff --git a/src/Configuration.php b/src/Configuration.php index bc524a0d8..37cbca25e 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -23,6 +23,7 @@ use Humbug\PhpScoper\Configuration as PhpScoperConfiguration; use Humbug\PhpScoper\Console\ApplicationFactory; use Humbug\PhpScoper\Scoper; +use Humbug\PhpScoper\Scoper\FileWhitelistScoper; use InvalidArgumentException; use KevinGH\Box\Compactor\Php as PhpCompactor; use KevinGH\Box\Compactor\PhpScoper as PhpScoperCompactor; @@ -2444,6 +2445,21 @@ public static function createScoper(): Scoper } })::createScoper(); + if ([] !== $phpScoperConfig->getWhitelistedFiles()) { + $whitelistedFiles = array_values( + array_unique( + array_map( + static function (string $path) use ($basePath): string { + return make_path_relative($path, $basePath); + }, + $phpScoperConfig->getWhitelistedFiles() + ) + ) + ); + + $phpScoper = new FileWhitelistScoper($phpScoper, ...$whitelistedFiles); + } + $prefix = null === $phpScoperConfig->getPrefix() ? unique_id('_HumbugBox') : $phpScoperConfig->getPrefix()