22
22
use Doctrine \ODM \MongoDB \DocumentManager ;
23
23
use Doctrine \ODM \MongoDB \Repository \DocumentRepository ;
24
24
use Doctrine \Persistence \ManagerRegistry ;
25
+ use Psr \Container \ContainerInterface ;
25
26
26
27
/**
27
28
* Item state provider using the Doctrine ODM.
@@ -36,41 +37,50 @@ final class ItemProvider implements ProviderInterface
36
37
/**
37
38
* @param AggregationItemExtensionInterface[] $itemExtensions
38
39
*/
39
- public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , private readonly ManagerRegistry $ managerRegistry , private readonly iterable $ itemExtensions = [])
40
+ public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , private readonly ManagerRegistry $ managerRegistry , private readonly iterable $ itemExtensions = [], ContainerInterface $ handleLinksLocator = null )
40
41
{
41
42
$ this ->resourceMetadataCollectionFactory = $ resourceMetadataCollectionFactory ;
43
+ $ this ->handleLinksLocator = $ handleLinksLocator ;
42
44
}
43
45
44
46
public function provide (Operation $ operation , array $ uriVariables = [], array $ context = []): ?object
45
47
{
46
- $ resourceClass = $ operation ->getClass ();
48
+ $ documentClass = $ operation ->getClass ();
49
+ if (($ options = $ operation ->getStateOptions ()) && $ options instanceof Options && $ options ->getDocumentClass ()) {
50
+ $ documentClass = $ options ->getDocumentClass ();
51
+ }
52
+
47
53
/** @var DocumentManager $manager */
48
- $ manager = $ this ->managerRegistry ->getManagerForClass ($ resourceClass );
54
+ $ manager = $ this ->managerRegistry ->getManagerForClass ($ documentClass );
49
55
50
56
$ fetchData = $ context ['fetch_data ' ] ?? true ;
51
57
if (!$ fetchData ) {
52
- return $ manager ->getReference ($ resourceClass , reset ($ uriVariables ));
58
+ return $ manager ->getReference ($ documentClass , reset ($ uriVariables ));
53
59
}
54
60
55
- $ repository = $ manager ->getRepository ($ resourceClass );
61
+ $ repository = $ manager ->getRepository ($ documentClass );
56
62
if (!$ repository instanceof DocumentRepository) {
57
- throw new RuntimeException (sprintf ('The repository for "%s" must be an instance of "%s". ' , $ resourceClass , DocumentRepository::class));
63
+ throw new RuntimeException (sprintf ('The repository for "%s" must be an instance of "%s". ' , $ documentClass , DocumentRepository::class));
58
64
}
59
65
60
66
$ aggregationBuilder = $ repository ->createAggregationBuilder ();
61
67
62
- $ this ->handleLinks ($ aggregationBuilder , $ uriVariables , $ context , $ resourceClass , $ operation );
68
+ if ($ handleLinks = $ this ->getLinksHandler ($ operation )) {
69
+ $ handleLinks ($ aggregationBuilder , $ uriVariables , ['documentClass ' => $ documentClass , 'operation ' => $ operation ] + $ context );
70
+ } else {
71
+ $ this ->handleLinks ($ aggregationBuilder , $ uriVariables , $ context , $ documentClass , $ operation );
72
+ }
63
73
64
74
foreach ($ this ->itemExtensions as $ extension ) {
65
- $ extension ->applyToItem ($ aggregationBuilder , $ resourceClass , $ uriVariables , $ operation , $ context );
75
+ $ extension ->applyToItem ($ aggregationBuilder , $ documentClass , $ uriVariables , $ operation , $ context );
66
76
67
- if ($ extension instanceof AggregationResultItemExtensionInterface && $ extension ->supportsResult ($ resourceClass , $ operation , $ context )) {
68
- return $ extension ->getResult ($ aggregationBuilder , $ resourceClass , $ operation , $ context );
77
+ if ($ extension instanceof AggregationResultItemExtensionInterface && $ extension ->supportsResult ($ documentClass , $ operation , $ context )) {
78
+ return $ extension ->getResult ($ aggregationBuilder , $ documentClass , $ operation , $ context );
69
79
}
70
80
}
71
81
72
82
$ executeOptions = $ operation ->getExtraProperties ()['doctrine_mongodb ' ]['execute_options ' ] ?? [];
73
83
74
- return $ aggregationBuilder ->hydrate ($ resourceClass )->execute ($ executeOptions )->current () ?: null ;
84
+ return $ aggregationBuilder ->hydrate ($ documentClass )->execute ($ executeOptions )->current () ?: null ;
75
85
}
76
86
}
0 commit comments