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
|
|
|
public function __construct(BaseReader $reader, DocBlockFactory $docBlockFactory) |
42
|
|
|
{ |
43
|
|
|
$this->reader = $reader; |
44
|
|
|
$this->docBlockFactory = $docBlockFactory; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isApi($method): bool |
48
|
|
|
{ |
49
|
|
|
return (bool) $this->getApi($method); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getDescription($method): string |
53
|
|
|
{ |
54
|
|
|
return $this->docBlockFactory->create($this->getMethod($method))->getSummary(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getName($method): string |
58
|
|
|
{ |
59
|
|
|
return (string) $this->getApi($method)->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array|Param[] |
64
|
|
|
*/ |
65
|
|
|
public function getParameters($method): array |
66
|
|
|
{ |
67
|
|
|
$reflection = $this->getMethod($method); |
68
|
|
|
$parameters = []; |
69
|
|
|
$docBlock = $this->docBlockFactory->create($reflection->getDocComment()); |
70
|
|
|
$params = $docBlock->getTagsByName('param'); |
71
|
|
|
|
72
|
|
|
foreach ($reflection->getParameters() as $parameter) { |
73
|
|
|
$parameters[$parameter->name] = new Param( |
74
|
|
|
$parameter->name, |
75
|
|
|
$this->parseParameterDocblock($params, $parameter), |
76
|
|
|
$parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : Param::class |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $parameters; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Api|null |
85
|
|
|
*/ |
86
|
|
|
private function getApi($method) |
87
|
|
|
{ |
88
|
|
|
return $this->reader->getMethodAnnotation( |
89
|
|
|
$this->getMethod($method), |
90
|
|
|
Api::class |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getMethod($method): \ReflectionMethod |
95
|
|
|
{ |
96
|
|
|
if ($method instanceof \ReflectionMethod) { |
97
|
|
|
return $method; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
list($api, $method) = explode('::', $method); |
101
|
|
|
|
102
|
|
|
return new \ReflectionMethod($api, $method); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function parseParameterDocblock(array $params, \ReflectionParameter $parameter): string |
106
|
|
|
{ |
107
|
|
|
foreach ($params as $param) { |
108
|
|
|
if ($parameter->name === $param->getVariableName()) { |
109
|
|
|
return $param->getDescription()->render(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return ''; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|