forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocBlockParser.php
More file actions
89 lines (78 loc) · 2.74 KB
/
Copy pathDocBlockParser.php
File metadata and controls
89 lines (78 loc) · 2.74 KB
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
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Analysers;
use Doctrine\Common\Annotations\DocParser;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
/**
* Extract swagger-php annotations from a [PHPDoc](http://en.wikipedia.org/wiki/PHPDoc) using Doctrine's DocParser.
*/
class DocBlockParser
{
protected DocParser $docParser;
/**
* @param array<string, class-string> $aliases
*/
public function __construct(array $aliases = [])
{
if (DocBlockParser::isEnabled()) {
$docParser = new DocParser();
$docParser->setIgnoreNotImportedAnnotations(true);
$docParser->setImports($aliases);
$this->docParser = $docParser;
}
}
/**
* Check if we can process annotations.
*/
public static function isEnabled(): bool
{
return class_exists('Doctrine\\Common\\Annotations\\DocParser');
}
/**
* @param array<string, class-string> $aliases
*/
public function setAliases(array $aliases): void
{
$this->docParser->setImports($aliases);
}
/**
* Use doctrine to parse the comment block and return the detected annotations.
*
* @param string $comment a T_DOC_COMMENT
*
* @return array<OA\AbstractAnnotation|object>
*/
public function fromComment(string $comment, Context $context): array
{
$context->comment = $comment;
try {
Generator::$context = $context;
if ($context->is('annotations') === false) {
$context->annotations = [];
}
return $this->docParser->parse($comment, $context->getDebugLocation());
} catch (\Exception $exception) {
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote((string) $context, '/') . '\.$/', $exception->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = (int) $matches[2];
$atPos = strpos($comment, '@');
$context->line -= substr_count($comment, "\n", $atPos + $errorPos) + 1;
$lines = explode("\n", substr($comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1; // position starts at 0 character starts at 1
$context->logger->error($errorMessage . ' in ' . $context, ['exception' => $exception]);
} else {
$context->logger->error(
$exception->getMessage() . ($context->filename ? ('; file=' . $context->filename) : ''),
['exception' => $exception]
);
}
return [];
} finally {
Generator::$context = null;
}
}
}