Skip to content

Commit

Permalink
Add support for PhpScoper files whitelisting (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 6, 2018
1 parent 205bb25 commit ecb4b91
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions fixtures/build/dir011/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

return [
'whitelist' => ['Foo\back'],
'files-whitelist' => ['src/Y.php'],
];
21 changes: 21 additions & 0 deletions fixtures/build/dir011/src/Y.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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';
}
}
8 changes: 3 additions & 5 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,

Expand Down
16 changes: 16 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ecb4b91

Please sign in to comment.