24
24
use ApiPlatform \Core \Identifier \IdentifierConverterInterface ;
25
25
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
26
26
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
27
+ use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
27
28
use Doctrine \Common \Persistence \ManagerRegistry ;
28
29
use Doctrine \ODM \MongoDB \Aggregation \Builder ;
29
30
use Doctrine \ODM \MongoDB \DocumentManager ;
@@ -43,16 +44,18 @@ final class SubresourceDataProvider implements SubresourceDataProviderInterface
43
44
use IdentifierManagerTrait;
44
45
45
46
private $ managerRegistry ;
47
+ private $ resourceMetadataFactory ;
46
48
private $ collectionExtensions ;
47
49
private $ itemExtensions ;
48
50
49
51
/**
50
52
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
51
53
* @param AggregationItemExtensionInterface[] $itemExtensions
52
54
*/
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 = [])
54
56
{
55
57
$ this ->managerRegistry = $ managerRegistry ;
58
+ $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
56
59
$ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
57
60
$ this ->propertyMetadataFactory = $ propertyMetadataFactory ;
58
61
$ this ->collectionExtensions = $ collectionExtensions ;
@@ -80,7 +83,11 @@ public function getSubresource(string $resourceClass, array $identifiers, array
80
83
throw new ResourceClassNotSupportedException ('The given resource class is not a subresource. ' );
81
84
}
82
85
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 ' ]));
84
91
85
92
if (true === $ context ['collection ' ]) {
86
93
foreach ($ this ->collectionExtensions as $ extension ) {
@@ -98,15 +105,15 @@ public function getSubresource(string $resourceClass, array $identifiers, array
98
105
}
99
106
}
100
107
101
- $ iterator = $ aggregationBuilder ->hydrate ($ resourceClass )->execute ();
108
+ $ iterator = $ aggregationBuilder ->hydrate ($ resourceClass )->execute ($ executeOptions );
102
109
103
110
return $ context ['collection ' ] ? $ iterator ->toArray () : ($ iterator ->current () ?: null );
104
111
}
105
112
106
113
/**
107
114
* @throws RuntimeException
108
115
*/
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
110
117
{
111
118
if ($ remainingIdentifiers <= 0 ) {
112
119
return $ previousAggregationBuilder ;
@@ -154,9 +161,9 @@ private function buildAggregation(array $identifiers, array $context, Builder $p
154
161
}
155
162
156
163
// Recurse aggregations
157
- $ aggregation = $ this ->buildAggregation ($ identifiers , $ context , $ aggregation , --$ remainingIdentifiers , $ topAggregationBuilder );
164
+ $ aggregation = $ this ->buildAggregation ($ identifiers , $ context , $ executeOptions , $ aggregation , --$ remainingIdentifiers , $ topAggregationBuilder );
158
165
159
- $ results = $ aggregation ->execute ()->toArray ();
166
+ $ results = $ aggregation ->execute ($ executeOptions )->toArray ();
160
167
$ in = array_reduce ($ results , function ($ in , $ result ) use ($ previousAssociationProperty ) {
161
168
return $ in + array_map (function ($ result ) {
162
169
return $ result ['_id ' ];
0 commit comments