Skip to content

Generator compat improvements #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Bridge/Doctrine/Orm/CollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use Doctrine\Common\Persistence\ManagerRegistry;

Expand All @@ -27,7 +27,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class CollectionDataProvider implements CollectionDataProviderInterface
class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $managerRegistry;
private $collectionExtensions;
Expand All @@ -42,6 +42,11 @@ public function __construct(ManagerRegistry $managerRegistry, array $collectionE
$this->collectionExtensions = $collectionExtensions;
}

public function supports(string $resourceClass, string $operationName = null): bool
{
return null !== $this->managerRegistry->getManagerForClass($resourceClass);
}

/**
* {@inheritdoc}
*
Expand All @@ -50,9 +55,6 @@ public function __construct(ManagerRegistry $managerRegistry, array $collectionE
public function getCollection(string $resourceClass, string $operationName = null)
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
if (null === $manager) {
throw new ResourceClassNotSupportedException();
}

$repository = $manager->getRepository($resourceClass);
if (!method_exists($repository, 'createQueryBuilder')) {
Expand Down
12 changes: 7 additions & 5 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\IdentifierManagerTrait;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand All @@ -32,7 +32,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class ItemDataProvider implements ItemDataProviderInterface
class ItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
use IdentifierManagerTrait;

Expand All @@ -53,6 +53,11 @@ public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollec
$this->itemExtensions = $itemExtensions;
}

public function supports(string $resourceClass, string $operationName = null): bool
{
return null !== $this->managerRegistry->getManagerForClass($resourceClass);
}

/**
* {@inheritdoc}
*
Expand All @@ -63,9 +68,6 @@ public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollec
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
if (null === $manager) {
throw new ResourceClassNotSupportedException();
}

$identifiers = $this->normalizeIdentifiers($id, $manager, $resourceClass);

Expand Down
6 changes: 6 additions & 0 deletions src/DataProvider/ChainCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ public function getCollection(string $resourceClass, string $operationName = nul
{
foreach ($this->dataProviders as $dataProvider) {
try {
if ($dataProvider instanceof RestrictedDataProviderInterface
&& !$dataProvider->supports($resourceClass, $operationName)) {
continue;
}

return $dataProvider->getCollection($resourceClass, $operationName);
} catch (ResourceClassNotSupportedException $e) {
@trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka I misunderstood your comment at first. I hope you all see how easier it would have been had each argument been on its own line: you could have commented directly under get_class($e). BTW, the sf standard is to have the message on one line, but is the sprintf part of the message? If yes, that's really a stupid rule who just made us loose precious time. It just makes life harder for everyone for no good reason.

continue;
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/DataProvider/ChainItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ public function __construct(array $dataProviders)
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
foreach ($this->dataProviders as $dataProviders) {
foreach ($this->dataProviders as $dataProvider) {
try {
return $dataProviders->getItem($resourceClass, $id, $operationName, $context);
if ($dataProvider instanceof RestrictedDataProviderInterface
&& !$dataProvider->supports($resourceClass, $operationName)) {
continue;
}

return $dataProvider->getItem($resourceClass, $id, $operationName, $context);
} catch (ResourceClassNotSupportedException $e) {
@trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
continue;
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/DataProvider/RestrictedDataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\DataProvider;

/**
* Restricts a data provider based on a condition.
*/
interface RestrictedDataProviderInterface
{
public function supports(string $resourceClass, string $operationName = null): bool;
}
7 changes: 2 additions & 5 deletions tests/Bridge/Doctrine/Orm/CollectionDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,14 @@ public function testCannotCreateQueryBuilder()
$this->assertEquals([], $dataProvider->getCollection(Dummy::class, 'foo'));
}

/**
* @expectedException \ApiPlatform\Core\Exception\ResourceClassNotSupportedException
*/
public function testThrowResourceClassNotSupportedException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this test anyway to ensure backward compatibility?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind I saw it in the chain!

public function testUnsupportedClass()
{
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn(null)->shouldBeCalled();

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

$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
$dataProvider->getCollection(Dummy::class, 'foo');
$this->assertFalse($dataProvider->supports(Dummy::class, 'foo'));
}
}
7 changes: 2 additions & 5 deletions tests/Bridge/Doctrine/Orm/ItemDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ public function testQueryResultExtension()
$this->assertEquals([], $dataProvider->getItem(Dummy::class, 1, 'foo'));
}

/**
* @expectedException \ApiPlatform\Core\Exception\ResourceClassNotSupportedException
*/
public function testThrowResourceClassNotSupportedException()
public function testUnsupportedClass()
{
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn(null)->shouldBeCalled();
Expand All @@ -193,7 +190,7 @@ public function testThrowResourceClassNotSupportedException()
]);

$dataProvider = new ItemDataProvider($managerRegistryProphecy->reveal(), $propertyNameCollectionFactory, $propertyMetadataFactory, [$extensionProphecy->reveal()]);
$dataProvider->getItem(Dummy::class, 'foo');
$this->assertFalse($dataProvider->supports(Dummy::class, 'foo'));
}

