|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Scoper; |
|
16
|
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\NodeVisitor; |
|
18
|
|
|
use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection; |
|
19
|
|
|
use Humbug\PhpScoper\NodeVisitor\Collection\UseStmtCollection; |
|
20
|
|
|
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver; |
|
21
|
|
|
use PhpParser\NodeTraverser; |
|
22
|
|
|
use PhpParser\NodeTraverserInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @final |
|
26
|
|
|
*/ |
|
27
|
|
|
class TraverserFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Functions for which the arguments will be prefixed. |
|
31
|
|
|
*/ |
|
32
|
|
|
public const WHITELISTED_FUNCTIONS = [ |
|
33
|
|
|
'class_exists', |
|
34
|
|
|
'interface_exists', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $prefix Prefix to apply to the files. |
|
39
|
|
|
* @param string[] $whitelist List of classes to exclude from the scoping. |
|
40
|
|
|
* @param callable $globalWhitelister Closure taking a class name from the global namespace as an argument and |
|
41
|
|
|
* returning a boolean which if `true` means the class should be scoped |
|
42
|
|
|
* (i.e. is ignored) or scoped otherwise. |
|
43
|
|
|
* |
|
44
|
|
|
* @return NodeTraverserInterface |
|
45
|
|
|
*/ |
|
46
|
331 |
|
public function create(string $prefix, array $whitelist, callable $globalWhitelister): NodeTraverserInterface |
|
47
|
|
|
{ |
|
48
|
331 |
|
$traverser = new NodeTraverser(); |
|
49
|
|
|
|
|
50
|
331 |
|
$namespaceStatements = new NamespaceStmtCollection(); |
|
51
|
331 |
|
$useStatements = new UseStmtCollection(); |
|
52
|
|
|
|
|
53
|
331 |
|
$nameResolver = new FullyQualifiedNameResolver($namespaceStatements, $useStatements); |
|
54
|
|
|
|
|
55
|
331 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\GroupUseStmtTransformer()); |
|
56
|
|
|
|
|
57
|
331 |
|
$traverser->addVisitor(new NodeVisitor\AppendParentNode()); |
|
58
|
|
|
|
|
59
|
331 |
|
$traverser->addVisitor(new NodeVisitor\NamespaceStmtPrefixer($prefix, $namespaceStatements, $whitelist)); |
|
60
|
|
|
|
|
61
|
331 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements)); |
|
62
|
331 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $whitelist, $globalWhitelister)); |
|
63
|
|
|
|
|
64
|
331 |
|
$traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $whitelist, $globalWhitelister, $nameResolver)); |
|
65
|
331 |
|
$traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, self::WHITELISTED_FUNCTIONS, $whitelist, $globalWhitelister, $nameResolver)); |
|
66
|
|
|
|
|
67
|
331 |
|
$traverser->addVisitor(new NodeVisitor\WhitelistedClassAppender($whitelist)); |
|
68
|
|
|
|
|
69
|
331 |
|
return $traverser; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|