2222use Doctrine \ODM \MongoDB \DocumentManager ;
2323use Doctrine \ODM \MongoDB \Repository \DocumentRepository ;
2424use Doctrine \Persistence \ManagerRegistry ;
25+ use Psr \Container \ContainerInterface ;
2526
2627/**
2728 * Item state provider using the Doctrine ODM.
@@ -36,41 +37,50 @@ final class ItemProvider implements ProviderInterface
3637 /**
3738 * @param AggregationItemExtensionInterface[] $itemExtensions
3839 */
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 )
4041 {
4142 $ this ->resourceMetadataCollectionFactory = $ resourceMetadataCollectionFactory ;
43+ $ this ->handleLinksLocator = $ handleLinksLocator ;
4244 }
4345
4446 public function provide (Operation $ operation , array $ uriVariables = [], array $ context = []): ?object
4547 {
46- $ resourceClass = $ operation ->getClass ();
48+ $ documentClass = $ operation ->getClass ();
49+ if (($ options = $ operation ->getStateOptions ()) && $ options instanceof Options && $ options ->getDocumentClass ()) {
50+ $ documentClass = $ options ->getDocumentClass ();
51+ }
52+
4753 /** @var DocumentManager $manager */
48- $ manager = $ this ->managerRegistry ->getManagerForClass ($ resourceClass );
54+ $ manager = $ this ->managerRegistry ->getManagerForClass ($ documentClass );
4955
5056 $ fetchData = $ context ['fetch_data ' ] ?? true ;
5157 if (!$ fetchData ) {
52- return $ manager ->getReference ($ resourceClass , reset ($ uriVariables ));
58+ return $ manager ->getReference ($ documentClass , reset ($ uriVariables ));
5359 }
5460
55- $ repository = $ manager ->getRepository ($ resourceClass );
61+ $ repository = $ manager ->getRepository ($ documentClass );
5662 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));
5864 }
5965
6066 $ aggregationBuilder = $ repository ->createAggregationBuilder ();
6167
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+ }
6373
6474 foreach ($ this ->itemExtensions as $ extension ) {
65- $ extension ->applyToItem ($ aggregationBuilder , $ resourceClass , $ uriVariables , $ operation , $ context );
75+ $ extension ->applyToItem ($ aggregationBuilder , $ documentClass , $ uriVariables , $ operation , $ context );
6676
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 );
6979 }
7080 }
7181
7282 $ executeOptions = $ operation ->getExtraProperties ()['doctrine_mongodb ' ]['execute_options ' ] ?? [];
7383
74- return $ aggregationBuilder ->hydrate ($ resourceClass )->execute ($ executeOptions )->current () ?: null ;
84+ return $ aggregationBuilder ->hydrate ($ documentClass )->execute ($ executeOptions )->current () ?: null ;
7585 }
7686}
0 commit comments