19
19
use ApiPlatform \Core \Exception \RuntimeException ;
20
20
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
21
21
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceNameCollectionFactoryInterface ;
22
- use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
22
+ use ApiPlatform \Core \Metadata \Resource \OperationCollectionMetadata ;
23
23
use ApiPlatform \Core \Operation \Factory \SubresourceOperationFactoryInterface ;
24
24
use ApiPlatform \Core \PathResolver \OperationPathResolverInterface ;
25
25
use Symfony \Component \Config \FileLocator ;
@@ -92,63 +92,64 @@ public function load($data, $type = null): RouteCollection
92
92
$ this ->loadExternalFiles ($ routeCollection );
93
93
94
94
foreach ($ this ->resourceNameCollectionFactory ->create () as $ resourceClass ) {
95
- $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
96
- $ resourceMetadata = $ resourceMetadata ->withAttributes (($ resourceMetadata ->getAttributes () ?: []) + ['identified_by ' => $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resourceClass )]);
97
- $ resourceShortName = $ resourceMetadata ->getShortName ();
95
+ foreach ( $ this ->resourceMetadataFactory ->create ($ resourceClass ) as $ operationCollectionMetadata ) {
96
+ $ operationCollectionMetadata = $ operationCollectionMetadata ->withAttributes (($ operationCollectionMetadata ->getAttributes () ?: []) + ['identified_by ' => $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resourceClass )]);
97
+ $ resourceShortName = $ operationCollectionMetadata ->getShortName ();
98
98
99
- if (null === $ resourceShortName ) {
100
- throw new InvalidResourceException (sprintf ('Resource %s has no short name defined. ' , $ resourceClass ));
101
- }
99
+ if (null === $ resourceShortName ) {
100
+ throw new InvalidResourceException (sprintf ('Resource %s has no short name defined. ' , $ resourceClass ));
101
+ }
102
102
103
- if (null !== $ collectionOperations = $ resourceMetadata ->getCollectionOperations ()) {
104
- foreach ($ collectionOperations as $ operationName => $ operation ) {
105
- $ this ->addRoute ($ routeCollection , $ resourceClass , $ operationName , $ operation , $ resourceMetadata , OperationType::COLLECTION );
103
+ if (null !== $ collectionOperations = $ operationCollectionMetadata ->getCollectionOperations ()) {
104
+ foreach ($ collectionOperations as $ operationName => $ operation ) {
105
+ $ this ->addRoute ($ routeCollection , $ resourceClass , $ operationName , $ operation , $ operationCollectionMetadata , OperationType::COLLECTION );
106
+ }
106
107
}
107
- }
108
108
109
- if (null !== $ itemOperations = $ resourceMetadata ->getItemOperations ()) {
110
- foreach ($ itemOperations as $ operationName => $ operation ) {
111
- $ this ->addRoute ($ routeCollection , $ resourceClass , $ operationName , $ operation , $ resourceMetadata , OperationType::ITEM );
109
+ if (null !== $ itemOperations = $ operationCollectionMetadata ->getItemOperations ()) {
110
+ foreach ($ itemOperations as $ operationName => $ operation ) {
111
+ $ this ->addRoute ($ routeCollection , $ resourceClass , $ operationName , $ operation , $ operationCollectionMetadata , OperationType::ITEM );
112
+ }
112
113
}
113
- }
114
114
115
- if (null === $ this ->subresourceOperationFactory ) {
116
- continue ;
117
- }
115
+ if (null === $ this ->subresourceOperationFactory ) {
116
+ continue ;
117
+ }
118
118
119
- foreach ($ this ->subresourceOperationFactory ->create ($ resourceClass ) as $ operationId => $ operation ) {
120
- if (null === $ controller = $ operation ['controller ' ] ?? null ) {
121
- $ controller = self ::DEFAULT_ACTION_PATTERN .'get_subresource ' ;
119
+ foreach ($ this ->subresourceOperationFactory ->create ($ resourceClass ) as $ operationId => $ operation ) {
120
+ if (null === $ controller = $ operation ['controller ' ] ?? null ) {
121
+ $ controller = self ::DEFAULT_ACTION_PATTERN .'get_subresource ' ;
122
122
123
- if (!$ this ->container ->has ($ controller )) {
124
- throw new RuntimeException (sprintf ('There is no builtin action for the %s %s operation. You need to define the controller yourself. ' , OperationType::SUBRESOURCE , 'GET ' ));
123
+ if (!$ this ->container ->has ($ controller )) {
124
+ throw new RuntimeException (sprintf ('There is no builtin action for the %s %s operation. You need to define the controller yourself. ' , OperationType::SUBRESOURCE , 'GET ' ));
125
+ }
125
126
}
126
- }
127
127
128
- $ routeCollection ->add ($ operation ['route_name ' ], new Route (
129
- $ operation ['path ' ],
130
- [
131
- '_controller ' => $ controller ,
132
- '_format ' => null ,
133
- '_stateless ' => $ operation ['stateless ' ] ?? $ resourceMetadata ->getAttribute ('stateless ' ),
134
- '_api_resource_class ' => $ operation ['resource_class ' ],
135
- '_api_identified_by ' => $ operation ['identified_by ' ],
136
- '_api_has_composite_identifier ' => false ,
137
- '_api_subresource_operation_name ' => $ operation ['route_name ' ],
138
- '_api_subresource_context ' => [
139
- 'property ' => $ operation ['property ' ],
140
- 'identifiers ' => $ operation ['identifiers ' ],
141
- 'collection ' => $ operation ['collection ' ],
142
- 'operationId ' => $ operationId ,
143
- ],
144
- ] + ($ operation ['defaults ' ] ?? []),
145
- $ operation ['requirements ' ] ?? [],
146
- $ operation ['options ' ] ?? [],
147
- $ operation ['host ' ] ?? '' ,
148
- $ operation ['schemes ' ] ?? [],
149
- ['GET ' ],
150
- $ operation ['condition ' ] ?? ''
151
- ));
128
+ $ routeCollection ->add ($ operation ['route_name ' ], new Route (
129
+ $ operation ['path ' ],
130
+ [
131
+ '_controller ' => $ controller ,
132
+ '_format ' => null ,
133
+ '_stateless ' => $ operation ['stateless ' ] ?? $ operationCollectionMetadata ->getAttribute ('stateless ' ),
134
+ '_api_resource_class ' => $ operation ['resource_class ' ],
135
+ '_api_identified_by ' => $ operation ['identified_by ' ],
136
+ '_api_has_composite_identifier ' => false ,
137
+ '_api_subresource_operation_name ' => $ operation ['route_name ' ],
138
+ '_api_subresource_context ' => [
139
+ 'property ' => $ operation ['property ' ],
140
+ 'identifiers ' => $ operation ['identifiers ' ],
141
+ 'collection ' => $ operation ['collection ' ],
142
+ 'operationId ' => $ operationId ,
143
+ ],
144
+ ] + ($ operation ['defaults ' ] ?? []),
145
+ $ operation ['requirements ' ] ?? [],
146
+ $ operation ['options ' ] ?? [],
147
+ $ operation ['host ' ] ?? '' ,
148
+ $ operation ['schemes ' ] ?? [],
149
+ ['GET ' ],
150
+ $ operation ['condition ' ] ?? ''
151
+ ));
152
+ }
152
153
}
153
154
}
154
155
@@ -204,9 +205,9 @@ private function loadExternalFiles(RouteCollection $routeCollection): void
204
205
*
205
206
* @throws RuntimeException
206
207
*/
207
- private function addRoute (RouteCollection $ routeCollection , string $ resourceClass , string $ operationName , array $ operation , ResourceMetadata $ resourceMetadata , string $ operationType ): void
208
+ private function addRoute (RouteCollection $ routeCollection , string $ resourceClass , string $ operationName , array $ operation , OperationCollectionMetadata $ operationCollectionMetadata , string $ operationType ): void
208
209
{
209
- $ resourceShortName = $ resourceMetadata ->getShortName ();
210
+ $ resourceShortName = $ operationCollectionMetadata ->getShortName ();
210
211
211
212
if (isset ($ operation ['route_name ' ])) {
212
213
if (!isset ($ operation ['method ' ])) {
@@ -228,10 +229,10 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
228
229
}
229
230
}
230
231
231
- $ operation ['identified_by ' ] = (array ) ($ operation ['identified_by ' ] ?? $ resourceMetadata ->getAttribute ('identified_by ' ));
232
- $ operation ['has_composite_identifier ' ] = \count ($ operation ['identified_by ' ]) > 1 ? $ resourceMetadata ->getAttribute ('composite_identifier ' , true ) : false ;
232
+ $ operation ['identified_by ' ] = (array ) ($ operation ['identified_by ' ] ?? $ operationCollectionMetadata ->getAttribute ('identified_by ' ));
233
+ $ operation ['has_composite_identifier ' ] = \count ($ operation ['identified_by ' ]) > 1 ? $ operationCollectionMetadata ->getAttribute ('composite_identifier ' , true ) : false ;
233
234
234
- $ path = trim (trim ($ resourceMetadata ->getAttribute ('route_prefix ' , '' )), '/ ' );
235
+ $ path = trim (trim ($ operationCollectionMetadata ->getAttribute ('route_prefix ' , '' )), '/ ' );
235
236
$ path .= $ this ->operationPathResolver ->resolveOperationPath ($ resourceShortName , $ operation , $ operationType , $ operationName );
236
237
237
238
$ route = new Route (
0 commit comments