Skip to content

Commit b3bfaa4

Browse files
committed
add config
1 parent 2797635 commit b3bfaa4

7 files changed

Lines changed: 45 additions & 22 deletions

File tree

.openapi.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
<?php
22

3+
use Astral\Serialize\OpenApi\Storage\OpenAPI\ServersStorage;
4+
35
return [
46
'title' => 'API Docs',
7+
58
'description' => 'API Docs description.',
9+
10+
/**
11+
* 向全局头部参数存储中添加一个的头部参数。
12+
* @param string $name
13+
* @param string $example
14+
* @param string $description
15+
*/
616
'headers' => [
717

818
],
9-
'service' => [
1019

20+
'service' => [
21+
new ServersStorage('http://127.0.0.1','默认环境'),
1122
],
1223
];

src/OpenApi.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public function buildByClass(string $className): void
5353
Headers::class => null,
5454
];
5555

56-
5756
foreach ($methodAttributes as $methodAttribute) {
5857
$inst = $methodAttribute->newInstance();
5958
foreach (array_keys($instances) as $anchorClass) {
@@ -63,7 +62,6 @@ public function buildByClass(string $className): void
6362
}
6463
}
6564

66-
6765
if ($instances[Route::class] === null || $instances[Summary::class] === null) {
6866
continue;
6967
}

src/OpenApi/Handler/Config.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
class Config
1717
{
18-
19-
public static $config;
18+
public static array $config;
2019

2120
public static function rootPath(): string
2221
{
@@ -29,12 +28,11 @@ public static function build()
2928
return self::$config;
3029
}
3130

31+
self::$config = include dirname(__DIR__, 3).'/.openapi.php';
32+
3233
$path = self::rootPath().'/.openapi.php';
3334
if(is_file($path)){
34-
self::$config = include $path;
35-
}
36-
else{
37-
self::$config = include dirname(__DIR__, 3).'/.openapi.php';
35+
self::$config = array_merge(self::$config,include $path);
3836
}
3937

4038
return self::$config;
@@ -45,4 +43,9 @@ public static function get($key)
4543
return self::build()[$key] ?? '';
4644
}
4745

46+
public static function has($key): bool
47+
{
48+
return isset(self::build()[$key]);
49+
}
50+
4851
}

src/OpenApi/Handler/HandleInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
interface HandleInterface
88
{
9-
public function output(string $path): bool;
109

1110
public function toString(): string;
1211
}

src/OpenApi/Handler/Handler.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@
1515

1616
abstract class Handler implements HandleInterface
1717
{
18-
/** @var OpenAPI */
19-
public static OpenAPI $OpenAPI;
18+
/** @var OpenAPI|null */
19+
public static ?OpenAPI $OpenAPI = null;
2020

2121
public function __construct(
2222
protected readonly ParameterStorage $headerParameterStorages = new ParameterStorage()
2323
) {
24-
self::$OpenAPI ??= (new OpenAPI())->withApiInfo(new ApiInfo('API Doc',''));
24+
25+
self::$OpenAPI ??= (new OpenAPI())
26+
->withApiInfo(new ApiInfo(Config::get('title'), Config::get('description')))
27+
->withServers(Config::get('service'));
28+
29+
if(Config::has('headers')) {
30+
foreach (Config::get('headers') as $header) {
31+
$this->headerParameterStorages->addHeaderProperties($header['name'], $header['description'], $header['example']);
32+
}
33+
}
2534
}
2635

2736
public function rootPath(): string
@@ -114,7 +123,6 @@ protected function scanFolderRecursively(string $folder, string $namespace): voi
114123

115124
// 如果是子目录,则递归,并拼接命名空间
116125
if (is_dir($path)) {
117-
118126
// 例如,如果当前命名空间是 "App":
119127
// 子目录 "Http" 则新的命名空间为 "App\Http"
120128
$newNamespace = $namespace !== '' ? ($namespace . '\\' . $file) : $file;
@@ -152,11 +160,6 @@ protected function scanFolderRecursively(string $folder, string $namespace): voi
152160
}
153161
}
154162

155-
public function output(string $path): bool
156-
{
157-
return true;
158-
}
159-
160163
/**
161164
* @throws JsonException
162165
*/

src/OpenApi/Storage/OpenAPI/OpenAPI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
class OpenAPI implements StorageInterface
1212
{
13-
1413
public string $openapi = '3.1.1';
1514

1615
public ApiInfo $info;
@@ -34,6 +33,7 @@ public function withApiInfo(ApiInfo $apiInfo): self
3433
return $this;
3534
}
3635

36+
3737
public function withServers(array $servers): self
3838
{
3939
$this->servers = $servers;

src/OpenApi/Storage/OpenAPI/ServersStorage.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ class ServersStorage implements StorageInterface
1212
public function __construct(
1313
public string $url,
1414
public string $description,
15-
public array|stdClass|null $variables = null
15+
public array|stdClass $variables = new stdClass(),
1616
) {
17-
$this->variables = $this->variables ?: new stdClass();
17+
18+
}
19+
20+
public function addVariable(string $name, $description, $default = ''): static
21+
{
22+
$this->variables = $this->variables instanceof stdClass::class ? [] : $this->variables;
23+
24+
$this->variables[$name] = ['default' => $default, 'description'=> $description];
25+
26+
return $this;
1827
}
1928
}

0 commit comments

Comments
 (0)