Skip to content

Commit 24f53ca

Browse files
authored
Use a chain pattern for data providers (#578)
1 parent ab77784 commit 24f53ca

26 files changed

+200
-56
lines changed

src/Action/ActionUtilTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Action;
1313

14-
use ApiPlatform\Core\Api\ItemDataProviderInterface;
14+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1515
use ApiPlatform\Core\Exception\RuntimeException;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

src/Action/GetCollectionAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace ApiPlatform\Core\Action;
1313

14-
use ApiPlatform\Core\Api\CollectionDataProviderInterface;
15-
use ApiPlatform\Core\Api\PaginatorInterface;
14+
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
15+
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1616
use ApiPlatform\Core\Exception\RuntimeException;
1717
use Symfony\Component\HttpFoundation\Request;
1818

src/Action/GetItemAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Action;
1313

14-
use ApiPlatform\Core\Api\ItemDataProviderInterface;
14+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1515
use ApiPlatform\Core\Exception\RuntimeException;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

src/Action/PutItemAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Action;
1313

14-
use ApiPlatform\Core\Api\ItemDataProviderInterface;
14+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1515
use ApiPlatform\Core\Exception\RuntimeException;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

src/Api/ResourceClassResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Api;
1313

14+
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1415
use ApiPlatform\Core\Exception\InvalidArgumentException;
1516
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1617
use ApiPlatform\Core\Util\ClassInfoTrait;

src/Bridge/Doctrine/Orm/CollectionDataProvider.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
1313

14-
use ApiPlatform\Core\Api\CollectionDataProviderInterface;
1514
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
1615
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
16+
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
1717
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
1818
use Doctrine\Common\Persistence\ManagerRegistry;
1919

@@ -27,33 +27,22 @@ class CollectionDataProvider implements CollectionDataProviderInterface
2727
{
2828
private $managerRegistry;
2929
private $collectionExtensions;
30-
private $decorated;
3130

3231
/**
33-
* @param ManagerRegistry $managerRegistry
34-
* @param QueryCollectionExtensionInterface[] $collectionExtensions
35-
* @param CollectionDataProviderInterface|null $decorated
32+
* @param ManagerRegistry $managerRegistry
33+
* @param QueryCollectionExtensionInterface[] $collectionExtensions
3634
*/
37-
public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [], CollectionDataProviderInterface $decorated = null)
35+
public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [])
3836
{
3937
$this->managerRegistry = $managerRegistry;
4038
$this->collectionExtensions = $collectionExtensions;
41-
$this->decorated = $decorated;
4239
}
4340

4441
/**
4542
* {@inheritdoc}
4643
*/
4744
public function getCollection(string $resourceClass, string $operationName = null)
4845
{
49-
if ($this->decorated) {
50-
try {
51-
return $this->decorated->getCollection($resourceClass, $operationName);
52-
} catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) {
53-
// Ignore it
54-
}
55-
}
56-
5746
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
5847
if (null === $manager) {
5948
throw new ResourceClassNotSupportedException();

src/Bridge/Doctrine/Orm/ItemDataProvider.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
1313

14-
use ApiPlatform\Core\Api\ItemDataProviderInterface;
1514
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
15+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1616
use ApiPlatform\Core\Exception\InvalidArgumentException;
1717
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
1818
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -33,37 +33,26 @@ class ItemDataProvider implements ItemDataProviderInterface
3333
private $propertyNameCollectionFactory;
3434
private $propertyMetadataFactory;
3535
private $itemExtensions;
36-
private $decorated;
3736

3837
/**
3938
* @param ManagerRegistry $managerRegistry
4039
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
4140
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory
4241
* @param QueryItemExtensionInterface[] $itemExtensions
43-
* @param ItemDataProviderInterface|null $decorated
4442
*/
45-
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [], ItemDataProviderInterface $decorated = null)
43+
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [])
4644
{
4745
$this->managerRegistry = $managerRegistry;
4846
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
4947
$this->propertyMetadataFactory = $propertyMetadataFactory;
5048
$this->itemExtensions = $itemExtensions;
51-
$this->decorated = $decorated;
5249
}
5350

5451
/**
5552
* {@inheritdoc}
5653
*/
5754
public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
5855
{
59-
if ($this->decorated) {
60-
try {
61-
return $this->decorated->getItem($resourceClass, $id, $operationName, $fetchData);
62-
} catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) {
63-
// Ignore it
64-
}
65-
}
66-
6756
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
6857
if (null === $manager) {
6958
throw new ResourceClassNotSupportedException();

src/Bridge/Doctrine/Orm/Paginator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
1313

14-
use ApiPlatform\Core\Api\PaginatorInterface;
14+
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1515
use Doctrine\ORM\Query;
1616
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
1717

src/Bridge/Symfony/Bundle/ApiPlatformBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace ApiPlatform\Core\Bridge\Symfony\Bundle;
1313

14+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DataProviderPass;
1415
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DoctrineQueryExtensionPass;
1516
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\FilterPass;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -30,6 +31,7 @@ public function build(ContainerBuilder $container)
3031
{
3132
parent::build($container);
3233

34+
$container->addCompilerPass(new DataProviderPass());
3335
$container->addCompilerPass(new FilterPass());
3436
$container->addCompilerPass(new DoctrineQueryExtensionPass());
3537
}

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function load(array $configs, ContainerBuilder $container)
8181
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
8282
$loader->load('api.xml');
8383
$loader->load('metadata.xml');
84+
$loader->load('data_provider.xml');
8485

8586
$this->enableJsonLd($loader);
8687
$this->registerAnnotationLoaders($container);

0 commit comments

Comments
 (0)