1 | <?php |
||
2 | |||
3 | namespace SoliDry\Documentation; |
||
4 | |||
5 | use SoliDry\ApiGenerator; |
||
6 | use SoliDry\Blocks\ContentManager; |
||
7 | use SoliDry\Controllers\BaseCommand; |
||
8 | use SoliDry\Extension\JSONApiInterface; |
||
9 | use SoliDry\Helpers\Classes; |
||
10 | use SoliDry\Helpers\MigrationsHelper; |
||
11 | use SoliDry\Types\ApiInterface; |
||
12 | use SoliDry\Types\DefaultInterface; |
||
13 | use SoliDry\Types\DocumentationInterface; |
||
14 | use SoliDry\Types\PhpInterface; |
||
15 | |||
16 | /** |
||
17 | * Class Documentation |
||
18 | * |
||
19 | * @package SoliDry\Blocks |
||
20 | * |
||
21 | * @property BaseCommand generator |
||
22 | */ |
||
23 | abstract class Documentation |
||
24 | { |
||
25 | use ContentManager, RelationsDoc, BulksDoc; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
26 | |||
27 | public const SUCCESSFUL_OPERATION = 'successful operation'; |
||
28 | |||
29 | /** |
||
30 | * @var ApiGenerator |
||
31 | */ |
||
32 | protected ApiGenerator $generator; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected string $sourceCode = ''; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected string $className; |
||
43 | |||
44 | /** |
||
45 | * Controllers constructor. |
||
46 | * |
||
47 | * @param ApiGenerator $generator |
||
48 | */ |
||
49 | public function __construct($generator) |
||
50 | { |
||
51 | $this->generator = $generator; |
||
52 | $this->className = Classes::getClassName($this->generator->objectName); |
||
53 | } |
||
54 | |||
55 | protected function setDefaultDocs(): void |
||
56 | { |
||
57 | $this->setComment(DefaultInterface::METHOD_START); |
||
58 | |||
59 | $this->openComment(); |
||
60 | |||
61 | // generate basic info |
||
62 | $this->setStarredComment(DocumentationInterface::OA_INFO . PhpInterface::OPEN_PARENTHESES); |
||
63 | |||
64 | $this->setInfoParams(); |
||
65 | |||
66 | // generate contact info |
||
67 | $this->setContactInfo(); |
||
68 | |||
69 | // generate license info |
||
70 | $this->setLicenseInfo(); |
||
71 | |||
72 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
73 | |||
74 | $this->closeComment(); |
||
75 | |||
76 | $this->setComment(DefaultInterface::METHOD_END); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Sets info params - title, version, description |
||
81 | */ |
||
82 | private function setInfoParams(): void |
||
83 | { |
||
84 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_TITLE]) === false) { |
||
85 | $this->setStarredComment('title="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_TITLE] . '",', |
||
86 | 1, 1); |
||
87 | } |
||
88 | |||
89 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_VERSION]) === false) { |
||
90 | $this->setStarredComment('version="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_VERSION] . '",', |
||
91 | 1, 1); |
||
92 | } |
||
93 | |||
94 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_DESCRIPTION]) === false) { |
||
95 | $this->setStarredComment('description="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_DESCRIPTION] . '",', |
||
96 | 1, 1); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Sets license info - name, url |
||
102 | */ |
||
103 | private function setLicenseInfo(): void |
||
104 | { |
||
105 | $this->setStarredComment(DocumentationInterface::OA_LICENSE . PhpInterface::OPEN_PARENTHESES, |
||
106 | 1, 1); |
||
107 | |||
108 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_NAME]) === false) { |
||
109 | $this->setStarredComment('name="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_NAME] . '",', |
||
110 | 1, 2); |
||
111 | } |
||
112 | |||
113 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_URL]) === false) { |
||
114 | $this->setStarredComment('url="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_URL] . '",', |
||
115 | 1, 2); |
||
116 | } |
||
117 | |||
118 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Sets contact info - email, name, url |
||
123 | */ |
||
124 | private function setContactInfo(): void |
||
125 | { |
||
126 | $this->setStarredComment(DocumentationInterface::OA_CONTACT . PhpInterface::OPEN_PARENTHESES, |
||
127 | 1, 1); |
||
128 | |||
129 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_EMAIL]) === false) { |
||
130 | $this->setStarredComment('email="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_EMAIL] . '",', |
||
131 | 1, 2); |
||
132 | } |
||
133 | |||
134 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_NAME]) === false) { |
||
135 | $this->setStarredComment('name="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_NAME] . '",', |
||
136 | 1, 2); |
||
137 | } |
||
138 | |||
139 | if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_URL]) === false) { |
||
140 | $this->setStarredComment('url="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_URL] . '",', |
||
141 | 1, 2); |
||
142 | } |
||
143 | |||
144 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Sets doc comments for every controller |
||
149 | */ |
||
150 | protected function setControllersDocs(): void |
||
151 | { |
||
152 | $this->setComment(DefaultInterface::METHOD_START); |
||
153 | |||
154 | $this->setIndex(); |
||
155 | |||
156 | $this->setView(); |
||
157 | |||
158 | $this->setCreate(); |
||
159 | |||
160 | $this->setUpdate(); |
||
161 | |||
162 | $this->setDelete(); |
||
163 | |||
164 | $this->setRelated(); |
||
165 | |||
166 | $this->setRelations(); |
||
167 | |||
168 | $this->setCreateRelation(); |
||
169 | |||
170 | $this->setUpdateRelation(); |
||
171 | |||
172 | $this->setDeleteRelation(); |
||
173 | |||
174 | $this->setCreateBulk(); |
||
175 | |||
176 | $this->setUpdateBulk(); |
||
177 | |||
178 | $this->setDeleteBulk(); |
||
179 | |||
180 | $this->setComment(DefaultInterface::METHOD_END); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Sets OAS documentation for an index method |
||
185 | */ |
||
186 | private function setIndex(): void |
||
187 | { |
||
188 | $this->openComment(); |
||
189 | |||
190 | $this->setStarredComment(DocumentationInterface::OA_GET . PhpInterface::OPEN_PARENTHESES); |
||
191 | |||
192 | $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH |
||
193 | . MigrationsHelper::getTableName($this->generator->objectName) . '",', 1, 1); |
||
194 | |||
195 | $this->setStarredComment('summary="Get ' . Classes::getClassName($this->generator->objectName) . 's ",', 1, 1); |
||
196 | |||
197 | $this->setStarredComment('tags={"' . Classes::getClassName($this->generator->objectName) . DefaultInterface::CONTROLLER_POSTFIX |
||
198 | . '"},', 1, 1); |
||
199 | |||
200 | // define params |
||
201 | $this->setParameter([ |
||
202 | 'in' => '"query"', |
||
203 | 'name' => '"include"', |
||
204 | 'required' => 'false', |
||
205 | ]); |
||
206 | |||
207 | $this->setParameter([ |
||
208 | 'in' => '"query"', |
||
209 | 'name' => '"page"', |
||
210 | 'required' => 'false', |
||
211 | ], 'integer'); |
||
212 | |||
213 | $this->setParameter([ |
||
214 | 'in' => '"query"', |
||
215 | 'name' => '"limit"', |
||
216 | 'required' => 'false', |
||
217 | ], 'integer'); |
||
218 | |||
219 | $this->setParameter([ |
||
220 | 'in' => '"query"', |
||
221 | 'name' => '"sort"', |
||
222 | 'required' => 'false', |
||
223 | ]); |
||
224 | |||
225 | $this->setParameter([ |
||
226 | 'in' => '"query"', |
||
227 | 'name' => '"data"', |
||
228 | 'required' => 'false', |
||
229 | ]); |
||
230 | |||
231 | $this->setParameter([ |
||
232 | 'in' => '"query"', |
||
233 | 'name' => '"filter"', |
||
234 | 'required' => 'false', |
||
235 | ]); |
||
236 | |||
237 | $this->setParameter([ |
||
238 | 'in' => '"query"', |
||
239 | 'name' => '"order_by"', |
||
240 | 'required' => 'false', |
||
241 | ]); |
||
242 | |||
243 | $this->setResponse([ |
||
244 | 'response' => '"' . JSONApiInterface::HTTP_RESPONSE_CODE_OK . '"', |
||
245 | 'description' => '"' . self::SUCCESSFUL_OPERATION . '"', |
||
246 | ]); |
||
247 | |||
248 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
249 | |||
250 | $this->closeComment(); |
||
251 | $this->setNewLines(); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Sets OAS documentation for a view method |
||
256 | */ |
||
257 | private function setView(): void |
||
258 | { |
||
259 | $this->openComment(); |
||
260 | |||
261 | $this->setStarredComment(DocumentationInterface::OA_GET . PhpInterface::OPEN_PARENTHESES); |
||
262 | |||
263 | $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH |
||
264 | . MigrationsHelper::getTableName($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1); |
||
265 | |||
266 | $this->setStarredComment('summary="Get ' . Classes::getClassName($this->generator->objectName) . '",', 1, 1); |
||
267 | |||
268 | $this->setStarredComment('tags={"' . Classes::getClassName($this->generator->objectName) . DefaultInterface::CONTROLLER_POSTFIX |
||
269 | . '"},', 1, 1); |
||
270 | |||
271 | $this->setParameter([ |
||
272 | 'in' => '"query"', |
||
273 | 'name' => '"include"', |
||
274 | 'required' => 'false', |
||
275 | ]); |
||
276 | |||
277 | $this->setParameter([ |
||
278 | 'in' => '"query"', |
||
279 | 'name' => '"data"', |
||
280 | 'required' => 'false', |
||
281 | ]); |
||
282 | |||
283 | $this->setResponse([ |
||
284 | 'response' => '"' . JSONApiInterface::HTTP_RESPONSE_CODE_OK . '"', |
||
285 | 'description' => '"' . self::SUCCESSFUL_OPERATION . '"', |
||
286 | ]); |
||
287 | |||
288 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
289 | |||
290 | $this->closeComment(); |
||
291 | $this->setNewLines(); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Sets OAS documentation for a create method |
||
296 | */ |
||
297 | private function setCreate(): void |
||
298 | { |
||
299 | $this->openComment(); |
||
300 | |||
301 | $this->setStarredComment(DocumentationInterface::OA_POST . PhpInterface::OPEN_PARENTHESES); |
||
302 | |||
303 | $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH |
||
304 | . MigrationsHelper::getTableName($this->generator->objectName) . '",', 1, 1); |
||
305 | |||
306 | $this->setStarredComment('summary="Create ' . Classes::getClassName($this->generator->objectName) . '",', 1, 1); |
||
307 | |||
308 | $this->setStarredComment('tags={"' . Classes::getClassName($this->generator->objectName) . DefaultInterface::CONTROLLER_POSTFIX |
||
309 | . '"},', 1, 1); |
||
310 | |||
311 | $this->setResponse([ |
||
312 | 'response' => '"' . JSONApiInterface::HTTP_RESPONSE_CODE_CREATED . '"', |
||
313 | 'description' => '"' . self::SUCCESSFUL_OPERATION . '"', |
||
314 | ]); |
||
315 | |||
316 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
317 | |||
318 | $this->closeComment(); |
||
319 | $this->setNewLines(); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Sets OAS documentation for an update method |
||
324 | */ |
||
325 | private function setUpdate(): void |
||
326 | { |
||
327 | $this->openComment(); |
||
328 | |||
329 | $this->setStarredComment(DocumentationInterface::OA_PATCH . PhpInterface::OPEN_PARENTHESES); |
||
330 | |||
331 | $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH |
||
332 | . MigrationsHelper::getTableName($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1); |
||
333 | |||
334 | $this->setStarredComment('summary="Update ' . Classes::getClassName($this->generator->objectName) . '",', 1, 1); |
||
335 | |||
336 | $this->setStarredComment('tags={"' . Classes::getClassName($this->generator->objectName) . DefaultInterface::CONTROLLER_POSTFIX |
||
337 | . '"},', 1, 1); |
||
338 | |||
339 | $this->setResponse([ |
||
340 | 'response' => '"' . JSONApiInterface::HTTP_RESPONSE_CODE_OK . '"', |
||
341 | 'description' => '"' . self::SUCCESSFUL_OPERATION . '"', |
||
342 | ]); |
||
343 | |||
344 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
345 | |||
346 | $this->closeComment(); |
||
347 | $this->setNewLines(); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Sets OAS documentation for a delete method |
||
352 | */ |
||
353 | private function setDelete(): void |
||
354 | { |
||
355 | $this->openComment(); |
||
356 | |||
357 | $this->setStarredComment(DocumentationInterface::OA_DELETE . PhpInterface::OPEN_PARENTHESES); |
||
358 | |||
359 | $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH |
||
360 | . MigrationsHelper::getTableName($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1); |
||
361 | |||
362 | $this->setStarredComment('summary="Delete ' . Classes::getClassName($this->generator->objectName) . '",', 1, 1); |
||
363 | |||
364 | $this->setStarredComment('tags={"' . Classes::getClassName($this->generator->objectName) . DefaultInterface::CONTROLLER_POSTFIX |
||
365 | . '"},', 1, 1); |
||
366 | |||
367 | $this->setResponse([ |
||
368 | 'response' => '"' . JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT . '"', |
||
369 | 'description' => '"' . self::SUCCESSFUL_OPERATION . '"', |
||
370 | ]); |
||
371 | |||
372 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES); |
||
373 | |||
374 | $this->closeComment(); |
||
375 | $this->setNewLines(); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Sets any params of methods |
||
380 | * |
||
381 | * @param array $paramValues |
||
382 | * @param string $schemaType |
||
383 | */ |
||
384 | private function setParameter(array $paramValues, string $schemaType = 'string'): void |
||
385 | { |
||
386 | $this->setStarredComment(DocumentationInterface::OA_PARAMETER . PhpInterface::OPEN_PARENTHESES, 1, 1); |
||
387 | foreach ($paramValues as $key => $val) { |
||
388 | $this->setStarredComment($key . '=' . $val . ',', 1, 2); |
||
389 | } |
||
390 | |||
391 | $this->setSchema($schemaType); |
||
392 | |||
393 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Sets the parameter schema |
||
398 | * |
||
399 | * @param string $schemaType |
||
400 | */ |
||
401 | private function setSchema(string $schemaType): void |
||
402 | { |
||
403 | $this->setStarredComment(DocumentationInterface::OA_SCHEMA . PhpInterface::OPEN_PARENTHESES, 1, 2); |
||
404 | |||
405 | $this->setStarredComment('type="' . $schemaType . '",', 1, 3); |
||
406 | |||
407 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 2); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Sets any response of method |
||
412 | * |
||
413 | * @param array $paramValues |
||
414 | */ |
||
415 | private function setResponse(array $paramValues): void |
||
416 | { |
||
417 | $this->setStarredComment(DocumentationInterface::OA_RESPONSE . PhpInterface::OPEN_PARENTHESES, 1, 1); |
||
418 | foreach ($paramValues as $key => $val) { |
||
419 | $this->setStarredComment($key . '=' . $val . ',', 1, 2); |
||
420 | } |
||
421 | |||
422 | // set media type |
||
423 | $this->setStarredComment(DocumentationInterface::OA_MEDIA_TYPE . PhpInterface::OPEN_PARENTHESES, 1, 2); |
||
424 | $this->setStarredComment('mediaType="' . JSONApiInterface::HEADER_APP_VND_API_JSON . '"', 1, 3); |
||
425 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 2); |
||
426 | |||
427 | $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1); |
||
428 | } |
||
429 | } |