Skip to content

Commit

Permalink
Add QueryResultItemExtensionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
abluchet committed Sep 7, 2016
1 parent 5c706e2 commit a39b417
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Bridge/Doctrine/Orm/CollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getCollection(string $resourceClass, string $operationName = nul
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);

if ($extension instanceof QueryResultExtensionInterface) {
if ($extension instanceof QueryResultCollectionExtensionInterface) {
if ($extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class PaginationExtension implements QueryResultExtensionInterface
class PaginationExtension implements QueryResultCollectionExtensionInterface
{
private $managerRegistry;
private $requestStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.
*/

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension;

use Doctrine\ORM\QueryBuilder;

/**
* Interface of Doctrine ORM query extensions that supports result production
* for specific cases such as pagination.
*
* @author Antoine BLUCHET <soyuka@gmail.com>
*/
interface QueryResultCollectionExtensionInterface extends QueryResultExtensionInterface, QueryCollectionExtensionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

/**
* Interface of Doctrine ORM query extensions that supports result production
* for specific cases such as pagination.
* for specific cases such as pagination or Query alteration.
*
* @author Samuel ROZE <samuel.roze@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface QueryResultExtensionInterface extends QueryCollectionExtensionInterface
interface QueryResultExtensionInterface
{
/**
* @param string $resourceClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.
*/

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension;

use Doctrine\ORM\QueryBuilder;

/**
* Interface of Doctrine ORM query extensions that supports result production
* for specific cases such as Query alteration.
*
* @author Antoine BLUCHET <soyuka@gmail.com>
*/
interface QueryResultItemExtensionInterface extends QueryResultExtensionInterface, QueryItemExtensionInterface
{
}
7 changes: 7 additions & 0 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -74,6 +75,12 @@ public function getItem(string $resourceClass, $id, string $operationName = null

foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName);

if ($extension instanceof QueryResultItemExtensionInterface) {
if ($extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
}
}

return $queryBuilder->getQuery()->getOneOrNullResult();
Expand Down
6 changes: 3 additions & 3 deletions tests/Bridge/Doctrine/Orm/CollectionDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\CollectionDataProvider;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Doctrine\Common\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testQueryResultExtension()
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();

$exstensionProphecy = $this->prophesize(QueryResultExtensionInterface::class);
$exstensionProphecy = $this->prophesize(QueryResultCollectionExtensionInterface::class);
$exstensionProphecy->applyToCollection($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, 'foo')->shouldBeCalled();
$exstensionProphecy->supportsResult(Dummy::class, 'foo')->willReturn(true)->shouldBeCalled();
$exstensionProphecy->getResult($queryBuilder)->willReturn([])->shouldBeCalled();
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testThrowResourceClassNotSupportedException()
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn(null)->shouldBeCalled();

$extenstensionProphecy = $this->prophesize(QueryResultExtensionInterface::class);
$extenstensionProphecy = $this->prophesize(QueryResultCollectionExtensionInterface::class);

$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extenstensionProphecy->reveal()]);
$dataProvider->getCollection(Dummy::class, 'foo');
Expand Down

0 comments on commit a39b417

Please sign in to comment.