@@ -8154,6 +8154,123 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8154
8154
use Tqdev \PhpCrudApi \OpenApi \OpenApiDefinition ;
8155
8155
8156
8156
class OpenApiBuilder
8157
+ {
8158
+ private $ openapi ;
8159
+ private $ records ;
8160
+ private $ columns ;
8161
+
8162
+ public function __construct (ReflectionService $ reflection , $ base )
8163
+ {
8164
+ $ this ->openapi = new OpenApiDefinition ($ base );
8165
+ $ this ->records = new OpenApiRecordsBuilder ($ this ->openapi , $ reflection );
8166
+ $ this ->columns = new OpenApiColumnsBuilder ($ this ->openapi , $ reflection );
8167
+ }
8168
+
8169
+ private function getServerUrl (): string
8170
+ {
8171
+ $ protocol = @$ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] ?: @$ _SERVER ['REQUEST_SCHEME ' ] ?: ((isset ($ _SERVER ["HTTPS " ]) && $ _SERVER ["HTTPS " ] == "on " ) ? "https " : "http " );
8172
+ $ port = @intval ($ _SERVER ['HTTP_X_FORWARDED_PORT ' ]) ?: @intval ($ _SERVER ["SERVER_PORT " ]) ?: (($ protocol === 'https ' ) ? 443 : 80 );
8173
+ $ host = @explode (": " , $ _SERVER ['HTTP_HOST ' ])[0 ] ?: @$ _SERVER ['SERVER_NAME ' ] ?: @$ _SERVER ['SERVER_ADDR ' ];
8174
+ $ port = ($ protocol === 'https ' && $ port === 443 ) || ($ protocol === 'http ' && $ port === 80 ) ? '' : ': ' . $ port ;
8175
+ $ path = @trim (substr ($ _SERVER ['REQUEST_URI ' ], 0 , strpos ($ _SERVER ['REQUEST_URI ' ], '/openapi ' )), '/ ' );
8176
+ return sprintf ('%s://%s%s/%s ' , $ protocol , $ host , $ port , $ path );
8177
+ }
8178
+
8179
+ public function build () /*: void */
8180
+ {
8181
+ $ this ->openapi ->set ("openapi " , "3.0.0 " );
8182
+ if (!$ this ->openapi ->has ("servers " ) && isset ($ _SERVER ['REQUEST_URI ' ])) {
8183
+ $ this ->openapi ->set ("servers|0|url " , $ this ->getServerUrl ());
8184
+ }
8185
+ $ this ->records ->build ();
8186
+ }
8187
+
8188
+ public function getDefinition (): OpenApiDefinition
8189
+ {
8190
+ return $ this ->openapi ;
8191
+ }
8192
+ }
8193
+ }
8194
+
8195
+ // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiColumnsBuilder.php
8196
+ namespace Tqdev \PhpCrudApi \OpenApi {
8197
+
8198
+ use Tqdev \PhpCrudApi \Column \ReflectionService ;
8199
+ use Tqdev \PhpCrudApi \Middleware \Communication \VariableStore ;
8200
+ use Tqdev \PhpCrudApi \OpenApi \OpenApiDefinition ;
8201
+
8202
+ class OpenApiColumnsBuilder
8203
+ {
8204
+ private $ openapi ;
8205
+ private $ reflection ;
8206
+
8207
+ public function __construct (OpenApiDefinition $ openapi , ReflectionService $ reflection )
8208
+ {
8209
+ $ this ->openapi = $ openapi ;
8210
+ $ this ->reflection = $ reflection ;
8211
+ }
8212
+
8213
+ public function build () /*: void*/
8214
+ {
8215
+ }
8216
+ }
8217
+ }
8218
+
8219
+ // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
8220
+ namespace Tqdev \PhpCrudApi \OpenApi {
8221
+
8222
+ class OpenApiDefinition implements \JsonSerializable
8223
+ {
8224
+ private $ root ;
8225
+
8226
+ public function __construct ($ base )
8227
+ {
8228
+ $ this ->root = $ base ;
8229
+ }
8230
+
8231
+ public function set (string $ path , $ value ) /*: void*/
8232
+ {
8233
+ $ parts = explode ('| ' , trim ($ path , '| ' ));
8234
+ $ current = &$ this ->root ;
8235
+ while (count ($ parts ) > 0 ) {
8236
+ $ part = array_shift ($ parts );
8237
+ if (!isset ($ current [$ part ])) {
8238
+ $ current [$ part ] = [];
8239
+ }
8240
+ $ current = &$ current [$ part ];
8241
+ }
8242
+ $ current = $ value ;
8243
+ }
8244
+
8245
+ public function has (string $ path ): bool
8246
+ {
8247
+ $ parts = explode ('| ' , trim ($ path , '| ' ));
8248
+ $ current = &$ this ->root ;
8249
+ while (count ($ parts ) > 0 ) {
8250
+ $ part = array_shift ($ parts );
8251
+ if (!isset ($ current [$ part ])) {
8252
+ return false ;
8253
+ }
8254
+ $ current = &$ current [$ part ];
8255
+ }
8256
+ return true ;
8257
+ }
8258
+
8259
+ public function jsonSerialize ()
8260
+ {
8261
+ return $ this ->root ;
8262
+ }
8263
+ }
8264
+ }
8265
+
8266
+ // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiRecordsBuilder.php
8267
+ namespace Tqdev \PhpCrudApi \OpenApi {
8268
+
8269
+ use Tqdev \PhpCrudApi \Column \ReflectionService ;
8270
+ use Tqdev \PhpCrudApi \Middleware \Communication \VariableStore ;
8271
+ use Tqdev \PhpCrudApi \OpenApi \OpenApiDefinition ;
8272
+
8273
+ class OpenApiRecordsBuilder
8157
8274
{
8158
8275
private $ openapi ;
8159
8276
private $ reflection ;
@@ -8182,20 +8299,10 @@ class OpenApiBuilder
8182
8299
'boolean ' => ['type ' => 'boolean ' ],
8183
8300
];
8184
8301
8185
- public function __construct (ReflectionService $ reflection , $ base )
8302
+ public function __construct (OpenApiDefinition $ openapi , ReflectionService $ reflection )
8186
8303
{
8304
+ $ this ->openapi = $ openapi ;
8187
8305
$ this ->reflection = $ reflection ;
8188
- $ this ->openapi = new OpenApiDefinition ($ base );
8189
- }
8190
-
8191
- private function getServerUrl (): string
8192
- {
8193
- $ protocol = @$ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] ?: @$ _SERVER ['REQUEST_SCHEME ' ] ?: ((isset ($ _SERVER ["HTTPS " ]) && $ _SERVER ["HTTPS " ] == "on " ) ? "https " : "http " );
8194
- $ port = @intval ($ _SERVER ['HTTP_X_FORWARDED_PORT ' ]) ?: @intval ($ _SERVER ["SERVER_PORT " ]) ?: (($ protocol === 'https ' ) ? 443 : 80 );
8195
- $ host = @explode (": " , $ _SERVER ['HTTP_HOST ' ])[0 ] ?: @$ _SERVER ['SERVER_NAME ' ] ?: @$ _SERVER ['SERVER_ADDR ' ];
8196
- $ port = ($ protocol === 'https ' && $ port === 443 ) || ($ protocol === 'http ' && $ port === 80 ) ? '' : ': ' . $ port ;
8197
- $ path = @trim (substr ($ _SERVER ['REQUEST_URI ' ], 0 , strpos ($ _SERVER ['REQUEST_URI ' ], '/openapi ' )), '/ ' );
8198
- return sprintf ('%s://%s%s/%s ' , $ protocol , $ host , $ port , $ path );
8199
8306
}
8200
8307
8201
8308
private function getAllTableReferences (): array
@@ -8217,12 +8324,8 @@ private function getAllTableReferences(): array
8217
8324
return $ tableReferences ;
8218
8325
}
8219
8326
8220
- public function build (): OpenApiDefinition
8327
+ public function build () /*: void*/
8221
8328
{
8222
- $ this ->openapi ->set ("openapi " , "3.0.0 " );
8223
- if (!$ this ->openapi ->has ("servers " ) && isset ($ _SERVER ['REQUEST_URI ' ])) {
8224
- $ this ->openapi ->set ("servers|0|url " , $ this ->getServerUrl ());
8225
- }
8226
8329
$ tableNames = $ this ->reflection ->getTableNames ();
8227
8330
foreach ($ tableNames as $ tableName ) {
8228
8331
$ this ->setPath ($ tableName );
@@ -8247,7 +8350,6 @@ public function build(): OpenApiDefinition
8247
8350
foreach ($ tableNames as $ index => $ tableName ) {
8248
8351
$ this ->setTag ($ index , $ tableName );
8249
8352
}
8250
- return $ this ->openapi ;
8251
8353
}
8252
8354
8253
8355
private function isOperationOnTableAllowed (string $ operation , string $ tableName ): bool
@@ -8483,53 +8585,6 @@ private function setTag(int $index, string $tableName) /*: void*/
8483
8585
}
8484
8586
}
8485
8587
8486
- // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
8487
- namespace Tqdev \PhpCrudApi \OpenApi {
8488
-
8489
- class OpenApiDefinition implements \JsonSerializable
8490
- {
8491
- private $ root ;
8492
-
8493
- public function __construct ($ base )
8494
- {
8495
- $ this ->root = $ base ;
8496
- }
8497
-
8498
- public function set (string $ path , $ value ) /*: void*/
8499
- {
8500
- $ parts = explode ('| ' , trim ($ path , '| ' ));
8501
- $ current = &$ this ->root ;
8502
- while (count ($ parts ) > 0 ) {
8503
- $ part = array_shift ($ parts );
8504
- if (!isset ($ current [$ part ])) {
8505
- $ current [$ part ] = [];
8506
- }
8507
- $ current = &$ current [$ part ];
8508
- }
8509
- $ current = $ value ;
8510
- }
8511
-
8512
- public function has (string $ path ): bool
8513
- {
8514
- $ parts = explode ('| ' , trim ($ path , '| ' ));
8515
- $ current = &$ this ->root ;
8516
- while (count ($ parts ) > 0 ) {
8517
- $ part = array_shift ($ parts );
8518
- if (!isset ($ current [$ part ])) {
8519
- return false ;
8520
- }
8521
- $ current = &$ current [$ part ];
8522
- }
8523
- return true ;
8524
- }
8525
-
8526
- public function jsonSerialize ()
8527
- {
8528
- return $ this ->root ;
8529
- }
8530
- }
8531
- }
8532
-
8533
8588
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
8534
8589
namespace Tqdev \PhpCrudApi \OpenApi {
8535
8590
0 commit comments