-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathBaseRenderer.php
302 lines (271 loc) · 8.92 KB
/
BaseRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\apidoc\renderers;
use Yii;
use yii\apidoc\helpers\ApiMarkdown;
use yii\apidoc\helpers\ApiMarkdownLaTeX;
use yii\apidoc\models\BaseDoc;
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\ConstDoc;
use yii\apidoc\models\Context;
use yii\apidoc\models\EventDoc;
use yii\apidoc\models\InterfaceDoc;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\PropertyDoc;
use yii\apidoc\models\TraitDoc;
use yii\apidoc\models\TypeDoc;
use yii\base\Component;
use yii\console\Controller;
/**
* Base class for all documentation renderers
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
abstract class BaseRenderer extends Component
{
/**
* @deprecated since 2.0.1 use [[$guidePrefix]] instead which allows configuring this options
*/
const GUIDE_PREFIX = 'guide-';
public $guidePrefix = 'guide-';
public $apiUrl;
/**
* @var string string to use as the title of the generated page.
*/
public $pageTitle;
/**
* @var Context the [[Context]] currently being rendered.
*/
public $apiContext;
/**
* @var Controller the apidoc controller instance. Can be used to control output.
*/
public $controller;
public $guideUrl;
/**
* @var string URL for the README to use for the index of the guide.
* @since 2.0.7
*/
public $readmeUrl;
/**
* @var string[]
*/
private $phpTypes = [
'callable',
'array',
'string',
'boolean',
'bool',
'integer',
'int',
'float',
'object',
'resource',
'null',
'false',
'true',
];
/**
* @var string[]
*/
private $phpTypeAliases = [
'true' => 'boolean',
'false' => 'boolean',
'bool' => 'boolean',
'int' => 'integer',
];
/**
* @var string[]
*/
private $phpTypeDisplayAliases = [
'bool' => 'boolean',
'int' => 'integer',
];
public function init()
{
ApiMarkdown::$renderer = $this;
ApiMarkdownLaTeX::$renderer = $this;
}
/**
* creates a link to a type (class, interface or trait)
* @param ClassDoc|InterfaceDoc|TraitDoc|ClassDoc[]|InterfaceDoc[]|TraitDoc[]|string|string[] $types
* @param BaseDoc|null $context
* @param string $title a title to be used for the link TODO check whether [[yii\...|Class]] is supported
* @param array $options additional HTML attributes for the link.
* @return string
*/
public function createTypeLink($types, $context = null, $title = null, $options = [])
{
if (!is_array($types)) {
$types = [$types];
}
if (count($types) > 1) {
$title = null;
}
$links = [];
foreach ($types as $type) {
$postfix = '';
if (is_string($type)) {
if (!empty($type) && substr_compare($type, '[]', -2, 2) === 0) {
$postfix = '[]';
$type = substr($type, 0, -2);
}
if ($type === '$this' && $context instanceof TypeDoc) {
$title = '$this';
$type = $context;
} elseif (($t = $this->apiContext->getType(ltrim($type, '\\'))) !== null) {
$type = $t;
} elseif (!empty($type) && $type[0] !== '\\' && ($t = $this->apiContext->getType($this->resolveNamespace($context) . '\\' . ltrim($type, '\\'))) !== null) {
$type = $t;
}
}
if (is_object($type) && method_exists($type, '__toString')) {
$type = (string) $type;
}
if (is_string($type)) {
$linkText = ltrim($type, '\\');
if ($title !== null) {
$linkText = $title;
$title = null;
}
// check if it is PHP internal class
if (((class_exists($type, false) || interface_exists($type, false) || trait_exists($type, false)) &&
($reflection = new \ReflectionClass($type)) && $reflection->isInternal())) {
$links[] = $this->generateLink($linkText, 'https://www.php.net/class.' . strtolower(ltrim($type, '\\')), $options) . $postfix;
} elseif (in_array($type, $this->phpTypes)) {
if (isset($this->phpTypeDisplayAliases[$type])) {
$linkText = $this->phpTypeDisplayAliases[$type];
}
if (isset($this->phpTypeAliases[$type])) {
$type = $this->phpTypeAliases[$type];
}
$links[] = $this->generateLink($linkText, 'https://www.php.net/language.types.' . strtolower(ltrim($type, '\\')), $options) . $postfix;
} else {
$links[] = $type . $postfix;
}
} elseif ($type instanceof BaseDoc) {
$linkText = $type->name;
if ($title !== null) {
$linkText = $title;
$title = null;
}
$links[] = $this->generateLink($linkText, $this->generateApiUrl($type->name), $options) . $postfix;
}
}
return implode('|', $links);
}
/**
* @param MethodDoc $method
* @param TypeDoc $type
* @return string
*/
public function createMethodReturnTypeLink($method, $type)
{
if (!($type instanceof ClassDoc) || $type->isAbstract) {
return $this->createTypeLink($method->returnTypes, $type);
}
$returnTypes = [];
foreach ($method->returnTypes as $returnType) {
if ($returnType !== 'static' && $returnType !== 'static[]') {
$returnTypes[] = $returnType;
continue;
}
$context = $this->apiContext;
if (isset($context->interfaces[$method->definedBy]) || isset($context->traits[$method->definedBy])) {
$replacement = $type->name;
} else {
$replacement = $method->definedBy;
}
$returnTypes[] = str_replace('static', $replacement, $returnType);
}
return $this->createTypeLink($returnTypes, $type);
}
/**
* creates a link to a subject
* @param PropertyDoc|MethodDoc|ConstDoc|EventDoc $subject
* @param string|null $title
* @param array $options additional HTML attributes for the link.
* @param TypeDoc|null $type
* @return string
*/
public function createSubjectLink($subject, $title = null, $options = [], $type = null)
{
if ($title === null) {
if ($subject instanceof MethodDoc) {
$title = $subject->name . '()';
} else {
$title = $subject->name;
}
}
if (!$type) {
$type = $this->apiContext->getType($subject->definedBy);
}
if (!$type) {
return $subject->name;
}
$link = $this->generateApiUrl($type->name) . '#' . $subject->name;
if ($subject instanceof MethodDoc) {
$link .= '()';
}
$link .= '-detail';
return $this->generateLink($title, $link, $options);
}
/**
* @param BaseDoc|string $context
* @return string
*/
private function resolveNamespace($context)
{
// TODO use phpdoc Context for this
if ($context === null) {
return '';
}
if ($context instanceof TypeDoc) {
return $context->namespace;
}
if ($context->hasProperty('definedBy')) {
$type = $this->apiContext->getType($context);
if ($type !== null) {
return $type->namespace;
}
}
return '';
}
/**
* generate link markup
* @param $text
* @param $href
* @param array $options additional HTML attributes for the link.
* @return mixed
*/
abstract protected function generateLink($text, $href, $options = []);
/**
* Generate an url to a type in apidocs
* @param $typeName
* @return mixed
*/
abstract public function generateApiUrl($typeName);
/**
* Generate an url to a guide page
* @param string $file
* @return string
*/
public function generateGuideUrl($file)
{
//skip parsing external url
if ((strpos($file, 'https://') !== false) || (strpos($file, 'http://') !== false) ) {
return $file;
}
$hash = '';
if (($pos = strpos($file, '#')) !== false) {
$hash = substr($file, $pos);
$file = substr($file, 0, $pos);
}
return rtrim($this->guideUrl, '/') . '/' . $this->guidePrefix . basename($file, '.md') . '.html' . $hash;
}
}