This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpointDocblock.php
175 lines (127 loc) · 4.46 KB
/
EndpointDocblock.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Class EndpointDocblock
*
* @created 08.09.2018
* @author smiley <smiley@chillerlan.net>
* @copyright 2018 smiley
* @license MIT
*/
namespace chillerlan\OAuthTest\Providers;
use chillerlan\OAuth\Core\OAuthInterface;
use chillerlan\OAuth\MagicAPI\EndpointMapInterface;
use ReflectionClass;
use function count, dirname, file_get_contents, file_put_contents, implode, in_array, is_array, ksort, str_replace;
use const JSON_PRETTY_PRINT;
final class EndpointDocblock{
private OAuthInterface $provider;
private EndpointMapInterface $endpointMap;
/**
* EndpointDocblock constructor.
*
* @param \chillerlan\OAuth\Core\OAuthInterface $provider
* @param \chillerlan\OAuth\MagicAPI\EndpointMapInterface $endpointMap
*/
public function __construct(OAuthInterface $provider, EndpointMapInterface $endpointMap){
$this->provider = $provider;
$this->endpointMap = $endpointMap;
}
/**
* @param string $returntype
*
* @return string
*/
public function create(string $returntype):string{
$n = "\n";
$str = '/**'.$n;
$ep = $this->endpointMap->toArray();
ksort($ep);
foreach($ep as $methodName => $params){
if($methodName === 'API_BASE'){
continue;
}
$args = [];
if(isset($params['path_elements']) && count($params['path_elements']) > 0){
foreach($params['path_elements'] as $i){
$args[] = 'string $'.$i;
}
}
if(isset($params['query']) && !empty($params['query'])){
$args[] = 'array $params = [\''.implode('\', \'', $params['query']).'\']';
}
if(isset($params['method']) && in_array($params['method'], ['PATCH', 'POST', 'PUT', 'DELETE'], true)){
if(isset($params['body']) && $params['body'] !== null){
$args[] = is_array($params['body']) ? 'array $body = [\''.implode('\', \'', $params['body']).'\']' : 'array $body = []';
}
}
$str .= ' * @method \\'.$returntype.' '.$methodName.'('.implode(', ', $args).')'.$n;
}
$str .= ' *'.'/'.$n;
$reflection = new ReflectionClass($this->provider);
$classfile = $reflection->getFileName();
file_put_contents($classfile, str_replace($reflection->getDocComment().$n, $str, file_get_contents($classfile)));
return $str;
}
/**
* @param string $name
* @param string $returntype
* @param string|null $namespace
*
* @return bool
*/
public function createInterface(string $name, string $returntype, string $namespace = null):bool{
$reflection = new ReflectionClass($this->provider);
$interfaceName = $name.'Interface';
$n = "\n";
$ep = $this->endpointMap->toArray();
$str = '<?php'.$n.$n
.'namespace '.($namespace ?? $reflection->getNamespaceName()).';'.$n.$n
.'use '.$returntype.';'.$n.$n
.'interface '.$interfaceName.'{'.$n.$n;
ksort($ep);
foreach($ep as $methodName => $params){
if($methodName === 'API_BASE'){
continue;
}
$args = [];
$str .= "\t".'/**'.$n;
if(isset($params['path_elements']) && is_array($params['path_elements']) && count($params['path_elements']) > 0){
foreach($params['path_elements'] as $i){
$a = 'string $'.$i;
$str .= "\t".' * @param '.$a.$n;
$args[] = $a;
}
}
if(!empty($params['query'])){
$a = 'array $params = [\''.implode('\', \'', $params['query']).'\']';
$str .= "\t".' * @param '.$a.$n;
$args[] = $a;
}
if(isset($params['method']) && in_array($params['method'], ['PATCH', 'POST', 'PUT', 'DELETE'])){
if($params['body'] !== null){
$a = is_array($params['body']) ? 'array $body = [\''.implode('\', \'', $params['body']).'\']' : 'array $body = []';
$str .= "\t".' * @param '.$a.$n;
$args[] = $a;
}
}
$r = new ReflectionClass($returntype);
$str .= "\t".' * @return \\'.$r->getName().$n;
$str .= "\t".' */'.$n;
$str .= "\t".'public function '.$methodName.'('.implode(', ', $args).'):'.$r->getShortName().';'.$n.$n;
}
$str .= '}'.$n;
$file = dirname($reflection->getFileName()).'/'.$interfaceName.'.php';
return (bool)file_put_contents($file, $str);
}
/**
* @param string|null $name
* @param int $jsonOptions
*
* @return bool
*/
public function createJSON(string $name = null, int $jsonOptions = JSON_PRETTY_PRINT):bool{
$reflection = new ReflectionClass($this->endpointMap);
$file = dirname($reflection->getFileName()).'/'.($name ?? $reflection->getShortName()).'.json';
return (bool)file_put_contents($file, $this->endpointMap->toJSON($jsonOptions));
}
}