/**
Expand Down
59 changes: 57 additions & 2 deletions tests/DataProvider/ChainCollectionDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\DataProvider\ChainCollectionDataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
Expand All @@ -34,7 +35,57 @@ public function testGetCollection()
$dummy2->setName('Parks');

$firstDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$firstDataProvider->getCollection(Dummy::class, null)->willReturn([$dummy, $dummy2])->willThrow(ResourceClassNotSupportedException::class);
$firstDataProvider->willImplement(RestrictedDataProviderInterface::class);
$firstDataProvider->supports(Dummy::class, null)->willReturn(false);

$secondDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$secondDataProvider->willImplement(RestrictedDataProviderInterface::class);
$secondDataProvider->supports(Dummy::class, null)->willReturn(true);
$secondDataProvider->getCollection(Dummy::class, null)
->willReturn([$dummy, $dummy2]);

$thirdDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$thirdDataProvider->willImplement(RestrictedDataProviderInterface::class);
$thirdDataProvider->supports(Dummy::class, null)->willReturn(true);
$thirdDataProvider->getCollection(Dummy::class, null)->willReturn([$dummy]);

$chainItemDataProvider = new ChainCollectionDataProvider([
$firstDataProvider->reveal(),
$secondDataProvider->reveal(),
$thirdDataProvider->reveal(),
]);

$this->assertEquals(
[$dummy, $dummy2],
$chainItemDataProvider->getCollection(Dummy::class)
);
}

public function testGetCollectionNotSupported()
{
$firstDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$firstDataProvider->willImplement(RestrictedDataProviderInterface::class);
$firstDataProvider->supports('notfound', 'op')->willReturn(false);

$collection = (new ChainCollectionDataProvider([$firstDataProvider->reveal()]))->getCollection('notfound', 'op');

$this->assertTrue(is_array($collection) || $collection instanceof \Traversable);
$this->assertEmpty($collection);
}

/**
* @group legacy
* @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" in a data provider is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
*/
public function testLegacyGetCollection()
{
$dummy = new Dummy();
$dummy->setName('Rosa');
$dummy2 = new Dummy();
$dummy2->setName('Parks');

$firstDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$firstDataProvider->getCollection(Dummy::class, null)->willThrow(ResourceClassNotSupportedException::class);

$secondDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$secondDataProvider->getCollection(Dummy::class, null)->willReturn([$dummy, $dummy2]);
Expand All @@ -47,7 +98,11 @@ public function testGetCollection()
$this->assertEquals([$dummy, $dummy2], $chainItemDataProvider->getCollection(Dummy::class));
}

public function testGetCollectionExceptions()
/**
* @group legacy
* @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" in a data provider is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
*/
public function testLegacyGetCollectionExceptions()
{
$firstDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
$firstDataProvider->getCollection('notfound', 'op')->willThrow(ResourceClassNotSupportedException::class);
Expand Down
50 changes: 49 additions & 1 deletion tests/DataProvider/ChainItemDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\DataProvider\ChainItemDataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
Expand All @@ -31,6 +32,49 @@ public function testGetItem()
$dummy = new Dummy();
$dummy->setName('Lucie');

$firstDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$firstDataProvider->willImplement(RestrictedDataProviderInterface::class);
$firstDataProvider->supports(Dummy::class, null)->willReturn(false);

$secondDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$secondDataProvider->willImplement(RestrictedDataProviderInterface::class);
$secondDataProvider->supports(Dummy::class, null)->willReturn(true);
$secondDataProvider->getItem(Dummy::class, 1, null, [])->willReturn($dummy);

$thirdDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$thirdDataProvider->willImplement(RestrictedDataProviderInterface::class);
$thirdDataProvider->supports(Dummy::class, null)->willReturn(true);
$thirdDataProvider->getItem(Dummy::class, 1, null, [])->willReturn(new \stdClass());

$chainItemDataProvider = new ChainItemDataProvider([
$firstDataProvider->reveal(),
$secondDataProvider->reveal(),
$thirdDataProvider->reveal(),
]);

$this->assertEquals($dummy, $chainItemDataProvider->getItem(Dummy::class, 1));
}

public function testGetItemExceptions()
{
$firstDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$firstDataProvider->willImplement(RestrictedDataProviderInterface::class);
$firstDataProvider->supports('notfound', null)->willReturn(false);

$chainItemDataProvider = new ChainItemDataProvider([$firstDataProvider->reveal()]);

$this->assertEquals('', $chainItemDataProvider->getItem('notfound', 1));
}

/**
* @group legacy
* @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
*/
public function testLegacyGetItem()
{
$dummy = new Dummy();
$dummy->setName('Lucie');

$firstDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$firstDataProvider->getItem(Dummy::class, 1, null, [])->willThrow(ResourceClassNotSupportedException::class);

Expand All @@ -45,7 +89,11 @@ public function testGetItem()
$this->assertEquals($dummy, $chainItemDataProvider->getItem(Dummy::class, 1));
}

public function testGetItemExeptions()
/**
* @group legacy
* @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
*/
public function testLegacyGetItemExceptions()
{
$firstDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$firstDataProvider->getItem('notfound', 1, null, [])->willThrow(ResourceClassNotSupportedException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Tests\Fixtures\NotAResource;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ContainNonResource;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ContainNonResourceItemDataProvider implements ItemDataProviderInterface
class ContainNonResourceItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
public function supports(string $resourceClass, string $operationName = null): bool
{
return ContainNonResource::class === $resourceClass;
}

/**
* {@inheritdoc}
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
if (ContainNonResource::class !== $resourceClass) {
throw new ResourceClassNotSupportedException();
}

// Retrieve the blog post item from somewhere
$cnr = new ContainNonResource();
$cnr->id = $id;
Expand Down