25
25
use ApiPlatform \Util \ContentNegotiationTrait ;
26
26
use Negotiation \Negotiator ;
27
27
use Symfony \Component \HttpFoundation \Request ;
28
+ use Symfony \Component \HttpFoundation \Response ;
28
29
29
30
/**
30
31
* Generates the API documentation.
@@ -49,45 +50,59 @@ public function __construct(
49
50
}
50
51
51
52
/**
52
- * @return DocumentationInterface|OpenApi
53
+ * @return DocumentationInterface|OpenApi|Response
53
54
*/
54
55
public function __invoke (Request $ request = null )
55
56
{
56
- $ context = [];
57
- if (null !== $ request ) {
58
- $ isGateway = $ request ->query ->getBoolean (ApiGatewayNormalizer::API_GATEWAY );
59
- $ context ['api_gateway ' ] = $ isGateway ;
60
- $ context ['base_url ' ] = $ request ->getBaseUrl ();
61
- $ request ->attributes ->set ('_api_normalization_context ' , $ request ->attributes ->get ('_api_normalization_context ' , []) + $ context );
62
- $ format = $ this ->getRequestFormat ($ request , ['json ' => ['application/json ' ], 'jsonld ' => ['application/ld+json ' ], 'html ' => ['text/html ' ]]);
63
-
64
- if ('html ' === $ format || 'json ' === $ format && null !== $ this ->openApiFactory ) {
65
- if ($ this ->provider && $ this ->processor ) {
66
- $ context ['request ' ] = $ request ;
67
- $ operation = new Get (class: OpenApi::class, provider: fn () => $ this ->openApiFactory ->__invoke ($ context ), normalizationContext: [ApiGatewayNormalizer::API_GATEWAY => $ isGateway ]);
68
- if ('html ' === $ format ) {
69
- $ operation = $ operation ->withProcessor ('api_platform.swagger_ui.processor ' )->withWrite (true );
70
- }
71
-
72
- $ body = $ this ->provider ->provide ($ operation , [], $ context );
73
-
74
- return $ this ->processor ->process ($ body , $ operation , [], $ context );
75
- }
76
-
77
- return $ this ->openApiFactory ->__invoke ($ context );
57
+ if (null === $ request ) {
58
+ return new Documentation ($ this ->resourceNameCollectionFactory ->create (), $ this ->title , $ this ->description , $ this ->version );
59
+ }
60
+
61
+ $ context = ['api_gateway ' => $ request ->query ->getBoolean (ApiGatewayNormalizer::API_GATEWAY ), 'base_url ' => $ request ->getBaseUrl ()];
62
+ $ request ->attributes ->set ('_api_normalization_context ' , $ request ->attributes ->get ('_api_normalization_context ' , []) + $ context );
63
+ $ format = $ this ->getRequestFormat ($ request , ['json ' => ['application/json ' ], 'jsonld ' => ['application/ld+json ' ], 'html ' => ['text/html ' ]]);
64
+
65
+ if (null !== $ this ->openApiFactory && ('html ' === $ format || 'json ' === $ format )) {
66
+ return $ this ->getOpenApiDocumentation ($ context , $ format , $ request );
67
+ }
68
+
69
+ return $ this ->getHydraDocumentation ($ context , $ request );
70
+ }
71
+
72
+ /**
73
+ * @param array<string,mixed> $context
74
+ */
75
+ private function getOpenApiDocumentation (array $ context , string $ format , Request $ request ): OpenApi |Response
76
+ {
77
+ if ($ this ->provider && $ this ->processor ) {
78
+ $ context ['request ' ] = $ request ;
79
+ $ operation = new Get (class: OpenApi::class, provider: fn () => $ this ->openApiFactory ->__invoke ($ context ), normalizationContext: [ApiGatewayNormalizer::API_GATEWAY => $ context ['api_gateway ' ] ?? null ]);
80
+ if ('html ' === $ format ) {
81
+ $ operation = $ operation ->withProcessor ('api_platform.swagger_ui.processor ' )->withWrite (true );
78
82
}
83
+
84
+ return $ this ->processor ->process ($ this ->provider ->provide ($ operation , [], $ context ), $ operation , [], $ context );
79
85
}
80
86
87
+ return $ this ->openApiFactory ->__invoke ($ context );
88
+ }
89
+
90
+ /**
91
+ * TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
92
+ * We should transform this to a provider, it'd improve performances also by a bit.
93
+ *
94
+ * @param array<string,mixed> $context
95
+ */
96
+ private function getHydraDocumentation (array $ context , Request $ request ): DocumentationInterface |Response
97
+ {
81
98
if ($ this ->provider && $ this ->processor ) {
82
99
$ context ['request ' ] = $ request ;
83
100
$ operation = new Get (
84
101
class: Documentation::class,
85
- provider: fn () => new Documentation ($ this ->resourceNameCollectionFactory ->create (), $ this ->title , $ this ->description , $ this ->version ),
86
- normalizationContext: [ApiGatewayNormalizer::API_GATEWAY => $ isGateway ?? false ]
102
+ provider: fn () => new Documentation ($ this ->resourceNameCollectionFactory ->create (), $ this ->title , $ this ->description , $ this ->version )
87
103
);
88
- $ body = $ this ->provider ->provide ($ operation , [], $ context );
89
104
90
- return $ this ->processor ->process ($ body , $ operation , [], $ context );
105
+ return $ this ->processor ->process ($ this -> provider -> provide ( $ operation , [], $ context ) , $ operation , [], $ context );
91
106
}
92
107
93
108
return new Documentation ($ this ->resourceNameCollectionFactory ->create (), $ this ->title , $ this ->description , $ this ->version );
0 commit comments