|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
7
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
8
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
9
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
10
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
11
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
12
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
13
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
14
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
15
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
16
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
17
|
|
|
* |
|
18
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
19
|
|
|
* and is licensed under the MIT license. For more information, see |
|
20
|
|
|
* <https://github.com/digitalkaoz/php-ipfs> |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace IPFS\Utils; |
|
24
|
|
|
|
|
25
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as BaseReader; |
|
26
|
|
|
use IPFS\Annotation\Api; |
|
27
|
|
|
use IPFS\Annotation\Param; |
|
28
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
|
29
|
|
|
|
|
30
|
|
|
class AnnotationReader |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var BaseReader |
|
34
|
|
|
*/ |
|
35
|
|
|
private $reader; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var DocBlockFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
private $docBlockFactory; |
|
40
|
|
|
|
|
41
|
6 |
|
public function __construct(BaseReader $reader, DocBlockFactory $docBlockFactory) |
|
42
|
|
|
{ |
|
43
|
6 |
|
$this->reader = $reader; |
|
44
|
6 |
|
$this->docBlockFactory = $docBlockFactory; |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function isApi($method): bool |
|
48
|
|
|
{ |
|
49
|
2 |
|
return (bool) $this->getApi($method); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function getDescription($method): string |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->docBlockFactory->create($this->getMethod($method))->getSummary(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function getName($method): string |
|
58
|
|
|
{ |
|
59
|
1 |
|
return (string) $this->getApi($method)->name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array|Param[] |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getParameters($method): array |
|
66
|
|
|
{ |
|
67
|
1 |
|
$reflection = $this->getMethod($method); |
|
68
|
1 |
|
$parameters = []; |
|
69
|
1 |
|
$docBlock = $this->docBlockFactory->create($reflection->getDocComment()); |
|
70
|
1 |
|
$params = $docBlock->getTagsByName('param'); |
|
71
|
|
|
|
|
72
|
1 |
|
foreach ($reflection->getParameters() as $parameter) { |
|
73
|
1 |
|
$parameters[$parameter->name] = new Param( |
|
74
|
1 |
|
$parameter->name, |
|
75
|
1 |
|
$this->parseParameterDocblock($params, $parameter), |
|
76
|
1 |
|
$parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : Param::class |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $parameters; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Api|null |
|
85
|
|
|
*/ |
|
86
|
3 |
|
private function getApi($method) |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->reader->getMethodAnnotation( |
|
89
|
3 |
|
$this->getMethod($method), |
|
90
|
3 |
|
Api::class |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
private function getMethod($method): \ReflectionMethod |
|
95
|
|
|
{ |
|
96
|
5 |
|
if ($method instanceof \ReflectionMethod) { |
|
97
|
1 |
|
return $method; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
list($api, $method) = explode('::', $method); |
|
101
|
|
|
|
|
102
|
4 |
|
return new \ReflectionMethod($api, $method); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
private function parseParameterDocblock(array $params, \ReflectionParameter $parameter): string |
|
106
|
|
|
{ |
|
107
|
1 |
|
foreach ($params as $param) { |
|
108
|
1 |
|
if ($parameter->name === $param->getVariableName()) { |
|
109
|
1 |
|
return $param->getDescription()->render(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return ''; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|