|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.1 |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhUml\Parser\Raw; |
|
9
|
|
|
|
|
10
|
|
|
use PhpParser\NodeTraverser; |
|
11
|
|
|
use PhpParser\Parser; |
|
12
|
|
|
use PhpParser\ParserFactory; |
|
13
|
|
|
use PhUml\Parser\CodeFinder; |
|
14
|
|
|
use PhUml\Parser\Raw\Visitors\ClassVisitor; |
|
15
|
|
|
use PhUml\Parser\Raw\Visitors\InterfaceVisitor; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* It traverses the AST of all the files and interfaces found by the `CodeFinder` and builds a |
|
19
|
|
|
* `RawDefinitions` object |
|
20
|
|
|
* |
|
21
|
|
|
* In order to create the collection of raw definitions it uses two visitors |
|
22
|
|
|
* |
|
23
|
|
|
* - The `ClassVisitor` which builds `RawDefinitions` for classes |
|
24
|
|
|
* - The `InterfaceVisitor` which builds `RawDefinitions` for interfaces |
|
25
|
|
|
* |
|
26
|
|
|
* It will call the `ExternalDefinitionsResolver` to add generic `RawDefinition`s for classes and |
|
27
|
|
|
* interfaces that do not belong directly to the current codebase. |
|
28
|
|
|
* These external definitions are either built-in or from third party libraries |
|
29
|
|
|
*/ |
|
30
|
|
|
class TokenParser |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var Parser */ |
|
33
|
|
|
private $parser; |
|
34
|
|
|
|
|
35
|
|
|
/** @var NodeTraverser */ |
|
36
|
|
|
private $traverser; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ExternalDefinitionsResolver */ |
|
39
|
|
|
private $resolver; |
|
40
|
|
|
|
|
41
|
|
|
/** @var RawDefinitions */ |
|
42
|
|
|
private $definitions; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
Parser $parser = null, |
|
46
|
|
|
NodeTraverser $traverser = null, |
|
47
|
|
|
ExternalDefinitionsResolver $resolver = null |
|
48
|
|
|
) { |
|
49
|
|
|
$this->definitions = new RawDefinitions(); |
|
50
|
|
|
$this->parser = $parser ?? (new ParserFactory)->create(ParserFactory::PREFER_PHP5); |
|
51
|
|
|
$this->traverser = $traverser ?? $this->defaultTraverser(); |
|
52
|
|
|
$this->resolver = $resolver ?? new ExternalDefinitionsResolver(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function parse(CodeFinder $finder): RawDefinitions |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($finder->files() as $code) { |
|
58
|
|
|
$this->traverser->traverse($this->parser->parse($code)); |
|
59
|
|
|
} |
|
60
|
|
|
$this->resolver->resolve($this->definitions); |
|
61
|
|
|
return $this->definitions; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function defaultTraverser(): NodeTraverser |
|
65
|
|
|
{ |
|
66
|
|
|
$traverser = new NodeTraverser(); |
|
67
|
|
|
$traverser->addVisitor(new ClassVisitor($this->definitions)); |
|
68
|
|
|
$traverser->addVisitor(new InterfaceVisitor($this->definitions)); |
|
69
|
|
|
return $traverser; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|