Skip to content

Commit 4f40b5a

Browse files
authored
Switch to the whitelist class for the autoload generator (#216)
1 parent 33c1b2e commit 4f40b5a

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ The class aliasing mechanism is done like follows:
288288
which will ensure the `class_alias()` statement is executed
289289

290290
It is done this way to ensure prefixed and whitelisted classes can co-exist together without breaking the autoloading.
291+
The `class_exists()` statements are dumped in `vendor/scoper-autoload.php`, do not forget to include this file in favour
292+
of `vendor/autoload.php`. This part is however sorted out by [Box][box] if you are using it with the
293+
[`PhpScoper` compactor][php-scoper-integration].
291294

292295
The constant aliasing mechanism is done by transforming the constant declaration into a `define()` statement when this
293296
is not already the case. Note that there is a difference here since `define()` defines a constant at runtime whereas

src/Autoload/ScoperAutoloadGenerator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
namespace Humbug\PhpScoper\Autoload;
1616

17+
use Humbug\PhpScoper\Whitelist;
1718
use function array_map;
1819

1920
final class ScoperAutoloadGenerator
2021
{
2122
private $whitelist;
2223

23-
/**
24-
* @param string[] $whitelist
25-
*/
26-
public function __construct(array $whitelist)
24+
public function __construct(Whitelist $whitelist)
2725
{
2826
$this->whitelist = $whitelist;
2927
}
@@ -61,7 +59,7 @@ function (string $whitelistedElement) use ($prefix): string {
6159
$whitelistedElement
6260
);
6361
},
64-
$this->whitelist
62+
$this->whitelist->getClassWhitelistArray()
6563
);
6664
}
6765
}

src/Whitelist.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public function isClassWhitelisted(string $name): bool
7979
return in_array($name, $this->classes, true);
8080
}
8181

82+
/**
83+
* @return string[]
84+
*/
85+
public function getClassWhitelistArray(): array
86+
{
87+
return $this->classes;
88+
}
89+
8290
public function isNamespaceWhitelisted(string $name): bool
8391
{
8492
foreach ($this->namespaces as $namespace) {

tests/Autoload/ScoperAutoloadGeneratorTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
namespace Humbug\PhpScoper\Autoload;
1616

17+
use Humbug\PhpScoper\Whitelist;
1718
use PHPUnit\Framework\TestCase;
1819

1920
class ScoperAutoloadGeneratorTest extends TestCase
2021
{
2122
public function test_generate_the_autoload()
2223
{
23-
$whitelist = [
24-
'A\Foo',
25-
'B\Bar',
26-
];
24+
$whitelist = Whitelist::create('A\Foo', 'B\Bar');
2725

2826
$prefix = 'Humbug';
2927

tests/WhitelistTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public function test_it_can_be_created_from_a_list_of_strings(
3434

3535
$whitelistReflection = new ReflectionClass(Whitelist::class);
3636

37-
$whitelistClassReflection = $whitelistReflection->getProperty('classes');
38-
$whitelistClassReflection->setAccessible(true);
39-
$actualClasses = $whitelistClassReflection->getValue($whitelistObject);
37+
$actualClasses = $whitelistObject->getClassWhitelistArray();
4038

4139
$whitelistNamespaceReflection = $whitelistReflection->getProperty('namespaces');
4240
$whitelistNamespaceReflection->setAccessible(true);

0 commit comments

Comments
 (0)