|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework, IDE Helper |
|
4
|
|
|
* |
|
5
|
|
|
* @author Dmitry Mironov <[email protected]> |
|
6
|
|
|
* @licence MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\IdeHelper\Renderer; |
|
10
|
|
|
|
|
11
|
|
|
use Spiral\IdeHelper\Helpers; |
|
12
|
|
|
use Spiral\IdeHelper\Model\ClassProperty; |
|
13
|
|
|
use Spiral\Reactor\ClassDeclaration; |
|
14
|
|
|
use Spiral\Reactor\FileDeclaration; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ReactorBasedDocRenderer |
|
18
|
|
|
* |
|
19
|
|
|
* WARNING: this class only support rendering multiple classes in the same namespace. |
|
20
|
|
|
* |
|
21
|
|
|
* @package Spiral\IdeHelper\Renderer |
|
22
|
|
|
*/ |
|
23
|
|
|
class ReactorBasedDocRenderer implements RendererInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public function render(array $classes): string |
|
29
|
|
|
{ |
|
30
|
|
|
if (count($classes) === 0) { |
|
31
|
|
|
$declaration = new FileDeclaration(); |
|
32
|
|
|
|
|
33
|
|
|
return $declaration->render(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$namespace = $classes[0]->getNamespace() ?? ''; |
|
37
|
|
|
$fileDeclaration = new FileDeclaration($namespace); |
|
38
|
|
|
foreach ($classes as $class) { |
|
39
|
|
|
$classDeclaration = new ClassDeclaration($class->getShortName()); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($class->getMembers() as $member) { |
|
42
|
|
|
if ($member instanceof ClassProperty) { |
|
43
|
|
|
$classDeclaration->getComment()->addLine($this->renderProperty($member)); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$fileDeclaration->addElement($classDeclaration); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $fileDeclaration->render(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ClassProperty $property |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
private function renderProperty(ClassProperty $property): string |
|
58
|
|
|
{ |
|
59
|
|
|
$type = implode('|', array_map([Helpers::class, 'fqcn'], $property->getTypes())); |
|
60
|
|
|
|
|
61
|
|
|
return "@property {$type} \${$property->getName()}"; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|