33
33
use ApiPlatform \Metadata \Post ;
34
34
use ApiPlatform \Metadata \Put ;
35
35
use ApiPlatform \Metadata \Resource \ResourceMetadataCollection ;
36
+ use ApiPlatform \State \CreateProvider ;
36
37
use Psr \Log \LoggerInterface ;
37
38
use Psr \Log \NullLogger ;
38
39
use Symfony \Component \Serializer \NameConverter \CamelCaseToSnakeCaseNameConverter ;
@@ -130,8 +131,9 @@ private function buildResourceOperations(array $attributes, string $resourceClas
130
131
// Loop again and set default operations if none where found
131
132
foreach ($ resources as $ index => $ resource ) {
132
133
$ operations = [];
133
- foreach ($ resource ->getOperations () ?? [new Get (), new GetCollection (), new Post (), new Put (), new Patch (), new Delete ()] as $ i => $ operation ) {
134
- [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resource , $ operation );
134
+
135
+ foreach ($ resource ->getOperations () ?? $ this ->getDefaultHttpOperations ($ resource ) as $ i => $ operation ) {
136
+ [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resource , $ operation , $ resource ->getOperations () ? false : true );
135
137
$ operations [$ key ] = $ operation ;
136
138
}
137
139
@@ -162,7 +164,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
162
164
return $ resources ;
163
165
}
164
166
165
- private function getOperationWithDefaults (ApiResource $ resource , Operation $ operation ): array
167
+ private function getOperationWithDefaults (ApiResource $ resource , Operation $ operation, bool $ generated = false ): array
166
168
{
167
169
// Inherit from resource defaults
168
170
foreach (get_class_methods ($ resource ) as $ methodName ) {
@@ -181,7 +183,11 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
181
183
$ operation = $ operation ->{'with ' .substr ($ methodName , 3 )}($ value );
182
184
}
183
185
184
- $ operation = $ operation ->withExtraProperties (array_merge ($ resource ->getExtraProperties (), $ operation ->getExtraProperties ()));
186
+ $ operation = $ operation ->withExtraProperties (array_merge (
187
+ $ resource ->getExtraProperties (),
188
+ $ operation ->getExtraProperties (),
189
+ $ generated ? ['generated_operation ' => true ] : []
190
+ ));
185
191
186
192
// Add global defaults attributes to the operation
187
193
$ operation = $ this ->addGlobalDefaults ($ operation );
@@ -312,4 +318,14 @@ private function addDefaultGraphQlOperations(ApiResource $resource): ApiResource
312
318
313
319
return $ resource ->withGraphQlOperations ($ graphQlOperations );
314
320
}
321
+
322
+ private function getDefaultHttpOperations ($ resource ): iterable
323
+ {
324
+ $ post = new Post ();
325
+ if ($ resource ->getUriTemplate () && !$ resource ->getProvider ()) {
326
+ $ post = $ post ->withProvider (CreateProvider::class);
327
+ }
328
+
329
+ return [new Get (), new GetCollection (), $ post , new Put (), new Patch (), new Delete ()];
330
+ }
315
331
}
0 commit comments