1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Templating\Filters; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Generator\Resolvers\ElementResolverInterface; |
6
|
|
|
use ApiGen\Contracts\Generator\SourceCodeHighlighter\SourceCodeHighlighterInterface; |
7
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
8
|
|
|
use ApiGen\Contracts\Parser\Reflection\ElementReflectionInterface; |
9
|
|
|
use ApiGen\Contracts\Parser\Reflection\FunctionReflectionInterface; |
10
|
|
|
use ApiGen\Event\ProcessDocTextEvent; |
11
|
|
|
use ApiGen\Templating\Filters\Helpers\ElementLinkFactory; |
12
|
|
|
use ApiGen\Templating\Filters\Helpers\LinkBuilder; |
13
|
|
|
use ApiGen\Templating\Filters\Helpers\Strings; |
14
|
|
|
use Latte\Runtime\Filters as LatteFilters; |
15
|
|
|
use Nette\Utils\Validators; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
|
18
|
|
|
final class UrlFilters extends Filters |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SourceCodeHighlighterInterface |
22
|
|
|
*/ |
23
|
|
|
private $highlighter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ElementResolverInterface |
27
|
|
|
*/ |
28
|
|
|
private $elementResolver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LinkBuilder |
32
|
|
|
*/ |
33
|
|
|
private $linkBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ElementLinkFactory |
37
|
|
|
*/ |
38
|
|
|
private $elementLinkFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var EventDispatcherInterface |
42
|
|
|
*/ |
43
|
|
|
private $eventDispatcher; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
SourceCodeHighlighterInterface $highlighter, |
47
|
|
|
ElementResolverInterface $elementResolver, |
48
|
|
|
LinkBuilder $linkBuilder, |
49
|
|
|
ElementLinkFactory $elementLinkFactory, |
50
|
|
|
EventDispatcherInterface $eventDispatcher |
51
|
|
|
) { |
52
|
|
|
$this->highlighter = $highlighter; |
53
|
|
|
$this->elementResolver = $elementResolver; |
54
|
|
|
$this->linkBuilder = $linkBuilder; |
55
|
|
|
$this->elementLinkFactory = $elementLinkFactory; |
56
|
|
|
$this->eventDispatcher = $eventDispatcher; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Tries to parse a definition of a class/method/property/constant/function |
61
|
|
|
* and returns the appropriate link if successful. |
62
|
|
|
*/ |
63
|
|
|
public function resolveLink(string $definition, ElementReflectionInterface $reflectionElement): ?string |
64
|
|
|
{ |
65
|
|
|
if (empty($definition)) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$suffix = ''; |
70
|
|
|
if (substr($definition, -2) === '[]') { |
71
|
|
|
$definition = substr($definition, 0, -2); |
72
|
|
|
$suffix = '[]'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$element = $this->elementResolver->resolveElement($definition, $reflectionElement, $expectedName); |
76
|
|
|
if ($element === null || $element instanceof FunctionReflectionInterface) { |
77
|
|
|
return $expectedName; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$classes = []; |
81
|
|
|
if ($element->isDeprecated()) { |
82
|
|
|
$classes[] = 'deprecated'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$link = $this->elementLinkFactory->createForElement($element, $classes); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return '<code>' . $link . $suffix . '</code>'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function annotation(string $value, string $name, ElementReflectionInterface $reflectionElement): string |
91
|
|
|
{ |
92
|
|
|
$annotationProcessors = [ |
93
|
|
|
'return' => $this->processReturnAnnotations($value, $reflectionElement), |
94
|
|
|
'throws' => $this->processThrowsAnnotations($value, $reflectionElement), |
95
|
|
|
'license' => $this->processLicenseAnnotations($value), |
96
|
|
|
'link' => $this->processLinkAnnotations($value), |
97
|
|
|
'see' => $this->processSeeAnnotations($value, $reflectionElement), |
98
|
|
|
'uses' => $this->processUsesAnnotations($value, $reflectionElement), |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if (isset($annotationProcessors[$name])) { |
102
|
|
|
return $annotationProcessors[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->doc($value, $reflectionElement); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns links for types. |
110
|
|
|
*/ |
111
|
|
|
public function typeLinks(string $annotation, ElementReflectionInterface $reflectionElement): string |
112
|
|
|
{ |
113
|
|
|
$links = []; |
114
|
|
|
|
115
|
|
|
// typehints can not contain spaces |
116
|
|
|
// valid typehint is: |
117
|
|
|
// [TYPE[|TYPE[|...]][SPACE[METHOD|PARAM][DESCRIPTION]] |
|
|
|
|
118
|
|
|
$parts = explode(' ', $annotation); |
119
|
|
|
|
120
|
|
|
foreach (explode('|', $parts[0]) as $type) { |
121
|
|
|
$type = $this->getTypeName($type, false); |
122
|
|
|
$links[] = $this->resolveLink($type, $reflectionElement) ?: LatteFilters::escapeHtml(ltrim($type, '\\')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return implode('|', $links); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function annotationDescription(string $annotation, ElementReflectionInterface $reflectionElement): string |
129
|
|
|
{ |
130
|
|
|
$description = trim(strpbrk($annotation, "\n\r\t $")) ?: $annotation; |
131
|
|
|
return $this->doc($description, $reflectionElement); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function description(ElementReflectionInterface $element): string |
135
|
|
|
{ |
136
|
|
|
$long = $element->getDescription(); |
137
|
|
|
|
138
|
|
|
// Merge lines |
139
|
|
|
$long = preg_replace_callback('~(?:<(code|pre)>.+?</\1>)|([^<]*)~s', function ($matches) { |
140
|
|
|
return ! empty($matches[2]) |
141
|
|
|
? preg_replace('~\n(?:(\s+\n){2,})+~', ' ', $matches[2]) |
142
|
|
|
: $matches[0]; |
143
|
|
|
}, $long); |
144
|
|
|
|
145
|
|
|
return $this->doc($long, $element); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function doc(string $text, ElementReflectionInterface $reflectionElement): string |
149
|
|
|
{ |
150
|
|
|
$text = $this->resolveInternalAnnotation($text); |
151
|
|
|
|
152
|
|
|
$processDocTextEvent = new ProcessDocTextEvent($text, $reflectionElement); |
153
|
|
|
$this->eventDispatcher->dispatch(ProcessDocTextEvent::class, $processDocTextEvent); |
154
|
|
|
|
155
|
|
|
return $this->resolveLinkAndSeeAnnotation($processDocTextEvent->getText(), $reflectionElement); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function resolveInternalAnnotation(string $text): string |
159
|
|
|
{ |
160
|
|
|
$pattern = '~\\{@(\\w+)(?:(?:\\s+((?>(?R)|[^{}]+)*)\\})|\\})~'; |
161
|
|
|
return preg_replace_callback($pattern, function ($matches) { |
162
|
|
|
if ($matches[1] !== 'internal') { |
163
|
|
|
return $matches[0]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return ''; |
167
|
|
|
}, $text); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function resolveLinkAndSeeAnnotation(string $text, ElementReflectionInterface $reflectionElement): string |
171
|
|
|
{ |
172
|
|
|
return preg_replace_callback('~{@(?:link|see)\\s+([^}]+)}~', function ($matches) use ($reflectionElement) { |
173
|
|
|
[$url, $description] = Strings::split($matches[1]); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$link = $this->resolveLink($matches[1], $reflectionElement); |
176
|
|
|
if ($link) { |
|
|
|
|
177
|
|
|
return $link; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (Validators::isUri($url)) { |
181
|
|
|
return $this->linkBuilder->build($url, $description ?: $url); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $matches[1]; |
185
|
|
|
}, $text); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function highlightPhp(string $source, ElementReflectionInterface $reflectionElement): string |
189
|
|
|
{ |
190
|
|
|
return $this->resolveLink($this->getTypeName($source), $reflectionElement) |
191
|
|
|
?: $this->highlighter->highlight((string) $source); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function highlightValue(string $definition, ElementReflectionInterface $reflectionElement): string |
195
|
|
|
{ |
196
|
|
|
return $this->highlightPhp(preg_replace('~^(?:[ ]{4}|\t)~m', '', $definition), $reflectionElement); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function processReturnAnnotations(string $value, ElementReflectionInterface $reflectionElement): string |
200
|
|
|
{ |
201
|
|
|
$description = $this->getDescriptionFromValue($value, $reflectionElement); |
202
|
|
|
$typeLinks = $this->typeLinks($value, $reflectionElement); |
203
|
|
|
return $typeLinks . $description; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function processThrowsAnnotations(string $value, ElementReflectionInterface $elementReflection): string |
207
|
|
|
{ |
208
|
|
|
$description = $this->getDescriptionFromValue($value, $elementReflection); |
209
|
|
|
$typeLinks = $this->typeLinks($value, $elementReflection); |
210
|
|
|
return $typeLinks . $description; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param mixed $value |
215
|
|
|
* @param ElementReflectionInterface $elementReflection |
216
|
|
|
*/ |
217
|
|
|
private function getDescriptionFromValue($value, ElementReflectionInterface $elementReflection): string |
218
|
|
|
{ |
219
|
|
|
$description = (string) trim((string) strpbrk($value, "\n\r\t $")) ?: null; |
220
|
|
|
if ($description) { |
|
|
|
|
221
|
|
|
$description = '<br>' . $this->doc($description, $elementReflection); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return (string) $description; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
private function processLicenseAnnotations(string $value): string |
228
|
|
|
{ |
229
|
|
|
[$url, $description] = Strings::split($value); |
|
|
|
|
230
|
|
|
return $this->linkBuilder->build($url, $description ?: $url); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
private function processLinkAnnotations(string $value): string |
234
|
|
|
{ |
235
|
|
|
[$url, $description] = Strings::split($value); |
|
|
|
|
236
|
|
|
if (Validators::isUrl($url)) { |
237
|
|
|
return $this->linkBuilder->build($url, $description ?: $url); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return ''; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
private function processSeeAnnotations(string $value, ElementReflectionInterface $reflectionElement): string |
244
|
|
|
{ |
245
|
|
|
$doc = []; |
246
|
|
|
foreach (preg_split('~\\s*,\\s*~', $value) as $link) { |
247
|
|
|
if ($this->elementResolver->resolveElement($link, $reflectionElement) !== null) { |
248
|
|
|
$doc[] = $this->typeLinks($link, $reflectionElement); |
249
|
|
|
} else { |
250
|
|
|
$doc[] = $this->doc($link, $reflectionElement); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return implode(', ', $doc); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
private function processUsesAnnotations(string $value, ElementReflectionInterface $reflectionElement): ?string |
258
|
|
|
{ |
259
|
|
|
[$link, $description] = Strings::split($value); |
|
|
|
|
260
|
|
|
$separator = $reflectionElement instanceof ClassReflectionInterface || ! $description ? ' ' : '<br>'; |
261
|
|
|
if ($this->elementResolver->resolveElement($link, $reflectionElement) !== null) { |
262
|
|
|
$value = $this->typeLinks($link, $reflectionElement) . $separator . $description; |
263
|
|
|
return trim($value); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return null; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.