@@ -42,7 +42,6 @@ final class ApiLoader extends Loader
42
42
*/
43
43
const ROUTE_NAME_PREFIX = 'api_ ' ;
44
44
const DEFAULT_ACTION_PATTERN = 'api_platform.action. ' ;
45
- const SUBRESOURCE_SUFFIX = '_get_subresource ' ;
46
45
47
46
private $ fileLoader ;
48
47
private $ propertyNameCollectionFactory ;
@@ -54,7 +53,7 @@ final class ApiLoader extends Loader
54
53
private $ formats ;
55
54
private $ resourceClassDirectories ;
56
55
57
- public function __construct (KernelInterface $ kernel , ResourceNameCollectionFactoryInterface $ resourceNameCollectionFactory , ResourceMetadataFactoryInterface $ resourceMetadataFactory , OperationPathResolverInterface $ operationPathResolver , ContainerInterface $ container , array $ formats , array $ resourceClassDirectories = [], PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory = null , PropertyMetadataFactoryInterface $ propertyMetadataFactory = null )
56
+ public function __construct (KernelInterface $ kernel , ResourceNameCollectionFactoryInterface $ resourceNameCollectionFactory , ResourceMetadataFactoryInterface $ resourceMetadataFactory , OperationPathResolverInterface $ operationPathResolver , ContainerInterface $ container , array $ formats , array $ resourceClassDirectories = [], SubresourceOperationFactory $ subresourceOperationFactory = null )
58
57
{
59
58
$ this ->fileLoader = new XmlFileLoader (new FileLocator ($ kernel ->locateResource ('@ApiPlatformBundle/Resources/config/routing ' )));
60
59
$ this ->resourceNameCollectionFactory = $ resourceNameCollectionFactory ;
@@ -63,8 +62,7 @@ public function __construct(KernelInterface $kernel, ResourceNameCollectionFacto
63
62
$ this ->container = $ container ;
64
63
$ this ->formats = $ formats ;
65
64
$ this ->resourceClassDirectories = $ resourceClassDirectories ;
66
- $ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
67
- $ this ->propertyMetadataFactory = $ propertyMetadataFactory ;
65
+ $ this ->subresourceOperationFactory = $ subresourceOperationFactory ;
68
66
}
69
67
70
68
/**
@@ -99,90 +97,34 @@ public function load($data, $type = null): RouteCollection
99
97
}
100
98
}
101
99
102
- $ this ->computeSubresourceOperations ($ routeCollection , $ resourceClass );
103
- }
104
-
105
- return $ routeCollection ;
106
- }
107
-
108
- /**
109
- * Handles subresource operations recursively and declare their corresponding routes.
110
- *
111
- * @param RouteCollection $routeCollection
112
- * @param string $resourceClass
113
- * @param string $rootResourceClass null on the first iteration, it then keeps track of the origin resource class
114
- * @param array $parentOperation the previous call operation
115
- */
116
- private function computeSubresourceOperations (RouteCollection $ routeCollection , string $ resourceClass , string $ rootResourceClass = null , array $ parentOperation = null , array $ visited = [])
117
- {
118
- if (null === $ this ->propertyNameCollectionFactory || null === $ this ->propertyMetadataFactory ) {
119
- return ;
120
- }
121
-
122
- if (null === $ rootResourceClass ) {
123
- $ rootResourceClass = $ resourceClass ;
124
- }
125
-
126
- foreach ($ this ->propertyNameCollectionFactory ->create ($ resourceClass ) as $ property ) {
127
- $ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ resourceClass , $ property );
128
-
129
- if (!$ propertyMetadata ->hasSubresource ()) {
130
- continue ;
131
- }
132
-
133
- $ subresource = $ propertyMetadata ->getSubresource ();
134
-
135
- $ operation = [
136
- 'property ' => $ property ,
137
- 'collection ' => $ subresource ->isCollection (),
138
- ];
139
-
140
- $ visiting = "$ rootResourceClass $ resourceClass $ property {$ subresource ->isCollection ()} {$ subresource ->getResourceClass ()}" ;
141
-
142
- if (in_array ($ visiting , $ visited , true )) {
100
+ if (null === $ this ->subresourceOperationFactory ) {
143
101
continue ;
144
102
}
145
103
146
- $ visited [] = $ visiting ;
147
-
148
- if (null === $ parentOperation ) {
149
- $ rootResourceMetadata = $ this ->resourceMetadataFactory ->create ($ rootResourceClass );
150
- $ rootShortname = $ rootResourceMetadata ->getShortName ();
151
-
152
- $ operation ['identifiers ' ] = [['id ' , $ rootResourceClass ]];
153
- $ operation ['route_name ' ] = RouteNameGenerator::generate ('get ' , $ rootShortname , OperationType::SUBRESOURCE , $ operation );
154
- $ operation ['path ' ] = $ this ->operationPathResolver ->resolveOperationPath ($ rootShortname , $ operation , OperationType::SUBRESOURCE , $ operation ['route_name ' ]);
155
- } else {
156
- $ operation ['identifiers ' ] = $ parentOperation ['identifiers ' ];
157
- $ operation ['identifiers ' ][] = [$ parentOperation ['property ' ], $ resourceClass ];
158
- $ operation ['route_name ' ] = str_replace ('get ' .RouteNameGenerator::SUBRESOURCE_SUFFIX , RouteNameGenerator::routeNameResolver ($ property , $ operation ['collection ' ]).'_get ' .RouteNameGenerator::SUBRESOURCE_SUFFIX , $ parentOperation ['route_name ' ]);
159
- $ operation ['path ' ] = $ this ->operationPathResolver ->resolveOperationPath ($ parentOperation ['path ' ], $ operation , OperationType::SUBRESOURCE , $ operation ['route_name ' ]);
160
- }
161
-
162
- $ route = new Route (
163
- $ operation ['path ' ],
164
- [
165
- '_controller ' => self ::DEFAULT_ACTION_PATTERN .'get_subresource ' ,
166
- '_format ' => null ,
167
- '_api_resource_class ' => $ subresource ->getResourceClass (),
168
- '_api_subresource_operation_name ' => $ operation ['route_name ' ],
169
- '_api_subresource_context ' => [
170
- 'property ' => $ operation ['property ' ],
171
- 'identifiers ' => $ operation ['identifiers ' ],
172
- 'collection ' => $ subresource ->isCollection (),
104
+ foreach ($ this ->subresourceOperationFactory ->create ($ resourceClass ) as $ operation ) {
105
+ $ routeCollection ->add ($ operation ['route_name ' ], new Route (
106
+ $ operation ['path ' ],
107
+ [
108
+ '_controller ' => self ::DEFAULT_ACTION_PATTERN .'get_subresource ' ,
109
+ '_format ' => null ,
110
+ '_api_resource_class ' => $ operation ['resource_class ' ],
111
+ '_api_subresource_operation_name ' => $ operation ['route_name ' ],
112
+ '_api_subresource_context ' => [
113
+ 'property ' => $ operation ['property ' ],
114
+ 'identifiers ' => $ operation ['identifiers ' ],
115
+ 'collection ' => $ operation ['collection ' ],
116
+ ],
173
117
],
174
- ],
175
- [],
176
- [],
177
- '' ,
178
- [],
179
- ['GET ' ]
180
- );
181
-
182
- $ routeCollection ->add ($ operation ['route_name ' ], $ route );
183
-
184
- $ this ->computeSubresourceOperations ($ routeCollection , $ subresource ->getResourceClass (), $ rootResourceClass , $ operation , $ visited );
118
+ [],
119
+ [],
120
+ '' ,
121
+ [],
122
+ ['GET ' ]
123
+ ));
124
+ }
185
125
}
126
+
127
+ return $ routeCollection ;
186
128
}
187
129
188
130
/**
0 commit comments