Skip to content

Commit

Permalink
Reworking the whitelist API (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 16, 2018
1 parent 53494e8 commit bc3061c
Show file tree
Hide file tree
Showing 62 changed files with 1,659 additions and 1,296 deletions.
62 changes: 28 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.DEFAULT_GOAL := help

PHPNOGC=php -d zend.enable_gc=0
PHPBIN=php

.PHONY: help
help:
Expand Down Expand Up @@ -35,7 +35,7 @@ test: tc e2e
PHPUNIT=bin/phpunit
tu: ## Run PHPUnit tests
tu: bin/phpunit
$(PHPNOGC) $(PHPUNIT)
$(PHPBIN) $(PHPUNIT)

.PHONY: tc
tc: ## Run PHPUnit tests with test coverage
Expand All @@ -55,23 +55,23 @@ PHPSCOPER=bin/php-scoper.phar
.PHONY: e2e_004
e2e_004: ## Run end-to-end tests for the fixture set 004 — Source code case
e2e_004: bin/php-scoper.phar
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set004
$(PHPBIN) $(BOX) compile --working-dir fixtures/set004

php build/set004/bin/greet.phar > build/set004/output
diff fixtures/set004/expected-output build/set004/output

.PHONY: e2e_005
e2e_005: ## Run end-to-end tests for the fixture set 005 — Third-party code case
e2e_005: bin/php-scoper.phar fixtures/set005/vendor
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set005
$(PHPBIN) $(BOX) compile --working-dir fixtures/set005

php build/set005/bin/greet.phar > build/set005/output
diff fixtures/set005/expected-output build/set005/output

.PHONY: e2e_011
e2e_011: ## Run end-to-end tests for the fixture set 011 — Whitelist case
e2e_011: bin/php-scoper.phar fixtures/set011/vendor
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set011
$(PHPBIN) $(BOX) compile --working-dir fixtures/set011
cp -R fixtures/set011/tests/ build/set011/tests/

php build/set011/bin/greet.phar > build/set011/output
Expand All @@ -88,23 +88,23 @@ e2e_013: bin/php-scoper.phar
.PHONY: e2e_014
e2e_014: ## Run end-to-end tests for the fixture set 014 — Source code case with PSR-0
e2e_014: bin/php-scoper.phar
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set014
$(PHPBIN) $(BOX) compile --working-dir fixtures/set014

php build/set014/bin/greet.phar > build/set014/output
diff fixtures/set014/expected-output build/set014/output

.PHONY: e2e_015
e2e_015: ## Run end-to-end tests for the fixture set 015 — Third-party code case with PSR-0
e2e_015: bin/php-scoper.phar fixtures/set015/vendor
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set015
$(PHPBIN) $(BOX) compile --working-dir fixtures/set015

php build/set015/bin/greet.phar > build/set015/output
diff fixtures/set015/expected-output build/set015/output

.PHONY: e2e_016
e2e_016: ## Run end-to-end tests for the fixture set 016 — Symfony Finder
e2e_016: bin/php-scoper.phar fixtures/set016-symfony-finder/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set016-symfony-finder \
--output-dir=../../build/set016-symfony-finder \
--force \
Expand All @@ -119,7 +119,7 @@ e2e_016: bin/php-scoper.phar fixtures/set016-symfony-finder/vendor
.PHONY: e2e_017
e2e_017: ## Run end-to-end tests for the fixture set 017 — Symfony DependencyInjection
e2e_017: bin/php-scoper.phar fixtures/set017-symfony-di/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set017-symfony-di \
--output-dir=../../build/set017-symfony-di \
--force \
Expand All @@ -134,7 +134,7 @@ e2e_017: bin/php-scoper.phar fixtures/set017-symfony-di/vendor
.PHONY: e2e_018
e2e_018: ## Run end-to-end tests for the fixture set 018 — Nikic PHP-Parser
e2e_018: bin/php-scoper.phar fixtures/set018-nikic-parser/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set018-nikic-parser \
--prefix=_Prefixed \
--output-dir=../../build/set018-nikic-parser \
Expand All @@ -149,7 +149,7 @@ e2e_018: bin/php-scoper.phar fixtures/set018-nikic-parser/vendor
.PHONY: e2e_019
e2e_019: ## Run end-to-end tests for the fixture set 019 — Symfony Console
e2e_019: bin/php-scoper.phar fixtures/set019-symfony-console/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set019-symfony-console \
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set019-symfony-console \
--prefix=_Prefixed \
--output-dir=../../build/set019-symfony-console \
--force \
Expand All @@ -164,7 +164,7 @@ e2e_019: bin/php-scoper.phar fixtures/set019-symfony-console/vendor
.PHONY: e2e_020
e2e_020: ## Run end-to-end tests for the fixture set 020 — Infection
e2e_020: bin/php-scoper.phar fixtures/set020-infection/vendor clover.xml
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set020-infection \
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set020-infection \
--output-dir=../../build/set020-infection \
--force \
--no-interaction \
Expand All @@ -182,8 +182,8 @@ e2e_020: bin/php-scoper.phar fixtures/set020-infection/vendor clover.xml

.PHONY: e2e_021
e2e_021: ## Run end-to-end tests for the fixture set 020 — Composer
e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor clover.xml
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set021-composer \
e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set021-composer \
--output-dir=../../build/set021-composer \
--force \
--no-interaction \
Expand All @@ -203,7 +203,7 @@ e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor clover.xml
.PHONY: e2e_022
e2e_022: ## Run end-to-end tests for the fixture set 022 — Whitelist the project code with namespace whitelisting
e2e_022: bin/php-scoper.phar fixtures/set022/vendor
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set022
$(PHPBIN) $(BOX) compile --working-dir fixtures/set022
cp -R fixtures/set022/tests/ build/set022/tests/

php build/set022/bin/greet.phar > build/set022/output
Expand All @@ -213,7 +213,7 @@ e2e_022: bin/php-scoper.phar fixtures/set022/vendor
.PHONY: e2e_023
e2e_023: ## Run end-to-end tests for the fixture set 023 — Whitelisting a whole third-party component with namespace whitelisting
e2e_023: bin/php-scoper.phar fixtures/set023/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set023 \
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set023 \
--output-dir=../../build/set023 \
--force \
--no-interaction \
Expand All @@ -226,7 +226,7 @@ e2e_023: bin/php-scoper.phar fixtures/set023/vendor
.PHONY: e2e_024
e2e_024: ## Run end-to-end tests for the fixture set 024 — Whitelisting user functions registered in the global namespace
e2e_024: bin/php-scoper.phar fixtures/set024/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set024 \
--output-dir=../../build/set024 \
--force \
Expand All @@ -241,7 +241,7 @@ e2e_024: bin/php-scoper.phar fixtures/set024/vendor
.PHONY: e2e_025
e2e_025: ## Run end-to-end tests for the fixture set 025 — Whitelisting a vendor function
e2e_025: bin/php-scoper.phar fixtures/set025/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set025 \
--output-dir=../../build/set025 \
--force \
Expand All @@ -255,7 +255,7 @@ e2e_025: bin/php-scoper.phar fixtures/set025/vendor
.PHONY: e2e_026
e2e_026: ## Run end-to-end tests for the fixture set 026 — Whitelisting classes and functions with pattern matching
e2e_026: bin/php-scoper.phar fixtures/set026/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix \
$(PHPBIN) $(PHPSCOPER) add-prefix \
--working-dir=fixtures/set026 \
--output-dir=../../build/set026 \
--force \
Expand All @@ -270,32 +270,26 @@ e2e_026: bin/php-scoper.phar fixtures/set026/vendor
.PHONY: tb
BLACKFIRE=blackfire
tb: ## Run Blackfire profiling
tb: vendor
rm -rf build
rm -rf vendor-bin/*/vendor

mv -f vendor tmp-back
composer install --no-dev --prefer-dist --classmap-authoritative

$(BLACKFIRE) --new-reference run $(PHPNOGC) bin/php-scoper add-prefix --output-dir=build/php-scoper --force --quiet

rm -rf vendor
mv -f tmp-back vendor
tb: bin/php-scoper.phar vendor
$(BLACKFIRE) --new-reference run $(PHPBIN) bin/php-scoper.phar add-prefix --output-dir=build/php-scoper --force --quiet

#
# Rules from files
#---------------------------------------------------------------------------

vendor: composer.lock
composer install
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
unset "COMPOSER_ROOT_VERSION"
touch $@

vendor/bamarni: composer.lock
composer install
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
unset "COMPOSER_ROOT_VERSION"
touch $@

bin/phpunit: composer.lock
composer install
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
unset "COMPOSER_ROOT_VERSION"
touch $@

vendor-bin/covers-validator/vendor: vendor-bin/covers-validator/composer.lock vendor/bamarni
Expand Down Expand Up @@ -404,7 +398,7 @@ bin/php-scoper.phar: bin/php-scoper src vendor scoper.inc.php box.json
box.json: box.json.dist
cat box.json.dist | sed -E 's/\"key\": \".+\",//g' | sed -E 's/\"algorithm\": \".+\",//g' > box.json

COVERS_VALIDATOR=$(PHPNOGC) vendor-bin/covers-validator/bin/covers-validator
COVERS_VALIDATOR=$(PHPBIN) vendor-bin/covers-validator/bin/covers-validator
clover.xml: src
$(COVERS_VALIDATOR)
phpdbg -qrr -d zend.enable_gc=0 $(PHPUNIT) \
Expand Down
37 changes: 16 additions & 21 deletions fixtures/set013/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
use Isolated\Symfony\Component\Finder\Finder;

return [
'global_namespace_whitelist' => [
'AppKernel',
function (string $className): bool {
return 'PHPUnit' === substr($className, 0, 6);
},
],
// The prefix configuration. If a non null value will be used, a random prefix will be generated.
'prefix' => null,

// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
// directory. You can however define which files should be scoped by defining a collection of Finders in the
Expand Down Expand Up @@ -45,25 +41,12 @@ function (string $className): bool {
// For more see: https://github.com/humbug/php-scoper#patchers
'patchers' => [
function (string $filePath, string $prefix, string $contents): string {
// Change the content here.
// Change the contents here.

return $contents;
},
],

// By default, PHP-Scoper only prefixes code where the namespace is non-global. In other words, non-namespaced
// code is not prefixed. This leaves the majority of classes, functions and constants in PHP - and most extensions,
// untouched.
//
// This is not necessarily a desirable outcome for vendor dependencies which are also not namespaced. To ensure
// they are isolated, you can configure the following which can be a list of strings or callables taking a string
// (the class name) as an argument and return a boolean (true meaning the class is going to prefixed).
//
// For more, see https://github.com/humbug/php-scoper#global-namespace-whitelisting
'global_namespace_whitelist' => [
'AppKernel',
],

// PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. However, you
// may want to share a common API between the bundled code of your PHAR and the consumer code. For example if
// you have a PHPUnit PHAR with isolated code, you still want the PHAR to be able to understand the
Expand All @@ -74,6 +57,18 @@ function (string $filePath, string $prefix, string $contents): string {
//
// Fore more see https://github.com/humbug/php-scoper#whitelist
'whitelist' => [
'PHPUnit\Framework\TestCase',
// 'PHPUnit\Framework\TestCase', // A specific class
// 'PHPUnit\Framework\*', // The whole namespace
// '*', // Everything
],

// If `true` then the user defined constants belonging to the global namespace will not be prefixed.
//
// For more see https://github.com/humbug/php-scoper#constants-from-the-global-namespace-whitelisting
'whitelist-global-constants' => true,

// If `true` then the user defined functions belonging to the global namespace will not be prefixed.
//
// For more see https://github.com/humbug/php-scoper#global-user-functions
'whitelist-global-functions' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
'whitelist-global-functions' => true,
],

[
'spec' => <<<'SPEC'
Constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace:
- prefix the use statement (cf. class belonging to the global scope tests)
- prefix the constant
SPEC
,
'Constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -51,13 +45,7 @@ class Foo
PHP
],

[
'spec' => <<<'SPEC'
FQ constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace:
- do not prefix the class (cf. class belonging to the global scope tests)
- do nothing
SPEC
,
'FQ constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -84,13 +72,7 @@ class X
PHP
],

[
'spec' => <<<'SPEC'
Constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace:
- prefix the use statement (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global whitelisted classes)
- transform the call into a FQ call
SPEC
,
'Constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -108,13 +90,7 @@ class X
PHP
],

[
'spec' => <<<'SPEC'
FQ constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace:
- prefix the use statement (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global whitelisted classes)
- do nothing
SPEC
,
'FQ constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
'whitelist-global-functions' => true,
],

[
'spec' => <<<'SPEC'
Constant call on a class which is imported via a use statement and which belongs to the global namespace:
- do not prefix the use statement (cf. class belonging to the global scope tests)
- transforms the call into a FQ call
SPEC
,
'Constant call on a class which is imported via a use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -51,13 +45,7 @@ class Command
PHP
],

[
'spec' => <<<'SPEC'
FQ constant call on a class which is imported via a use statement and which belongs to the global namespace:
- do not prefix the use statement (cf. class belonging to the global scope tests)
- do nothing
SPEC
,
'FQ constant call on a class which is imported via a use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -80,13 +68,7 @@ class Command
PHP
],

[
'spec' => <<<'SPEC'
Constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace:
- transform the call in a FQ call (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in
global whitelisted classes)
SPEC
,
'Constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand All @@ -104,13 +86,7 @@ class Command
PHP
],

[
'spec' => <<<'SPEC'
FQ constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace:
- prefix the class (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global
whitelisted classes)
SPEC
,
'FQ constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace' => [
'payload' => <<<'PHP'
<?php
Expand Down
Loading

0 comments on commit bc3061c

Please sign in to comment.