Skip to content

Commit 97ef462

Browse files
committed
:octocat:
1 parent 2a581ff commit 97ef462

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/MagicAPI/EndpointDocblock.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Class EndpointDocblock
4+
*
5+
* @filesource EndpointDocblock.php
6+
* @created 08.09.2018
7+
* @package chillerlan\HTTP\MagicAPI
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\HTTP\MagicAPI;
14+
15+
use ReflectionClass;
16+
17+
class EndpointDocblock{
18+
19+
/**
20+
* @var object
21+
*/
22+
protected $provider;
23+
24+
/**
25+
* @var \chillerlan\HTTP\MagicAPI\EndpointMapInterface
26+
*/
27+
protected $endpointMap;
28+
29+
/**
30+
* EndpointDocblock constructor.
31+
*
32+
* @param $provider
33+
* @param \chillerlan\HTTP\MagicAPI\EndpointMapInterface $endpointMap
34+
*/
35+
public function __construct($provider, EndpointMapInterface $endpointMap){
36+
$this->provider = $provider;
37+
$this->endpointMap = $endpointMap;
38+
}
39+
40+
/**
41+
* @param string $returntype
42+
*
43+
* @return string
44+
* @throws \chillerlan\HTTP\MagicAPI\ApiClientException
45+
*/
46+
public function create(string $returntype):string{
47+
48+
if(!$this->endpointMap instanceof EndpointMap){
49+
throw new ApiClientException('invalid endpoint map');
50+
}
51+
52+
$str = '/**'.PHP_EOL;
53+
54+
foreach($this->endpointMap->toArray() as $methodName => $params){
55+
56+
if($methodName === 'API_BASE'){
57+
continue;
58+
}
59+
60+
$args = [];
61+
62+
if(isset($params['path_elements']) && count($params['path_elements']) > 0){
63+
64+
foreach($params['path_elements'] as $i){
65+
$args[] = 'string $'.$i;
66+
}
67+
68+
}
69+
70+
if(isset($params['query']) && !empty($params['query'])){
71+
$args[] = 'array $params = [\''.implode('\', \'', $params['query']).'\']';
72+
}
73+
74+
if(isset($params['method']) && in_array($params['method'], ['PATCH', 'POST', 'PUT', 'DELETE'], true)){
75+
76+
if($params['body'] !== null){
77+
$args[] = is_array($params['body']) ? 'array $body = [\''.implode('\', \'', $params['body']).'\']' : 'array $body = []';
78+
}
79+
80+
}
81+
82+
$str.= ' * @method \\'.$returntype.' '.$methodName.'('.implode(', ', $args).')'.PHP_EOL;
83+
}
84+
85+
$str .= ' *'.'/'.PHP_EOL;
86+
87+
$reflection = new ReflectionClass($this->provider);
88+
$classfile = $reflection->getFileName();
89+
90+
file_put_contents($classfile, str_replace($reflection->getDocComment().PHP_EOL, $str, file_get_contents($classfile)));
91+
92+
return $str;
93+
}
94+
95+
/**
96+
* @param string $name
97+
* @param string $returntype
98+
*
99+
* @return bool
100+
*/
101+
public function createInterface(string $name, string $returntype):bool{
102+
$interfaceName = $name.'Interface';
103+
104+
$str = '<?php'.PHP_EOL.PHP_EOL
105+
.'namespace '.__NAMESPACE__.';'.PHP_EOL.PHP_EOL
106+
.'use \\'.$returntype.';'.PHP_EOL.PHP_EOL
107+
.'interface '.$interfaceName.'{'.PHP_EOL.PHP_EOL;
108+
109+
foreach($this->endpointMap->toArray() as $methodName => $params){
110+
111+
if($methodName === 'API_BASE'){
112+
continue;
113+
}
114+
115+
$args = [];
116+
$str.= "\t".'/**'.PHP_EOL;
117+
118+
if(is_array($params['path_elements']) && count($params['path_elements']) > 0){
119+
120+
foreach($params['path_elements'] as $i){
121+
$a = 'string $'.$i;
122+
$str.= "\t".' * @param '.$a.PHP_EOL;
123+
124+
$args[] = $a;
125+
}
126+
127+
}
128+
129+
if(!empty($params['query'])){
130+
$a = 'array $params = [\''.implode('\', \'', $params['query']).'\']';
131+
$str.= "\t".' * @param '.$a.PHP_EOL;
132+
$args[] = $a;
133+
}
134+
135+
if(in_array($params['method'], ['PATCH', 'POST', 'PUT', 'DELETE'])){
136+
137+
if($params['body'] !== null){
138+
$a = is_array($params['body']) ? 'array $body = [\''.implode('\', \'', $params['body']).'\']' : 'array $body = []';
139+
$str.= "\t".' * @param '.$a.PHP_EOL;
140+
$args[] = $a;
141+
}
142+
143+
}
144+
145+
$r = new ReflectionClass($returntype);
146+
147+
$str.= "\t".' * @return \\'.$r->getName().PHP_EOL;
148+
$str.= "\t".' */'.PHP_EOL;
149+
$str.= "\t".'public function '.$methodName.'('.implode(', ', $args).'):'.$r->getShortName().';'.PHP_EOL.PHP_EOL;
150+
}
151+
152+
$str .= '}'.PHP_EOL;
153+
154+
return (bool)file_put_contents(dirname((new ReflectionClass($this->provider))->getFileName()).'/'.$interfaceName.'.php', $str);
155+
}
156+
157+
}

0 commit comments

Comments
 (0)