@@ -8163,7 +8163,7 @@ public function __construct(ReflectionService $reflection, $base)
8163
8163
{
8164
8164
$ this ->openapi = new OpenApiDefinition ($ base );
8165
8165
$ this ->records = new OpenApiRecordsBuilder ($ this ->openapi , $ reflection );
8166
- $ this ->columns = new OpenApiColumnsBuilder ($ this ->openapi , $ reflection );
8166
+ $ this ->columns = new OpenApiColumnsBuilder ($ this ->openapi );
8167
8167
}
8168
8168
8169
8169
private function getServerUrl (): string
@@ -8199,103 +8199,152 @@ public function build(): OpenApiDefinition
8199
8199
class OpenApiColumnsBuilder
8200
8200
{
8201
8201
private $ openapi ;
8202
- private $ reflection ;
8203
8202
private $ operations = [
8204
- 'list ' => 'get ' ,
8205
- 'create ' => 'post ' ,
8206
- 'read ' => 'get ' ,
8207
- 'update ' => 'put ' ,
8208
- 'delete ' => 'delete ' ,
8203
+ 'database ' => [
8204
+ 'read ' => 'get ' ,
8205
+ ],
8206
+ 'table ' => [
8207
+ 'create ' => 'post ' ,
8208
+ 'read ' => 'get ' ,
8209
+ 'update ' => 'put ' , //rename
8210
+ 'delete ' => 'delete ' ,
8211
+ ],
8212
+ 'column ' => [
8213
+ 'create ' => 'post ' ,
8214
+ 'read ' => 'get ' ,
8215
+ 'update ' => 'put ' ,
8216
+ 'delete ' => 'delete ' ,
8217
+ ]
8209
8218
];
8210
8219
8211
- public function __construct (OpenApiDefinition $ openapi, ReflectionService $ reflection )
8220
+ public function __construct (OpenApiDefinition $ openapi )
8212
8221
{
8213
8222
$ this ->openapi = $ openapi ;
8214
- $ this ->reflection = $ reflection ;
8215
8223
}
8216
8224
8217
8225
public function build () /*: void*/
8218
8226
{
8219
- $ tableNames = $ this ->reflection ->getTableNames ();
8220
- foreach ($ tableNames as $ tableName ) {
8221
- $ this ->setPath ($ tableName );
8227
+ $ this ->setPaths ();
8228
+ $ this ->openapi ->set ("components|responses|boolSuccess|description " , "boolean indicating success or failure " );
8229
+ $ this ->openapi ->set ("components|responses|boolSuccess|content|application/json|schema|type " , "boolean " );
8230
+ $ this ->setComponentSchema ();
8231
+ $ this ->setComponentResponse ();
8232
+ $ this ->setComponentRequestBody ();
8233
+ $ this ->setComponentParameters ();
8234
+ foreach (array_keys ($ this ->operations ) as $ index => $ type ) {
8235
+ $ this ->setTag ($ index , $ type );
8222
8236
}
8223
8237
}
8224
8238
8225
- private function isOperationOnTableAllowed ( string $ operation , string $ tableName ): bool
8239
+ private function setPaths () /*: void*/
8226
8240
{
8227
- $ tableHandler = VariableStore::get ('authorization.tableHandler ' );
8228
- if (!$ tableHandler ) {
8229
- return true ;
8241
+ foreach (array_keys ($ this ->operations ) as $ type ) {
8242
+ foreach ($ this ->operations [$ type ] as $ operation => $ method ) {
8243
+ $ parameters = [];
8244
+ switch ($ type ) {
8245
+ case 'database ' :
8246
+ $ path = '/columns ' ;
8247
+ break ;
8248
+ case 'table ' :
8249
+ $ path = $ operation == 'create ' ? '/columns ' : '/columns/{table} ' ;
8250
+ break ;
8251
+ case 'column ' :
8252
+ $ path = $ operation == 'create ' ? '/columns/{table} ' : '/columns/{table}/{column} ' ;
8253
+ break ;
8254
+ }
8255
+ if (strpos ($ path , '{table} ' )) {
8256
+ $ parameters [] = 'table ' ;
8257
+ }
8258
+ if (strpos ($ path , '{column} ' )) {
8259
+ $ parameters [] = 'column ' ;
8260
+ }
8261
+ foreach ($ parameters as $ p => $ parameter ) {
8262
+ $ this ->openapi ->set ("paths| $ path| $ method|parameters| $ p| \$ref " , "#/components/parameters/ $ parameter " );
8263
+ }
8264
+ $ operationType = $ operation . ucfirst ($ type );
8265
+ if (in_array ($ operation , ['create ' , 'update ' ])) {
8266
+ $ this ->openapi ->set ("paths| $ path| $ method|requestBody| \$ref " , "#/components/requestBodies/ $ operationType " );
8267
+ }
8268
+ $ this ->openapi ->set ("paths| $ path| $ method|tags|0 " , "$ type " );
8269
+ $ this ->openapi ->set ("paths| $ path| $ method|description " , "$ operation $ type " );
8270
+ switch ($ operation ) {
8271
+ case 'read ' :
8272
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/ $ operationType " );
8273
+ break ;
8274
+ case 'create ' :
8275
+ case 'update ' :
8276
+ case 'delete ' :
8277
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/boolSuccess " );
8278
+ break ;
8279
+ }
8280
+ }
8230
8281
}
8231
- return (bool ) call_user_func ($ tableHandler , $ operation , $ tableName );
8232
8282
}
8233
8283
8234
- private function isOperationOnColumnAllowed ( string $ operation , string $ tableName , string $ columnName ): bool
8284
+ private function setComponentSchema () /*: void*/
8235
8285
{
8236
- $ columnHandler = VariableStore::get ('authorization.columnHandler ' );
8237
- if (!$ columnHandler ) {
8238
- return true ;
8286
+ foreach (array_keys ($ this ->operations ) as $ type ) {
8287
+ foreach (array_keys ($ this ->operations [$ type ]) as $ operation ) {
8288
+ if ($ operation == 'delete ' ) {
8289
+ continue ;
8290
+ }
8291
+ $ operationType = $ operation . ucfirst ($ type );
8292
+ $ prefix = "components|schemas| $ operationType " ;
8293
+ $ this ->openapi ->set ("$ prefix|type " , "object " );
8294
+ $ this ->openapi ->set ("$ prefix|properties|name|type " , 'string ' );
8295
+ $ this ->openapi ->set ("$ prefix|properties|type|type " , 'string ' );
8296
+ }
8239
8297
}
8240
- return (bool ) call_user_func ($ columnHandler , $ operation , $ tableName , $ columnName );
8241
8298
}
8242
8299
8243
- private function setPath ( string $ tableName ) /*: void*/
8300
+ private function setComponentResponse ( ) /*: void*/
8244
8301
{
8245
- $ table = $ this ->reflection ->getTable ($ tableName );
8246
- $ type = $ table ->getType ();
8247
- foreach ($ this ->operations as $ operation => $ method ) {
8248
- if ($ type != 'table ' && $ method != 'get ' ) {
8249
- continue ;
8250
- }
8251
- $ action = $ operation == 'get ' ? 'reflect ' : 'remodel ' ;
8252
- if (!$ this ->isOperationOnTableAllowed ($ action , $ tableName )) {
8253
- continue ;
8254
- }
8255
- $ parameters = [];
8256
- if (in_array ($ operation , ['list ' , 'create ' ])) {
8257
- $ path = sprintf ('/records/%s ' , $ tableName );
8258
- if ($ operation == 'list ' ) {
8259
- $ parameters = ['filter ' , 'include ' , 'exclude ' , 'order ' , 'size ' , 'page ' , 'join ' ];
8260
- }
8261
- } else {
8262
- $ path = sprintf ('/records/%s/{%s} ' , $ tableName );
8263
- if ($ operation == 'read ' ) {
8264
- $ parameters = ['pk ' , 'include ' , 'exclude ' , 'join ' ];
8265
- } else {
8266
- $ parameters = ['pk ' ];
8302
+ foreach (array_keys ($ this ->operations ) as $ type ) {
8303
+ foreach (array_keys ($ this ->operations [$ type ]) as $ operation ) {
8304
+ if ($ operation != 'read ' ) {
8305
+ continue ;
8267
8306
}
8307
+ $ operationType = $ operation . ucfirst ($ type );
8308
+ $ this ->openapi ->set ("components|responses| $ operationType|description " , "single $ type record " );
8309
+ $ this ->openapi ->set ("components|responses| $ operationType|content|application/json|schema| \$ref " , "#/components/schemas/ $ operationType " );
8268
8310
}
8269
- foreach ($ parameters as $ p => $ parameter ) {
8270
- $ this ->openapi ->set ("paths| $ path| $ method|parameters| $ p| \$ref " , "#/components/parameters/ $ parameter " );
8271
- }
8272
- if (in_array ($ operation , ['create ' , 'update ' , 'increment ' ])) {
8273
- $ this ->openapi ->set ("paths| $ path| $ method|requestBody| \$ref " , "#/components/requestBodies/ $ operation- " . rawurlencode ($ tableName ));
8274
- }
8275
- $ this ->openapi ->set ("paths| $ path| $ method|tags|0 " , "$ tableName " );
8276
- $ this ->openapi ->set ("paths| $ path| $ method|description " , "$ operation $ tableName " );
8277
- switch ($ operation ) {
8278
- case 'list ' :
8279
- $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/ $ operation- " . rawurlencode ($ tableName ));
8280
- break ;
8281
- case 'create ' :
8282
- if ($ pk ->getType () == 'integer ' ) {
8283
- $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/pk_integer " );
8284
- } else {
8285
- $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/pk_string " );
8286
- }
8287
- break ;
8288
- case 'read ' :
8289
- $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/ $ operation- " . rawurlencode ($ tableName ));
8290
- break ;
8291
- case 'update ' :
8292
- case 'delete ' :
8293
- case 'increment ' :
8294
- $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/rows_affected " );
8295
- break ;
8311
+ }
8312
+ }
8313
+
8314
+ private function setComponentRequestBody () /*: void*/
8315
+ {
8316
+ foreach (array_keys ($ this ->operations ) as $ type ) {
8317
+ foreach (array_keys ($ this ->operations [$ type ]) as $ operation ) {
8318
+ if (!in_array ($ operation , ['create ' , 'update ' ])) {
8319
+ continue ;
8320
+ }
8321
+ $ operationType = $ operation . ucfirst ($ type );
8322
+ $ this ->openapi ->set ("components|requestBodies| $ operationType|description " , "single $ type record " );
8323
+ $ this ->openapi ->set ("components|requestBodies| $ operationType|content|application/json|schema| \$ref " , "#/components/schemas/ $ operationType " );
8296
8324
}
8297
8325
}
8298
8326
}
8327
+
8328
+ private function setComponentParameters () /*: void*/
8329
+ {
8330
+ $ this ->openapi ->set ("components|parameters|table|name " , "table " );
8331
+ $ this ->openapi ->set ("components|parameters|table|in " , "path " );
8332
+ $ this ->openapi ->set ("components|parameters|table|schema|type " , "string " );
8333
+ $ this ->openapi ->set ("components|parameters|table|description " , "table name " );
8334
+ $ this ->openapi ->set ("components|parameters|table|required " , true );
8335
+
8336
+ $ this ->openapi ->set ("components|parameters|column|name " , "column " );
8337
+ $ this ->openapi ->set ("components|parameters|column|in " , "path " );
8338
+ $ this ->openapi ->set ("components|parameters|column|schema|type " , "string " );
8339
+ $ this ->openapi ->set ("components|parameters|column|description " , "column name " );
8340
+ $ this ->openapi ->set ("components|parameters|column|required " , true );
8341
+ }
8342
+
8343
+ private function setTag (int $ index , string $ type ) /*: void*/
8344
+ {
8345
+ $ this ->openapi ->set ("tags| $ index|name " , "$ type " );
8346
+ $ this ->openapi ->set ("tags| $ index|description " , "$ type operations " );
8347
+ }
8299
8348
}
8300
8349
}
8301
8350
0 commit comments