/
CollectionDataProvider.php
111 lines (95 loc) · 3.59 KB
/
CollectionDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Bridge\Elasticsearch\DataProvider;
use ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\FullBodySearchCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException;
use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\Pagination;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Elasticsearch\Client;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Collection data provider for Elasticsearch.
*
* @experimental
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
final class CollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $client;
private $documentMetadataFactory;
private $denormalizer;
private $pagination;
private $collectionExtensions;
/**
* @param FullBodySearchCollectionExtensionInterface[] $collectionExtensions
*/
public function __construct(Client $client, DocumentMetadataFactoryInterface $documentMetadataFactory, DenormalizerInterface $denormalizer, Pagination $pagination, iterable $collectionExtensions = [])
{
$this->client = $client;
$this->documentMetadataFactory = $documentMetadataFactory;
$this->denormalizer = $denormalizer;
$this->pagination = $pagination;
$this->collectionExtensions = $collectionExtensions;
}
/**
* {@inheritdoc}
*/
public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
{
try {
$this->documentMetadataFactory->create($resourceClass);
} catch (IndexNotFoundException $e) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function getCollection(string $resourceClass, ?string $operationName = null, array $context = [])
{
$documentMetadata = $this->documentMetadataFactory->create($resourceClass);
$body = [];
foreach ($this->collectionExtensions as $collectionExtension) {
$body = $collectionExtension->applyToCollection($body, $resourceClass, $operationName, $context);
}
if (!isset($body['query']) && !isset($body['aggs'])) {
$body = array_merge($body, [
'query' => [
'match_all' => new \stdClass(),
],
]);
}
$limit = $body['size'] ?? $this->pagination->getLimit($resourceClass, $operationName);
$offset = $body['offset'] ?? $this->pagination->getOffset($resourceClass, $operationName);
if (!isset($body['size'])) {
$body = array_merge($body, ['size' => $limit]);
}
if (!isset($body['from'])) {
$body = array_merge($body, ['from' => $offset]);
}
$documents = $this->client->search([
'index' => $documentMetadata->getIndex(),
'type' => $documentMetadata->getType(),
'body' => $body,
]);
return new Paginator(
$this->denormalizer,
$documents,
$resourceClass,
$limit,
$offset
);
}
}