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\NodeTraverser; |
18
|
|
|
use Humbug\PhpScoper\NodeVisitor; |
19
|
|
|
use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection; |
20
|
|
|
use Humbug\PhpScoper\NodeVisitor\Collection\UseStmtCollection; |
21
|
|
|
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver; |
22
|
|
|
use Humbug\PhpScoper\Reflector; |
23
|
|
|
use PhpParser\NodeTraverserInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @final |
27
|
|
|
*/ |
28
|
|
|
class TraverserFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Functions for which the arguments will be prefixed. |
32
|
|
|
*/ |
33
|
|
|
public const WHITELISTED_FUNCTIONS = [ |
34
|
|
|
'class_exists', |
35
|
|
|
'interface_exists', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
private $reflector; |
39
|
|
|
|
40
|
344 |
|
public function __construct(Reflector $reflector) |
41
|
|
|
{ |
42
|
344 |
|
$this->reflector = $reflector; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $prefix Prefix to apply to the files. |
47
|
|
|
* @param string[] $whitelist List of classes to exclude from the scoping. |
48
|
|
|
* |
49
|
|
|
* @return NodeTraverserInterface |
50
|
|
|
*/ |
51
|
339 |
|
public function create(string $prefix, array $whitelist): NodeTraverserInterface |
52
|
|
|
{ |
53
|
339 |
|
$traverser = new NodeTraverser($prefix); |
54
|
|
|
|
55
|
339 |
|
$namespaceStatements = new NamespaceStmtCollection(); |
56
|
339 |
|
$useStatements = new UseStmtCollection(); |
57
|
|
|
|
58
|
339 |
|
$nameResolver = new FullyQualifiedNameResolver($namespaceStatements, $useStatements); |
59
|
|
|
|
60
|
339 |
|
$traverser->addVisitor(new NodeVisitor\AppendParentNode()); |
61
|
|
|
|
62
|
339 |
|
$traverser->addVisitor(new NodeVisitor\NamespaceStmtPrefixer($prefix, $namespaceStatements)); |
63
|
|
|
|
64
|
339 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements)); |
65
|
339 |
|
$traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $whitelist, $this->reflector)); |
66
|
|
|
|
67
|
339 |
|
$traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $nameResolver, $this->reflector)); |
68
|
339 |
|
$traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, self::WHITELISTED_FUNCTIONS, $whitelist, $this->reflector)); |
69
|
|
|
|
70
|
339 |
|
$traverser->addVisitor(new NodeVisitor\WhitelistedClassAppender($whitelist)); |
71
|
|
|
|
72
|
339 |
|
return $traverser; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|