16
16
use ApiPlatform \Api \UrlGeneratorInterface ;
17
17
use ApiPlatform \Core \Annotation \ApiResource as LegacyApiResource ;
18
18
use ApiPlatform \Core \Annotation \ApiSubresource ;
19
+ use ApiPlatform \Core \Api \IdentifiersExtractorInterface ;
19
20
use ApiPlatform \Metadata \ApiResource ;
20
21
use ApiPlatform \Metadata \Delete ;
21
22
use ApiPlatform \Metadata \Get ;
22
23
use ApiPlatform \Metadata \GetCollection ;
23
24
use ApiPlatform \Metadata \GraphQl \Mutation ;
24
25
use ApiPlatform \Metadata \GraphQl \Query ;
25
26
use ApiPlatform \Metadata \GraphQl \QueryCollection ;
27
+ use ApiPlatform \Metadata \Link ;
26
28
use ApiPlatform \Metadata \Patch ;
27
29
use ApiPlatform \Metadata \Post ;
28
30
use ApiPlatform \Metadata \Put ;
@@ -36,12 +38,16 @@ final class UpgradeApiResourceVisitor extends NodeVisitorAbstract
36
38
use RemoveAnnotationTrait;
37
39
38
40
private LegacyApiResource $ resourceAnnotation ;
41
+ private IdentifiersExtractorInterface $ identifiersExtractor ;
39
42
private bool $ isAnnotation = false ;
43
+ private string $ resourceClass ;
40
44
41
- public function __construct (LegacyApiResource $ resourceAnnotation , bool $ isAnnotation = false )
45
+ public function __construct (LegacyApiResource $ resourceAnnotation , bool $ isAnnotation, IdentifiersExtractorInterface $ identifiersExtractor , string $ resourceClass )
42
46
{
43
47
$ this ->resourceAnnotation = $ resourceAnnotation ;
44
48
$ this ->isAnnotation = $ isAnnotation ;
49
+ $ this ->identifiersExtractor = $ identifiersExtractor ;
50
+ $ this ->resourceClass = $ resourceClass ;
45
51
}
46
52
47
53
/**
@@ -80,6 +86,10 @@ public function enterNode(Node $node)
80
86
$ this ->getGraphQlOperationsNamespaces ($ this ->resourceAnnotation ->graphql ?? [])
81
87
));
82
88
89
+ if (true === !($ this ->resourceAnnotation ->attributes ['composite_identifier ' ] ?? true )) {
90
+ $ namespaces [] = Link::class;
91
+ }
92
+
83
93
foreach ($ node ->stmts as $ k => $ stmt ) {
84
94
if (!$ stmt instanceof Node \Stmt \Use_) {
85
95
break ;
@@ -202,6 +212,40 @@ public function enterNode(Node $node)
202
212
continue ;
203
213
}
204
214
215
+ if ('compositeIdentifier ' === $ key ) {
216
+ if (false !== $ value ) {
217
+ continue ;
218
+ }
219
+
220
+ $ identifiers = $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ this ->resourceClass );
221
+ $ identifierNodeItems = [];
222
+ foreach ($ identifiers as $ identifier ) {
223
+ $ identifierNodes = [
224
+ 'compositeIdentifier ' => new Node \Expr \ConstFetch (new Node \Name ('false ' )),
225
+ 'fromClass ' => new Node \Expr \ClassConstFetch (
226
+ new Node \Name (
227
+ 'self '
228
+ ),
229
+ 'class '
230
+ ),
231
+ 'identifiers ' => new Node \Expr \Array_ (
232
+ [
233
+ new Node \Expr \ArrayItem (new Node \Scalar \String_ ($ identifier )),
234
+ ],
235
+ ['kind ' => Node \Expr \Array_::KIND_SHORT ]
236
+ ),
237
+ ];
238
+
239
+ $ identifierNodeItems [] = new Node \Expr \ArrayItem (
240
+ new Node \Expr \New_ (new Node \Name ('Link ' ), $ this ->arrayToArguments ($ identifierNodes )),
241
+ new Node \Scalar \String_ ($ identifier )
242
+ );
243
+ }
244
+
245
+ $ arguments ['uriVariables ' ] = new Node \Expr \Array_ ($ identifierNodeItems , ['kind ' => Node \Expr \Array_::KIND_SHORT ]);
246
+ continue ;
247
+ }
248
+
205
249
$ arguments [$ key ] = $ this ->valueToNode ($ value );
206
250
}
207
251
0 commit comments