1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Julius Härtl <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace JuliusHaertl\PHPDocToRst\Builder; |
25
|
|
|
|
26
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Param; |
27
|
|
|
use phpDocumentor\Reflection\Php\Argument; |
28
|
|
|
use phpDocumentor\Reflection\Php\Class_; |
29
|
|
|
|
30
|
|
|
class ClassFileBuilder extends FileBuilder { |
31
|
|
|
|
32
|
|
|
protected function render() { |
33
|
|
|
|
34
|
|
|
/** @var Class_ $class */ |
35
|
|
|
$class = $this->element; |
36
|
|
|
|
37
|
|
|
if (!$this->shouldRenderElement($class)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$docBlock = $class->getDocBlock(); |
42
|
|
|
|
43
|
|
|
$this->addH1(self::escape($class->getName())); |
44
|
|
|
|
45
|
|
|
$namespace = substr($class->getFqsen(), 0, strlen($class->getFqsen())-strlen('\\' . $class->getName())); |
46
|
|
|
if($namespace !== '') { |
47
|
|
|
$this->beginPhpDomain('namespace', substr($namespace, 1), false); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$modifiers = $class->isAbstract() ? ' abstract' : ''; |
51
|
|
|
$modifiers = $class->isFinal() ? ' final' : $modifiers; |
52
|
|
|
if ($modifiers !== '') { |
53
|
|
|
$this->addLine('.. rst-class:: ' . $modifiers)->addLine(); |
54
|
|
|
} |
55
|
|
|
$this->beginPhpDomain('class', $class->getName(), false); |
56
|
|
|
|
57
|
|
|
$this->indent(); |
58
|
|
|
$this->addDocBlockDescription($docBlock); |
59
|
|
|
|
60
|
|
|
if($class instanceof Class_) { |
61
|
|
|
// Add class details |
62
|
|
|
$parent = $class->getParent(); |
63
|
|
|
if ($parent !== null) { |
64
|
|
|
$this->addFieldList('Extends', $parent !== null ? $this->getLink('class', $parent) : ''); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
$implementedInterfaces = ''; |
68
|
|
|
foreach ($class->getInterfaces() as $int) { |
69
|
|
|
$implementedInterfaces .= $this->getLink('interface', $int) . ' '; |
70
|
|
|
} |
71
|
|
|
if ($implementedInterfaces !== '') { |
72
|
|
|
$this->addFieldList('Implements', $implementedInterfaces); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$usedTraits = ''; |
76
|
|
|
foreach ($class->getUsedTraits() as $trait) { |
77
|
|
|
$usedTraits .= $this->getLink('trait', $trait) . ' '; |
78
|
|
|
} |
79
|
|
|
if ($usedTraits !== '') { |
80
|
|
|
$this->addFieldList('Used traits', $usedTraits); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->unindent(); |
84
|
|
|
$this->addLine(); |
85
|
|
|
$this->addLine(); |
86
|
|
|
|
87
|
|
|
// Add class constants |
88
|
|
|
if (count($class->getConstants()) > 0) { |
89
|
|
|
$this->addH2('Constants'); |
90
|
|
|
foreach ($class->getConstants() as $constant) { |
91
|
|
|
if ($this->shouldRenderElement($constant)) { |
92
|
|
|
$this->addConstant($constant); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->addProperties($class->getProperties()); |
98
|
|
|
|
99
|
|
|
if (count($class->getMethods()) > 0) { |
100
|
|
|
$this->addH2('Methods'); |
101
|
|
|
/* Render methods of a class */ |
102
|
|
|
foreach ($class->getMethods() as $method) { |
103
|
|
|
if (!$this->shouldRenderElement($method)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$docBlock = $method->getDocBlock(); |
107
|
|
|
$params = []; |
108
|
|
|
if ($docBlock !== null) { |
109
|
|
|
/** @var Param $param */ |
110
|
|
|
foreach ($docBlock->getTagsByName('param') as $param) { |
111
|
|
|
$params[$param->getVariableName()] = $param; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
$args = ''; |
115
|
|
|
/** @var Argument $argument */ |
116
|
|
|
foreach ($method->getArguments() as $argument) { |
117
|
|
|
// TODO: defaults, types |
118
|
|
|
$args .= ' $' . $argument->getName() . ', '; |
119
|
|
|
} |
120
|
|
|
$args = substr($args, 0, -2); |
121
|
|
|
|
122
|
|
|
$modifiers = $method->getVisibility(); |
123
|
|
|
$modifiers .= $method->isAbstract() ? ' abstract' : ''; |
124
|
|
|
$modifiers .= $method->isFinal() ? ' final' : ''; |
125
|
|
|
$modifiers .= $method->isStatic() ? ' static' : ''; |
126
|
|
|
$this->addLine('.. rst-class:: ' . $modifiers)->addLine(); |
127
|
|
|
$this->indent(); |
128
|
|
|
$this->beginPhpDomain('method', $method->getName() . '(' . $args . ')'); |
129
|
|
|
$this->addDocBlockDescription($docBlock); |
130
|
|
|
$this->addLine(); |
131
|
|
View Code Duplication |
if (!empty($params)) { |
|
|
|
|
132
|
|
|
foreach ($method->getArguments() as $argument) { |
133
|
|
|
/** @var Param $param */ |
134
|
|
|
$param = $params[$argument->getName()]; |
135
|
|
|
if ($param !== null) $this->addMultiline(':param ' . self::escape($param->getType()) . ' $' . $argument->getName() . ': ' . $param->getDescription(), true); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
$this->endPhpDomain('method'); |
139
|
|
|
$this->unindent(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
$this->endPhpDomain(); //class |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |