Skip to content

Commit 8a93fc9

Browse files
committed
✨ automatic open api file generated
1 parent 9f136ad commit 8a93fc9

17 files changed

+1001
-78
lines changed

config/rest.php

+41
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@
6868
'identifier' => 'Apache-2.0'
6969
],
7070
'version' => '1.0.0'
71+
],
72+
// See https://spec.openapis.org/oas/v3.1.0#server-object
73+
'servers' => [
74+
[
75+
'url' => '/', // Relative to current
76+
'description' => 'The current server'
77+
],
78+
// [
79+
// 'url' => '"https://my-server.com:{port}/{basePath}"',
80+
// 'description' => 'Production server',
81+
// 'variables' => [
82+
// 'port' => [
83+
// 'enum' => ['80', '443'],
84+
// 'default' => '443'
85+
// ],
86+
// 'basePath' => [
87+
// 'default' => 'v2',
88+
// 'enum' => ['v1', 'v2'],
89+
// ]
90+
// ]
91+
// ]
92+
],
93+
// See https://spec.openapis.org/oas/v3.1.0#security-scheme-object
94+
'security' => [
95+
// [
96+
// 'type' => 'http',
97+
// 'description' => 'description',
98+
// 'scheme' => 'Bearer',
99+
// 'bearerFormat' => 'JWT'
100+
// ],
101+
// [
102+
// 'type' => 'oauth2',
103+
// 'flows' => [
104+
// 'authorizationCode' => [
105+
// 'scopes' => ['write:pets'],
106+
// 'tokenUrl' => 'https://example.com/api/oauth/token',
107+
// 'authorizationUrl' => 'https://example.com/api/oauth/dialog',
108+
// 'refreshUrl' => 'https://example.com/api/oauth/refresh',
109+
// ]
110+
// ]
111+
// ]
71112
]
72113
],
73114
];

src/Documentation/Schemas/MediaType.php

