|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace loophp\PhptreeAstGenerator\Exporter; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use loophp\phptree\Exporter\ExporterInterface; |
|
9
|
|
|
use loophp\phptree\Modifier\Apply; |
|
10
|
|
|
use loophp\phptree\Modifier\Filter; |
|
11
|
|
|
use loophp\phptree\Node\AttributeNodeInterface; |
|
12
|
|
|
use loophp\phptree\Node\NodeInterface; |
|
13
|
|
|
use Microsoft\PhpParser\Node; |
|
14
|
|
|
|
|
15
|
|
|
use function get_class; |
|
16
|
|
|
use function in_array; |
|
17
|
|
|
|
|
18
|
|
|
class MicrosoftFancyExporter implements ExporterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \loophp\phptree\Exporter\ExporterInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $exporter; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* MicrosoftFancyGenerator constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \loophp\phptree\Exporter\ExporterInterface $exporter |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ExporterInterface $exporter) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->exporter = $exporter; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function export(NodeInterface $node) |
|
39
|
|
|
{ |
|
40
|
|
|
$applyShapeModifier = new Apply(Closure::fromCallable([$this, 'applyShape'])); |
|
41
|
|
|
$applyLabelModifier = new Apply(Closure::fromCallable([$this, 'applyLabel'])); |
|
42
|
|
|
$filterModifier = new Filter(Closure::fromCallable([$this, 'filterObsoleteNode'])); |
|
43
|
|
|
$removeOriginalNode = new Apply(Closure::fromCallable([$this, 'removeOriginalNode'])); |
|
44
|
|
|
|
|
45
|
|
|
$node = $applyLabelModifier->modify($node); |
|
46
|
|
|
$node = $applyShapeModifier->modify($node); |
|
47
|
|
|
$node = $filterModifier->modify($node); |
|
48
|
|
|
$node = $removeOriginalNode->modify($node); |
|
49
|
|
|
|
|
50
|
|
|
return $this->exporter->export($node); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function applyLabel(AttributeNodeInterface $node): void |
|
54
|
|
|
{ |
|
55
|
|
|
$astNode = $node->getAttribute('astNode'); |
|
56
|
|
|
|
|
57
|
|
|
switch (true) { |
|
58
|
|
|
case $astNode instanceof Node\ClassBaseClause: |
|
59
|
|
|
case $astNode instanceof Node\QualifiedName: |
|
60
|
|
|
$node->setAttribute( |
|
61
|
|
|
'label', |
|
62
|
|
|
sprintf('%s', (string) $astNode) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
break; |
|
66
|
|
|
case $astNode instanceof Node\Statement\NamespaceDefinition: |
|
67
|
|
|
$node->setAttribute( |
|
68
|
|
|
'label', |
|
69
|
|
|
sprintf('Namespace %s', addslashes($node[0]->getAttribute('label'))) |
|
70
|
|
|
); |
|
71
|
|
|
unset($node[0]); |
|
72
|
|
|
|
|
73
|
|
|
break; |
|
74
|
|
|
case $astNode instanceof Node\Statement\ClassDeclaration: |
|
75
|
|
|
$node->setAttribute( |
|
76
|
|
|
'label', |
|
77
|
|
|
sprintf( |
|
78
|
|
|
'Class' |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function applyShape(AttributeNodeInterface $node): void |
|
87
|
|
|
{ |
|
88
|
|
|
$astNode = $node->getAttribute('astNode'); |
|
89
|
|
|
|
|
90
|
|
|
if (null === $astNode) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
switch (true) { |
|
95
|
|
|
default: |
|
96
|
|
|
$node->setAttribute('shape', 'rect'); |
|
97
|
|
|
|
|
98
|
|
|
break; |
|
99
|
|
|
case $astNode instanceof Node\Statement\NamespaceDefinition: |
|
100
|
|
|
$node->setAttribute('shape', 'house'); |
|
101
|
|
|
|
|
102
|
|
|
break; |
|
103
|
|
|
case $astNode instanceof Node\Statement\ClassDeclaration: |
|
104
|
|
|
$node->setAttribute('shape', 'invhouse'); |
|
105
|
|
|
|
|
106
|
|
|
break; |
|
107
|
|
|
case $astNode instanceof Node\MethodDeclaration: |
|
108
|
|
|
$node->setAttribute('shape', 'folder'); |
|
109
|
|
|
|
|
110
|
|
|
break; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function filterObsoleteNode(AttributeNodeInterface $node): bool |
|
115
|
|
|
{ |
|
116
|
|
|
$removeTypes = [ |
|
117
|
|
|
Node\Statement\NamespaceUseDeclaration::class, |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
return in_array(get_class($node->getAttribute('astNode')), $removeTypes, true); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function removeOriginalNode(AttributeNodeInterface $node): void |
|
124
|
|
|
{ |
|
125
|
|
|
$node->setAttribute('astNode', ''); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|