1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the "php-ipfs" package. |
7
|
|
|
* |
8
|
|
|
* (c) Robert Schönthal <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace IPFS\Utils; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as BaseReader; |
17
|
|
|
use IPFS\Annotation\Api; |
18
|
|
|
use IPFS\Annotation\Param; |
19
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
20
|
|
|
|
21
|
|
|
class AnnotationReader |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var BaseReader |
25
|
|
|
*/ |
26
|
|
|
private $reader; |
27
|
|
|
/** |
28
|
|
|
* @var DocBlockFactory |
29
|
|
|
*/ |
30
|
|
|
private $docBlockFactory; |
31
|
|
|
|
32
|
14 |
|
public function __construct(BaseReader $reader, DocBlockFactory $docBlockFactory) |
33
|
|
|
{ |
34
|
14 |
|
$this->reader = $reader; |
35
|
14 |
|
$this->docBlockFactory = $docBlockFactory; |
36
|
14 |
|
} |
37
|
|
|
|
38
|
4 |
|
public function isApi($method): bool |
39
|
|
|
{ |
40
|
4 |
|
return (bool) $this->getApi($method); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
public function getDescription($method): string |
44
|
|
|
{ |
45
|
3 |
|
return $this->docBlockFactory->create($this->getMethod($method))->getSummary(); |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
public function getName($method): string |
49
|
|
|
{ |
50
|
5 |
|
return (string) $this->getApi($method)->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array|Param[] |
55
|
|
|
*/ |
56
|
4 |
|
public function getParameters($method): array |
57
|
|
|
{ |
58
|
4 |
|
$reflection = $this->getMethod($method); |
59
|
4 |
|
$parameters = []; |
60
|
4 |
|
$docBlock = $this->docBlockFactory->create($reflection->getDocComment()); |
61
|
4 |
|
$params = $docBlock->getTagsByName('param'); |
62
|
|
|
|
63
|
4 |
|
foreach ($reflection->getParameters() as $parameter) { |
64
|
4 |
|
$parameters[$parameter->name] = new Param( |
65
|
4 |
|
$parameter->name, |
66
|
4 |
|
$this->parseParameterDocblock($params, $parameter), |
67
|
4 |
|
$parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : Param::class |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
return $parameters; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Api|null |
76
|
|
|
*/ |
77
|
7 |
|
private function getApi($method) |
78
|
|
|
{ |
79
|
7 |
|
return $this->reader->getMethodAnnotation( |
80
|
7 |
|
$this->getMethod($method), |
81
|
7 |
|
Api::class |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
10 |
|
private function getMethod($method): \ReflectionMethod |
86
|
|
|
{ |
87
|
10 |
|
if ($method instanceof \ReflectionMethod) { |
88
|
3 |
|
return $method; |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
list($api, $method) = explode('::', $method); |
92
|
|
|
|
93
|
7 |
|
return new \ReflectionMethod($api, $method); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
private function parseParameterDocblock(array $params, \ReflectionParameter $parameter): string |
97
|
|
|
{ |
98
|
4 |
|
foreach ($params as $param) { |
99
|
4 |
|
if ($parameter->name === $param->getVariableName()) { |
100
|
4 |
|
return $param->getDescription()->render(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|