+70
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,74 @@ public function generateMutate(Controller $controller): MediaType
9999
)
100100
->generate();
101101
}
102+
103+
public function generateActions(Controller $controller): MediaType
104+
{
105+
return $this
106+
->withExample(
107+
(new Example)
108+
->withValue(
109+
[
110+
'data' => [
111+
'impacted' => 2
112+
]
113+
]
114+
)
115+
)
116+
->generate();
117+
}
118+
119+
public function generateDestroy(Controller $controller): MediaType
120+
{
121+
return $this
122+
->withExample(
123+
(new Example)
124+
->withValue(
125+
$controller::newResource()::newResponse()
126+
->resource($controller::newResource())
127+
->responsable(
128+
$controller::newResource()::newModel()::factory()->makeOne()
129+
->withoutRelations()
130+
)
131+
->toResponse(request())
132+
)
133+
)
134+
->generate();
135+
}
136+
137+
public function generateRestore(Controller $controller): MediaType
138+
{
139+
return $this
140+
->withExample(
141+
(new Example)
142+
->withValue(
143+
$controller::newResource()::newResponse()
144+
->resource($controller::newResource())
145+
->responsable(
146+
$controller::newResource()::newModel()::factory()->makeOne()
147+
->withoutRelations()
148+
)
149+
->toResponse(request())
150+
)
151+
)
152+
->generate();
153+
}
154+
155+
public function generateForceDelete(Controller $controller): MediaType
156+
{
157+
return $this
158+
->withExample(
159+
(new Example)
160+
->withValue(
161+
$controller::newResource()::newResponse()
162+
->resource($controller::newResource())
163+
->responsable(
164+
$controller::newResource()::newModel()::factory()->makeOne()
165+
->withoutRelations()
166+
)
167+
->toResponse(request())
168+
)
169+
)
170+
->generate();
171+
}
102172
}
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Documentation\Schemas;
4+
5+
class OauthFlow extends Schema
6+
{
7+
/**
8+
* The authorization URL to be used for this flow. This MUST be in the form of a URL. The OAuth2 standard requires the use of TLS.
9+
* @var string
10+
*/
11+
protected string $authorizationUrl;
12+
13+
/**
14+
* The token URL to be used for this flow. This MUST be in the form of a URL. The OAuth2 standard requires the use of TLS.
15+
* @var string
16+
*/
17+
protected string $tokenUrl;
18+
19+
/**
20+
* The URL to be used for obtaining refresh tokens. This MUST be in the form of a URL. The OAuth2 standard requires the use of TLS.
21+
* @var string
22+
*/
23+
protected string $refreshUrl;
24+
25+
/**
26+
* The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it. The map MAY be empty.
27+
* @var array
28+
*/
29+
protected array $scopes;
30+
31+
public function withAuthorizationUrl(string $authorizationUrl): OauthFlow
32+
{
33+
$this->authorizationUrl = $authorizationUrl;
34+
return $this;
35+
}
36+
37+
public function authorizationUrl(): string
38+
{
39+
return $this->authorizationUrl;
40+
}
41+
42+
public function withTokenUrl(string $tokenUrl): OauthFlow
43+
{
44+
$this->tokenUrl = $tokenUrl;
45+
return $this;
46+
}
47+
48+
public function tokenUrl(): string
49+
{
50+
return $this->tokenUrl;
51+
}
52+
53+
public function withRefreshUrl(string $refreshUrl): OauthFlow
54+
{
55+
$this->refreshUrl = $refreshUrl;
56+
return $this;
57+
}
58+
59+
public function refreshUrl(): string
60+
{
61+
return $this->refreshUrl;
62+
}
63+
64+
public function withScopes(array $scopes): OauthFlow
65+
{
66+
$this->scopes = $scopes;
67+
return $this;
68+
}
69+
70+
public function scopes(): array
71+
{
72+
return $this->scopes;
73+
}
74+
75+
public function generate(): Schema
76+
{
77+
return $this;
78+
}
79+
80+
public function jsonSerialize(): mixed
81+
{
82+
return array_merge(
83+
isset($this->scopes) ? ['scopes' => $this->scopes()] : [],
84+
isset($this->tokenUrl) ? ['tokenUrl' => $this->tokenUrl()] : [],
85+
isset($this->refreshUrl) ? ['refreshUrl' => $this->refreshUrl()] : [],
86+
isset($this->authorizationUrl) ? ['authorizationUrl' => $this->authorizationUrl()] : [],
87+
);
88+
}
89+
}
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Documentation\Schemas;
4+
5+
class OauthFlows extends Schema
6+
{
7+
/**
8+
* Configuration for the OAuth Implicit flow
9+
* @var OauthFlow
10+
*/
11+
protected OauthFlow $implicit;
12+
13+
/**
14+
* Configuration for the OAuth Resource Owner Password flow
15+
* @var OauthFlow
16+
*/
17+
protected OauthFlow $password;
18+
19+
/**
20+
* Configuration for the OAuth Client Credentials flow. Previously called application in OpenAPI 2.0.
21+
* @var OauthFlow
22+
*/
23+
protected OauthFlow $clientCredentials;
24+
25+
/**
26+
* Configuration for the OAuth Authorization Code flow. Previously called accessCode in OpenAPI 2.0.
27+
* @var OauthFlow
28+
*/
29+
protected OauthFlow $authorizationCode;
30+
31+
public function withImplicit(OauthFlow $implicit): OauthFlows
32+
{
33+
$this->implicit = $implicit;
34+
return $this;
35+
}
36+
37+
public function implicit(): OauthFlow
38+
{
39+
return $this->implicit;
40+
}
41+
42+
public function withPassword(OauthFlow $password): OauthFlows
43+
{
44+
$this->password = $password;
45+
return $this;
46+
}
47+
48+
public function password(): OauthFlow
49+
{
50+
return $this->password;
51+
}
52+
53+
public function withClientCredentials(OauthFlow $clientCredentials): OauthFlows
54+
{
55+
$this->clientCredentials = $clientCredentials;
56+
return $this;
57+
}
58+
59+
public function clientCredentials(): OauthFlow
60+
{
61+
return $this->clientCredentials;
62+
}
63+
64+
public function withAuthorizationCode(OauthFlow $authorizationCode): OauthFlows
65+
{
66+
$this->authorizationCode = $authorizationCode;
67+
return $this;
68+
}
69+
70+
public function authorizationCode(): OauthFlow
71+
{
72+
return $this->authorizationCode;
73+
}
74+
75+
public function generate(): OauthFlows
76+
{
77+
return $this;
78+
}
79+
80+
public function jsonSerialize(): mixed
81+
{
82+
return array_merge(
83+
isset($this->implicit) ? ['implicit' => $this->implicit()->jsonSerialize()] : [],
84+
isset($this->password) ? ['password' => $this->password()->jsonSerialize()] : [],
85+
isset($this->authorizationCode) ? ['authorizationCode' => $this->authorizationCode()->jsonSerialize()] : [],
86+
isset($this->clientCredentials) ? ['clientCredentials' => $this->clientCredentials()->jsonSerialize()] : [],
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)