2121use ApiPlatform \Core \DataProvider \ContextAwareCollectionDataProviderInterface ;
2222use ApiPlatform \Core \DataProvider \Pagination ;
2323use ApiPlatform \Core \DataProvider \RestrictedDataProviderInterface ;
24+ use ApiPlatform \Core \Exception \ResourceClassNotFoundException ;
25+ use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
2426use Elasticsearch \Client ;
2527use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
2628
@@ -38,18 +40,20 @@ final class CollectionDataProvider implements ContextAwareCollectionDataProvider
3840 private $ identifierExtractor ;
3941 private $ denormalizer ;
4042 private $ pagination ;
43+ private $ resourceMetadataFactory ;
4144 private $ collectionExtensions ;
4245
4346 /**
4447 * @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
4548 */
46- public function __construct (Client $ client , DocumentMetadataFactoryInterface $ documentMetadataFactory , IdentifierExtractorInterface $ identifierExtractor , DenormalizerInterface $ denormalizer , Pagination $ pagination , iterable $ collectionExtensions = [])
49+ public function __construct (Client $ client , DocumentMetadataFactoryInterface $ documentMetadataFactory , IdentifierExtractorInterface $ identifierExtractor , DenormalizerInterface $ denormalizer , Pagination $ pagination , ResourceMetadataFactoryInterface $ resourceMetadataFactory , iterable $ collectionExtensions = [])
4750 {
4851 $ this ->client = $ client ;
4952 $ this ->documentMetadataFactory = $ documentMetadataFactory ;
5053 $ this ->identifierExtractor = $ identifierExtractor ;
5154 $ this ->denormalizer = $ denormalizer ;
5255 $ this ->pagination = $ pagination ;
56+ $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
5357 $ this ->collectionExtensions = $ collectionExtensions ;
5458 }
5559
@@ -58,6 +62,15 @@ public function __construct(Client $client, DocumentMetadataFactoryInterface $do
5862 */
5963 public function supports (string $ resourceClass , ?string $ operationName = null , array $ context = []): bool
6064 {
65+ try {
66+ $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
67+ if (false === $ resourceMetadata ->getCollectionOperationAttribute ($ operationName , 'elasticsearch ' , true , true )) {
68+ return false ;
69+ }
70+ } catch (ResourceClassNotFoundException $ e ) {
71+ return false ;
72+ }
73+
6174 try {
6275 $ this ->documentMetadataFactory ->create ($ resourceClass );
6376 } catch (IndexNotFoundException $ e ) {
0 commit comments