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\Api; |
15
|
|
|
|
16
|
|
|
use Http\Client\HttpAsyncClient; |
17
|
|
|
use Http\Message\MessageFactory; |
18
|
|
|
use IPFS\Utils\CaseFormatter; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
21
|
|
|
|
22
|
|
|
class ApiParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var HttpAsyncClient |
26
|
|
|
*/ |
27
|
|
|
private $client; |
28
|
|
|
/** |
29
|
|
|
* @var MessageFactory |
30
|
|
|
*/ |
31
|
|
|
private $messageFactory; |
32
|
|
|
/** |
33
|
|
|
* @var Crawler |
34
|
|
|
*/ |
35
|
|
|
private $crawler; |
36
|
|
|
|
37
|
2 |
|
public function __construct(HttpAsyncClient $client, MessageFactory $messageFactory, Crawler $crawler) |
38
|
|
|
{ |
39
|
2 |
|
$this->client = $client; |
40
|
2 |
|
$this->messageFactory = $messageFactory; |
41
|
2 |
|
$this->crawler = $crawler; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function build(string $url = 'https://ipfs.io/docs/api/', string $prefix = '#apiv0'): array |
45
|
|
|
{ |
46
|
1 |
|
return $this->client |
47
|
1 |
|
->sendAsyncRequest($this->messageFactory->createRequest('GET', $url)) |
48
|
|
|
->then(function (ResponseInterface $response) use ($prefix) { |
49
|
1 |
|
return $this->buildMethods($response, $prefix); |
50
|
1 |
|
}) |
51
|
1 |
|
->wait() |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
private function buildMethods(ResponseInterface $response, $prefix): array |
56
|
|
|
{ |
57
|
1 |
|
$this->crawler->addHtmlContent($response->getBody()->getContents()); |
58
|
|
|
$links = $this->crawler->filter('li a[href^="' . $prefix . '"]')->each(function (Crawler $node) { |
59
|
1 |
|
return $node->attr('href'); |
60
|
1 |
|
}); |
61
|
|
|
|
62
|
1 |
|
$config = []; |
63
|
1 |
|
foreach ($links as $link) { |
64
|
1 |
|
$anchor = $this->crawler->filter($link)->first(); |
65
|
1 |
|
$description = $anchor->nextAll()->first()->getNode(0)->nodeName === 'p' ? $anchor->nextAll()->first()->text() : null; |
66
|
|
|
|
67
|
|
|
$methodConfig = [ |
68
|
1 |
|
'parts' => str_replace('/', ':', str_replace('/api/v0/', '', $anchor->text())), |
69
|
1 |
|
'description' => $description, |
70
|
1 |
|
'arguments' => $this->parseArguments($anchor, $description ? 2 : 1), |
71
|
|
|
]; |
72
|
|
|
|
73
|
1 |
|
$nameParts = explode(':', $methodConfig['parts']); |
74
|
1 |
|
if (count($nameParts) > 1) { |
75
|
1 |
|
$class = array_shift($nameParts); |
76
|
1 |
|
$methodConfig['class'] = ucfirst(CaseFormatter::dashToCamel($class)); |
77
|
1 |
|
$methodConfig['method'] = CaseFormatter::dashToCamel(implode('-', $nameParts)); |
78
|
|
|
} else { |
79
|
1 |
|
$methodConfig['class'] = 'Basics'; |
80
|
1 |
|
$methodConfig['method'] = CaseFormatter::dashToCamel($methodConfig['parts']); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$config[$methodConfig['class']][] = $methodConfig; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $config; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
private function parseArguments(Crawler $anchor, int $index): array |
90
|
|
|
{ |
91
|
1 |
|
$argumentsRootNode = $anchor->nextAll()->eq($index)->first(); |
92
|
|
|
|
93
|
1 |
|
if ($argumentsRootNode->getNode(0)->nodeName === 'ul') { |
94
|
1 |
|
$names = []; |
95
|
|
|
|
96
|
1 |
|
return $argumentsRootNode->filter('li')->each(function (Crawler $argument) use (&$names) { |
97
|
1 |
|
$description = $argument->filter('code')->first()->getNode(0)->nextSibling->textContent; |
98
|
1 |
|
$name = CaseFormatter::dashToCamel($argument->filter('code')->first()->text()); |
99
|
1 |
|
if (!isset($names[$name])) { |
100
|
1 |
|
$names[$name] = 0; |
101
|
|
|
} else { |
102
|
1 |
|
++$names[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$config = [ |
106
|
1 |
|
'name' => $names[$name] === 0 ? $name : $name . $names[$name], |
107
|
1 |
|
'required' => $argument->filter('strong')->count() > 0, |
108
|
1 |
|
'description' => $this->parseDescription($description), |
109
|
1 |
|
'default' => $this->parseDefaultValue($description), |
110
|
1 |
|
'type' => $this->parseTypeHint($description), |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
//fixup files |
114
|
1 |
|
if ('file' === $config['type']) { |
115
|
1 |
|
$config['type'] = 'string'; |
116
|
1 |
|
$config['name'] = 'file'; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $config; |
120
|
1 |
|
}); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
private function parseDefaultValue(string $description) |
127
|
|
|
{ |
128
|
1 |
|
preg_match('/Default\: \"(.+)\"/', $description, $default); |
129
|
|
|
|
130
|
1 |
|
if (count($default) === 2) { |
131
|
1 |
|
$default = $default[1]; |
132
|
1 |
|
$default = CaseFormatter::stringToBool($default); |
133
|
|
|
|
134
|
1 |
|
return $default; |
135
|
|
|
} |
136
|
1 |
|
} |
137
|
|
|
|
138
|
1 |
|
private function parseDescription($description): string |
139
|
|
|
{ |
140
|
1 |
|
preg_match('/\]\: (.+)/', $description, $default); |
141
|
|
|
|
142
|
1 |
|
if (count($default) > 1) { |
143
|
1 |
|
return substr($default[1], 0, strpos($default[1], '.') ?: strlen($default[1])) . '.'; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
private function parseTypeHint(string $description): string |
148
|
|
|
{ |
149
|
1 |
|
preg_match('/\[(.+)\]/', $description, $default); |
150
|
|
|
|
151
|
1 |
|
if (count($default) > 1) { |
152
|
1 |
|
return $default[1]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|