@@ 24-76 (lines=53) @@ | ||
21 | /** |
|
22 | * Collection data provider for the Doctrine MongoDB ODM. |
|
23 | */ |
|
24 | class CollectionDataProvider implements CollectionDataProviderInterface |
|
25 | { |
|
26 | private $managerRegistry; |
|
27 | private $collectionExtensions; |
|
28 | private $decorated; |
|
29 | ||
30 | /** |
|
31 | * @param ManagerRegistry $managerRegistry |
|
32 | * @param QueryCollectionExtensionInterface[] $collectionExtensions |
|
33 | * @param CollectionDataProviderInterface|null $decorated |
|
34 | */ |
|
35 | public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [], CollectionDataProviderInterface $decorated = null) |
|
36 | { |
|
37 | $this->managerRegistry = $managerRegistry; |
|
38 | $this->collectionExtensions = $collectionExtensions; |
|
39 | $this->decorated = $decorated; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * {@inheritdoc} |
|
44 | */ |
|
45 | public function getCollection(string $resourceClass, string $operationName = null) |
|
46 | { |
|
47 | if ($this->decorated) { |
|
48 | try { |
|
49 | return $this->decorated->getCollection($resourceClass, $operationName); |
|
50 | } catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) { |
|
51 | // Ignore it |
|
52 | } |
|
53 | } |
|
54 | ||
55 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
|
56 | if (null === $manager) { |
|
57 | throw new ResourceClassNotSupportedException(); |
|
58 | } |
|
59 | ||
60 | /** @var DocumentRepository $repository */ |
|
61 | $repository = $manager->getRepository($resourceClass); |
|
62 | $queryBuilder = $repository->createQueryBuilder(); |
|
63 | ||
64 | foreach ($this->collectionExtensions as $extension) { |
|
65 | $extension->applyToCollection($queryBuilder, $resourceClass, $operationName); |
|
66 | ||
67 | if ($extension instanceof QueryResultExtensionInterface) { |
|
68 | if ($extension->supportsResult($resourceClass, $operationName)) { |
|
69 | return $extension->getResult($queryBuilder); |
|
70 | } |
|
71 | } |
|
72 | } |
|
73 | ||
74 | return $queryBuilder->getQuery()->execute()->toArray(); |
|
75 | } |
|
76 | } |
|
77 |
@@ 26-77 (lines=52) @@ | ||
23 | * @author Kévin Dunglas <[email protected]> |
|
24 | * @author Samuel ROZE <[email protected]> |
|
25 | */ |
|
26 | class CollectionDataProvider implements CollectionDataProviderInterface |
|
27 | { |
|
28 | private $managerRegistry; |
|
29 | private $collectionExtensions; |
|
30 | private $decorated; |
|
31 | ||
32 | /** |
|
33 | * @param ManagerRegistry $managerRegistry |
|
34 | * @param QueryCollectionExtensionInterface[] $collectionExtensions |
|
35 | * @param CollectionDataProviderInterface|null $decorated |
|
36 | */ |
|
37 | public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [], CollectionDataProviderInterface $decorated = null) |
|
38 | { |
|
39 | $this->managerRegistry = $managerRegistry; |
|
40 | $this->collectionExtensions = $collectionExtensions; |
|
41 | $this->decorated = $decorated; |
|
42 | } |
|
43 | ||
44 | /** |
|
45 | * {@inheritdoc} |
|
46 | */ |
|
47 | public function getCollection(string $resourceClass, string $operationName = null) |
|
48 | { |
|
49 | if ($this->decorated) { |
|
50 | try { |
|
51 | return $this->decorated->getCollection($resourceClass, $operationName); |
|
52 | } catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) { |
|
53 | // Ignore it |
|
54 | } |
|
55 | } |
|
56 | ||
57 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
|
58 | if (null === $manager) { |
|
59 | throw new ResourceClassNotSupportedException(); |
|
60 | } |
|
61 | ||
62 | $repository = $manager->getRepository($resourceClass); |
|
63 | $queryBuilder = $repository->createQueryBuilder('o'); |
|
64 | ||
65 | foreach ($this->collectionExtensions as $extension) { |
|
66 | $extension->applyToCollection($queryBuilder, $resourceClass, $operationName); |
|
67 | ||
68 | if ($extension instanceof QueryResultExtensionInterface) { |
|
69 | if ($extension->supportsResult($resourceClass, $operationName)) { |
|
70 | return $extension->getResult($queryBuilder); |
|
71 | } |
|
72 | } |
|
73 | } |
|
74 | ||
75 | return $queryBuilder->getQuery()->getResult(); |
|
76 | } |
|
77 | } |
|
78 |