2424use ApiPlatform \Core \Identifier \IdentifierConverterInterface ;
2525use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
2626use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
27+ use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
2728use Doctrine \Common \Persistence \ManagerRegistry ;
2829use Doctrine \ODM \MongoDB \Aggregation \Builder ;
2930use Doctrine \ODM \MongoDB \DocumentManager ;
@@ -43,16 +44,18 @@ final class SubresourceDataProvider implements SubresourceDataProviderInterface
4344 use IdentifierManagerTrait;
4445
4546 private $ managerRegistry ;
47+ private $ resourceMetadataFactory ;
4648 private $ collectionExtensions ;
4749 private $ itemExtensions ;
4850
4951 /**
5052 * @param AggregationCollectionExtensionInterface[] $collectionExtensions
5153 * @param AggregationItemExtensionInterface[] $itemExtensions
5254 */
53- public function __construct (ManagerRegistry $ managerRegistry , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , iterable $ collectionExtensions = [], iterable $ itemExtensions = [])
55+ public function __construct (ManagerRegistry $ managerRegistry , ResourceMetadataFactoryInterface $ resourceMetadataFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , iterable $ collectionExtensions = [], iterable $ itemExtensions = [])
5456 {
5557 $ this ->managerRegistry = $ managerRegistry ;
58+ $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
5659 $ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
5760 $ this ->propertyMetadataFactory = $ propertyMetadataFactory ;
5861 $ this ->collectionExtensions = $ collectionExtensions ;
@@ -80,7 +83,11 @@ public function getSubresource(string $resourceClass, array $identifiers, array
8083 throw new ResourceClassNotSupportedException ('The given resource class is not a subresource. ' );
8184 }
8285
83- $ aggregationBuilder = $ this ->buildAggregation ($ identifiers , $ context , $ repository ->createAggregationBuilder (), \count ($ context ['identifiers ' ]));
86+ $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
87+ $ attribute = $ resourceMetadata ->getSubresourceOperationAttribute ($ operationName , 'doctrine_mongodb ' , [], true );
88+ $ executeOptions = $ attribute ['execute_options ' ] ?? [];
89+
90+ $ aggregationBuilder = $ this ->buildAggregation ($ identifiers , $ context , $ executeOptions , $ repository ->createAggregationBuilder (), \count ($ context ['identifiers ' ]));
8491
8592 if (true === $ context ['collection ' ]) {
8693 foreach ($ this ->collectionExtensions as $ extension ) {
@@ -98,15 +105,15 @@ public function getSubresource(string $resourceClass, array $identifiers, array
98105 }
99106 }
100107
101- $ iterator = $ aggregationBuilder ->hydrate ($ resourceClass )->execute ();
108+ $ iterator = $ aggregationBuilder ->hydrate ($ resourceClass )->execute ($ executeOptions );
102109
103110 return $ context ['collection ' ] ? $ iterator ->toArray () : ($ iterator ->current () ?: null );
104111 }
105112
106113 /**
107114 * @throws RuntimeException
108115 */
109- private function buildAggregation (array $ identifiers , array $ context , Builder $ previousAggregationBuilder , int $ remainingIdentifiers , Builder $ topAggregationBuilder = null ): Builder
116+ private function buildAggregation (array $ identifiers , array $ context , array $ executeOptions , Builder $ previousAggregationBuilder , int $ remainingIdentifiers , Builder $ topAggregationBuilder = null ): Builder
110117 {
111118 if ($ remainingIdentifiers <= 0 ) {
112119 return $ previousAggregationBuilder ;
@@ -154,9 +161,9 @@ private function buildAggregation(array $identifiers, array $context, Builder $p
154161 }
155162
156163 // Recurse aggregations
157- $ aggregation = $ this ->buildAggregation ($ identifiers , $ context , $ aggregation , --$ remainingIdentifiers , $ topAggregationBuilder );
164+ $ aggregation = $ this ->buildAggregation ($ identifiers , $ context , $ executeOptions , $ aggregation , --$ remainingIdentifiers , $ topAggregationBuilder );
158165
159- $ results = $ aggregation ->execute ()->toArray ();
166+ $ results = $ aggregation ->execute ($ executeOptions )->toArray ();
160167 $ in = array_reduce ($ results , function ($ in , $ result ) use ($ previousAssociationProperty ) {
161168 return $ in + array_map (function ($ result ) {
162169 return $ result ['_id ' ];
0 commit comments