Skip to content

Commit 28fd6cb

Browse files
committed
add a tool class for gen swagger UI json
1 parent 3df31e7 commit 28fd6cb

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

lib/Util/SwaggerJSON.php

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-01-05
6+
* Time: 14:32
7+
*/
8+
9+
namespace App\Components;
10+
11+
use Swagger\Annotations\Info;
12+
use Swagger\Annotations\Swagger;
13+
14+
/**
15+
* Class SwaggerJSON
16+
* @package App\Components
17+
*/
18+
class SwaggerJSON
19+
{
20+
/** @var string Env name. like prod,test,dev */
21+
private $env;
22+
23+
/** @var string */
24+
private $fileTpl = 'swagger-%s.json';
25+
26+
/** @var array */
27+
private $settings = [
28+
// basic
29+
'host' => 'localhost',
30+
'basePath' => '/',
31+
'schemes' => ['http', 'https'],
32+
'consumes' => ['application/json'],
33+
'produces' => ['application/json'],
34+
35+
// info
36+
'version' => '1.0.0',
37+
'title' => 'some text',
38+
'description' => '## MY App(`env: {@env}`)
39+
some message',
40+
];
41+
42+
/** @var array */
43+
private $envSettings = [
44+
'dev' => [
45+
'host' => 'api.dev',
46+
'basePath' => '/',
47+
],
48+
'test' => [
49+
'host' => '172.19.49.177:8203',
50+
'basePath' => '/',
51+
],
52+
];
53+
54+
/**
55+
* @var array Want to scan's dirs
56+
*/
57+
private $scanDirs = [];
58+
59+
/**
60+
* @param array $settings
61+
* @return SwaggerJSON
62+
*/
63+
public static function make(array $settings = []): self
64+
{
65+
return new self($settings);
66+
}
67+
68+
/**
69+
* SwaggerJSON constructor.
70+
* @param array $settings
71+
*/
72+
public function __construct(array $settings = [])
73+
{
74+
if (!class_exists(Swagger::class)) {
75+
throw new \RuntimeException("please install 'zircote/swagger-php' package by composer.");
76+
}
77+
78+
if ($settings) {
79+
$this->setSettings($settings);
80+
}
81+
}
82+
83+
/**
84+
* @param string|null $outputDir
85+
* @param bool $refresh
86+
* @return string
87+
*/
88+
public function generate(string $outputDir = null, $refresh = false): string
89+
{
90+
if (!$this->env) {
91+
$this->env = APP_ENV;
92+
}
93+
94+
$env = $this->env;
95+
$path = $outputDir ?: BASE_PATH;
96+
$name = sprintf($this->fileTpl, $env);
97+
$file = $path . '/' . $name;
98+
99+
if (!$refresh && is_file($file)) {
100+
return $file;
101+
}
102+
103+
/** @var Swagger $swg */
104+
$swg = \Swagger\scan($this->scanDirs);
105+
106+
if (isset($this->envSettings[$env])) {
107+
$this->settings = array_merge($this->settings, $this->envSettings[$env]);
108+
}
109+
110+
// basic
111+
$swg->host = $this->settings['host'];
112+
$swg->basePath = $this->settings['basePath'];
113+
$swg->schemes = $this->settings['schemes'];
114+
$swg->consumes = $this->settings['consumes'];
115+
$swg->produces = $this->settings['produces'];
116+
117+
// info
118+
$info = new Info([]);
119+
$info->title = $this->settings['title'];
120+
$info->version = $this->settings['version'];
121+
$info->description = str_replace('{@env}', $env, $this->settings['description']);
122+
123+
$swg->info = $info;
124+
125+
file_put_contents($file, (string)$swg);
126+
127+
return $file;
128+
}
129+
130+
131+
/**
132+
* @param string $env
133+
* @param array $info
134+
*/
135+
public function setEnvSetting(string $env, array $info)
136+
{
137+
$this->envSettings[$env] = array_merge([
138+
'host' => 'localhost',
139+
'basePath' => '/',
140+
], $info);
141+
}
142+
143+
/**
144+
* @return array
145+
*/
146+
public function getEnvSettings(): array
147+
{
148+
return $this->envSettings;
149+
}
150+
151+
/**
152+
* @param array $envSettings
153+
*/
154+
public function setEnvSettings(array $envSettings)
155+
{
156+
$this->envSettings = $envSettings;
157+
}
158+
159+
/**
160+
* @return string
161+
*/
162+
public function getEnv(): string
163+
{
164+
return $this->env;
165+
}
166+
167+
/**
168+
* @param string $env
169+
*/
170+
public function setEnv(string $env)
171+
{
172+
$this->env = $env;
173+
}
174+
175+
/**
176+
* @param string $name
177+
* @param null $default
178+
* @return mixed|null
179+
*/
180+
public function get(string $name, $default = null)
181+
{
182+
return $this->settings[$name] ?? $default;
183+
}
184+
185+
/**
186+
* @param string $name
187+
* @param $value
188+
*/
189+
public function set(string $name, $value)
190+
{
191+
$this->settings[$name] = $value;
192+
}
193+
194+
/**
195+
* @return array
196+
*/
197+
public function getSettings(): array
198+
{
199+
return $this->settings;
200+
}
201+
202+
/**
203+
* @param array $settings
204+
*/
205+
public function setSettings(array $settings)
206+
{
207+
$this->settings = array_merge($this->settings, $settings);
208+
}
209+
210+
/**
211+
* @return array
212+
*/
213+
public function getScanDirs(): array
214+
{
215+
return $this->scanDirs;
216+
}
217+
218+
/**
219+
* @param array $scanDirs
220+
*/
221+
public function setScanDirs(array $scanDirs)
222+
{
223+
$this->scanDirs = $scanDirs;
224+
}
225+
226+
/**
227+
* @return string
228+
*/
229+
public function getFileTpl(): string
230+
{
231+
return $this->fileTpl;
232+
}
233+
234+
/**
235+
* @param string $fileTpl
236+
*/
237+
public function setFileTpl(string $fileTpl)
238+
{
239+
$this->fileTpl = $fileTpl;
240+
}
241+
}

0 commit comments

Comments
 (0)