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\PhpParser\NodeVisitor\Collection\NamespaceStmtCollection; |
18
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\UseStmtCollection; |
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver; |
20
|
|
|
use Humbug\PhpScoper\Reflector; |
21
|
|
|
use PhpParser\NodeTraverserInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @private |
25
|
|
|
*/ |
26
|
|
|
class TraverserFactory |
27
|
|
|
{ |
28
|
|
|
private $reflector; |
29
|
|
|
|
30
|
377 |
|
public function __construct(Reflector $reflector) |
31
|
|
|
{ |
32
|
377 |
|
$this->reflector = $reflector; |
33
|
|
|
} |
34
|
|
|
|
35
|
372 |
|
public function create(string $prefix, array $whitelist): NodeTraverserInterface |
36
|
|
|
{ |
37
|
372 |
|
$traverser = new NodeTraverser($prefix); |
38
|
|
|
|
39
|
372 |
|
$namespaceStatements = new NamespaceStmtCollection(); |
40
|
372 |
|
$useStatements = new UseStmtCollection(); |
41
|
|
|
|
42
|
372 |
|
$nameResolver = new FullyQualifiedNameResolver($namespaceStatements, $useStatements); |
43
|
|
|
|
44
|
372 |
|
$traverser->addVisitor(new NodeVisitor\AppendParentNode()); |
45
|
|
|
|
46
|
372 |
|
$traverser->addVisitor(new NodeVisitor\NamespaceStmtPrefixer($prefix, $namespaceStatements)); |
47
|
|
|
|
48
|
372 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements)); |
49
|
372 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $this->reflector)); |
50
|
|
|
|
51
|
372 |
|
$traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $nameResolver, $this->reflector)); |
52
|
372 |
|
$traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, $this->reflector)); |
53
|
|
|
|
54
|
372 |
|
$traverser->addVisitor(new NodeVisitor\WhitelistedClassAppender($whitelist)); |
55
|
|
|
|
56
|
372 |
|
return $traverser; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|