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\PhpParser; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Configuration\SymbolsConfiguration; |
18
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\NamespaceStmt\NamespaceStmtCollection; |
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver; |
20
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\UseStmt\UseStmtCollection; |
21
|
|
|
use Humbug\PhpScoper\Reflector; |
22
|
|
|
use Humbug\PhpScoper\Scoper\PhpScoper; |
23
|
|
|
use Humbug\PhpScoper\Symbol\EnrichedReflector; |
24
|
|
|
use Humbug\PhpScoper\Symbol\SymbolsRegistry; |
25
|
|
|
use PhpParser\NodeTraverserInterface; |
26
|
|
|
use PhpParser\NodeVisitor as PhpParserNodeVisitor; |
27
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @private |
31
|
|
|
*/ |
32
|
555 |
|
class TraverserFactory |
33
|
|
|
{ |
34
|
555 |
|
private EnrichedReflector $reflector; |
35
|
|
|
|
36
|
|
|
public function __construct(EnrichedReflector $reflector) |
37
|
550 |
|
{ |
38
|
|
|
$this->reflector = $reflector; |
39
|
550 |
|
} |
40
|
|
|
|
41
|
550 |
|
public function create( |
42
|
550 |
|
PhpScoper $scoper, |
43
|
|
|
string $prefix, |
44
|
550 |
|
SymbolsRegistry $symbolsRegistry |
45
|
|
|
): NodeTraverserInterface |
46
|
550 |
|
{ |
47
|
|
|
$traverser = new NodeTraverser(); |
48
|
550 |
|
|
49
|
|
|
$namespaceStatements = new NamespaceStmtCollection(); |
50
|
550 |
|
$useStatements = new UseStmtCollection(); |
51
|
550 |
|
|
52
|
|
|
$nameResolver = new NameResolver( |
53
|
550 |
|
null, |
54
|
550 |
|
['preserveOriginalNames' => true], |
55
|
550 |
|
); |
56
|
550 |
|
$identifierResolver = new IdentifierResolver($nameResolver); |
57
|
550 |
|
$stringNodePrefixer = new StringNodePrefixer($scoper); |
58
|
550 |
|
|
59
|
|
|
self::addVisitors( |
60
|
550 |
|
$traverser, |
61
|
550 |
|
[ |
62
|
|
|
$nameResolver, |
63
|
550 |
|
new NodeVisitor\ParentNodeAppender(), |
64
|
|
|
new NodeVisitor\IdentifierNameAppender($identifierResolver), |
65
|
|
|
|
66
|
|
|
new NodeVisitor\NamespaceStmt\NamespaceStmtPrefixer( |
67
|
|
|
$prefix, |
68
|
|
|
$this->reflector, |
69
|
|
|
$namespaceStatements, |
70
|
|
|
), |
71
|
|
|
|
72
|
|
|
new NodeVisitor\UseStmt\UseStmtCollector( |
73
|
|
|
$namespaceStatements, |
74
|
|
|
$useStatements, |
75
|
|
|
), |
76
|
|
|
new NodeVisitor\UseStmt\UseStmtPrefixer( |
77
|
|
|
$prefix, |
78
|
|
|
$this->reflector, |
79
|
|
|
), |
80
|
|
|
|
81
|
|
|
new NodeVisitor\NamespaceStmt\FunctionIdentifierRecorder( |
82
|
|
|
$prefix, |
83
|
|
|
$identifierResolver, |
84
|
|
|
$symbolsRegistry, |
85
|
|
|
$this->reflector, |
86
|
|
|
), |
87
|
|
|
new NodeVisitor\ClassIdentifierRecorder( |
88
|
|
|
$prefix, |
89
|
|
|
$identifierResolver, |
90
|
|
|
$symbolsRegistry, |
91
|
|
|
$this->reflector, |
92
|
|
|
), |
93
|
|
|
new NodeVisitor\NameStmtPrefixer( |
94
|
|
|
$prefix, |
95
|
|
|
$namespaceStatements, |
96
|
|
|
$useStatements, |
97
|
|
|
$this->reflector, |
98
|
|
|
), |
99
|
|
|
new NodeVisitor\StringScalarPrefixer( |
100
|
|
|
$prefix, |
101
|
|
|
$this->reflector, |
102
|
|
|
), |
103
|
|
|
new NodeVisitor\NewdocPrefixer($stringNodePrefixer), |
104
|
|
|
new NodeVisitor\EvalPrefixer($stringNodePrefixer), |
105
|
|
|
|
106
|
|
|
new NodeVisitor\ClassAliasStmtAppender( |
107
|
|
|
$prefix, |
108
|
|
|
$this->reflector, |
109
|
|
|
$identifierResolver, |
110
|
|
|
), |
111
|
|
|
new NodeVisitor\MultiConstStmtReplacer(), |
112
|
|
|
new NodeVisitor\ConstStmtReplacer( |
113
|
|
|
$identifierResolver, |
114
|
|
|
$this->reflector, |
115
|
|
|
), |
116
|
|
|
], |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $traverser; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param PhpParserNodeVisitor[] $nodeVisitors |
124
|
|
|
*/ |
125
|
|
|
private static function addVisitors( |
126
|
|
|
NodeTraverserInterface $nodeTraverser, |
127
|
|
|
array $nodeVisitors |
128
|
|
|
): void |
129
|
|
|
{ |
130
|
|
|
foreach ($nodeVisitors as $nodeVisitor) { |
131
|
|
|
$nodeTraverser->addVisitor($nodeVisitor); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|