Skip to content

Commit df9bcf5

Browse files
authored
Allow to whitelist a namespace (#213)
Closes #192 Allow to whitelist a complete namespace with the `*` notation, e.g. `*` (for the global namespace) or `Acme\*` **[BC break]:** the API signature has changed to convert the `array` `$whitelist` into the `Whitelist` object
1 parent 101faed commit df9bcf5

35 files changed

+1350
-98
lines changed

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
3838
- [Finders and paths](#finders-and-paths)
3939
- [Patchers](#patchers)
4040
- [Whitelist][whitelist]
41+
- [Class Whitelisting](#class-whitelisting)
42+
- [Namespace Whitelisting](#namespace-whitelisting)
4143
- [Building A Scoped PHAR](#building-a-scoped-phar)
4244
- [With Box](#with-box)
4345
- [Step 1: Configure build location and prep vendors](#step-1-configure-build-location-and-prep-vendors)
@@ -258,7 +260,10 @@ the bundled code of your PHAR and the consumer code. For example if you have
258260
a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
259261
understand the `PHPUnit\Framework\TestCase` class.
260262

261-
A way to achieve this is by specifying a list of classes to not prefix:
263+
264+
### Class whitelisting
265+
266+
You can whitelist classes and interfaces like so:
262267

263268
```php
264269
<?php declare(strict_types=1);
@@ -272,11 +277,44 @@ return [
272277
];
273278
```
274279

275-
Note that only classes are whitelisted, this does not affect constants
276-
or functions.
280+
Note that only classes are whitelisted, this does not affect constants,
281+
functions or traits. This whitelisting will actually not prevent the
282+
scoping to operate, i.e. the class or interface will still be prefixed,
283+
but a `class_alias()` statement will be registered pointing the prefixed
284+
class to the non-prefixed one.
285+
286+
287+
### Namespace whitelisting
288+
289+
If you want to be more generic and whitelist a whole namespace, you can
290+
do it so like this:
291+
292+
```php
293+
<?php declare(strict_types=1);
294+
295+
// scoper.inc.php
277296

278-
For whitelist to work, you then require to load `vendor/scoper-autoload.php`
279-
instead of the traditional `vendor/autoload.php`.
297+
return [
298+
'whitelist' => [
299+
'PHPUnit\Framework\*',
300+
],
301+
];
302+
```
303+
304+
Now anything under the `PHPUnit\Framework` namespace will not be prefixed.
305+
Note this works as well for the global namespace:
306+
307+
```php
308+
<?php declare(strict_types=1);
309+
310+
// scoper.inc.php
311+
312+
return [
313+
'whitelist' => [
314+
'*',
315+
],
316+
];
317+
```
280318

281319

282320
## Building A Scoped PHAR

specs/class-const/global-scope-single-level.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ class Command
4343
}
4444
\Humbug\Command::MAIN_CONST;
4545

46+
PHP
47+
],
48+
49+
'Constant call on a class belonging to the global namespace which is whitelisted: add root namespace statement' => [
50+
'whitelist' => ['\*'],
51+
'payload' => <<<'PHP'
52+
<?php
53+
54+
class Command {}
55+
56+
Command::MAIN_CONST;
57+
----
58+
<?php
59+
60+
namespace {
61+
class Command
62+
{
63+
}
64+
\Command::MAIN_CONST;
65+
}
66+
4667
PHP
4768
],
4869

specs/class-const/namespace-scope-two-level.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,74 @@ class Command
118118
119119
\Humbug\X\PHPUnit\Command::MAIN_CONST;
120120

121+
PHP
122+
],
123+
124+
[
125+
'spec' => <<<'SPEC'
126+
Constant call on a namespaced class belonging to a whitelisted namespace:
127+
- prefix the namespace
128+
- prefix the class
129+
- transforms the call into a FQ call to avoid autoloading issues
130+
SPEC
131+
,
132+
'whitelist' => ['X\PHPUnit\*'],
133+
'payload' => <<<'PHP'
134+
<?php
135+
136+
namespace X\PHPUnit {
137+
class Command {}
138+
}
139+
140+
namespace X {
141+
PHPUnit\Command::MAIN_CONST;
142+
}
143+
----
144+
<?php
145+
146+
namespace X\PHPUnit;
147+
148+
class Command
149+
{
150+
}
151+
namespace Humbug\X;
152+
153+
\X\PHPUnit\Command::MAIN_CONST;
154+
155+
PHP
156+
],
157+
158+
[
159+
'spec' => <<<'SPEC'
160+
Constant call on a namespaced class belonging to a whitelisted namespace (2):
161+
- prefix the namespace
162+
- prefix the class
163+
- transforms the call into a FQ call to avoid autoloading issues
164+
SPEC
165+
,
166+
'whitelist' => ['\*'],
167+
'payload' => <<<'PHP'
168+
<?php
169+
170+
namespace X\PHPUnit {
171+
class Command {}
172+
}
173+
174+
namespace X {
175+
PHPUnit\Command::MAIN_CONST;
176+
}
177+
----
178+
<?php
179+
180+
namespace X\PHPUnit;
181+
182+
class Command
183+
{
184+
}
185+
namespace X;
186+
187+
\X\PHPUnit\Command::MAIN_CONST;
188+
121189
PHP
122190
],
123191

specs/class-static-prop/global-scope-single-level.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ class Command
4343
}
4444
\Humbug\Command::$mainStaticProp;
4545

46+
PHP
47+
],
48+
49+
'Constant call on a class belonging to the global namespace which is whitelisted: add root namespace statement' => [
50+
'whitelist' => ['\*'],
51+
'payload' => <<<'PHP'
52+
<?php
53+
54+
class Command {}
55+
56+
Command::$mainStaticProp;
57+
----
58+
<?php
59+
60+
namespace {
61+
class Command
62+
{
63+
}
64+
\Command::$mainStaticProp;
65+
}
66+
4667
PHP
4768
],
4869

0 commit comments

Comments
 (0)