-
-
Notifications
You must be signed in to change notification settings - Fork 932
Use a chain pattern for data providers #578
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,8 @@ | |
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm; | ||
|
||
use ApiPlatform\Core\Api\ItemDataProviderInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; | ||
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; | ||
use ApiPlatform\Core\Exception\InvalidArgumentException; | ||
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
|
@@ -33,37 +33,26 @@ class ItemDataProvider implements ItemDataProviderInterface | |
private $propertyNameCollectionFactory; | ||
private $propertyMetadataFactory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too |
||
private $itemExtensions; | ||
private $decorated; | ||
|
||
/** | ||
* @param ManagerRegistry $managerRegistry | ||
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory | ||
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory | ||
* @param QueryItemExtensionInterface[] $itemExtensions | ||
* @param ItemDataProviderInterface|null $decorated | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [], ItemDataProviderInterface $decorated = null) | ||
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = []) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; | ||
$this->propertyMetadataFactory = $propertyMetadataFactory; | ||
$this->itemExtensions = $itemExtensions; | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false) | ||
{ | ||
if ($this->decorated) { | ||
try { | ||
return $this->decorated->getItem($resourceClass, $id, $operationName, $fetchData); | ||
} catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) { | ||
// Ignore it | ||
} | ||
} | ||
|
||
$manager = $this->managerRegistry->getManagerForClass($resourceClass); | ||
if (null === $manager) { | ||
throw new ResourceClassNotSupportedException(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/DataProviderPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\Symfony\Bundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Registers data providers. | ||
* | ||
* @internal | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class DataProviderPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->registerDataProviders($container, 'collection'); | ||
$this->registerDataProviders($container, 'item'); | ||
} | ||
|
||
/** | ||
* The priority sorting algorithm has been backported from Symfony 3.2. | ||
* | ||
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php | ||
* | ||
* @param ContainerBuilder $container | ||
* @param string $type | ||
*/ | ||
private function registerDataProviders(ContainerBuilder $container, string $type) | ||
{ | ||
$services = $container->findTaggedServiceIds('api_platform.'.$type.'_data_provider'); | ||
|
||
$queue = new \SplPriorityQueue(); | ||
|
||
foreach ($services as $serviceId => $tags) { | ||
foreach ($tags as $attributes) { | ||
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0; | ||
$queue->insert(new Reference($serviceId), $priority); | ||
} | ||
} | ||
|
||
$container->getDefinition('api_platform.'.$type.'_data_provider')->addArgument(iterator_to_array($queue, false)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Bridge/Symfony/Bundle/Resources/config/data_provider.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="api_platform.item_data_provider" class="ApiPlatform\Core\DataProvider\ChainItemDataProvider" /> | ||
|
||
<service id="api_platform.collection_data_provider" class="ApiPlatform\Core\DataProvider\ChainCollectionDataProvider" /> | ||
|
||
<service id="api_platform.filters" class="ApiPlatform\Core\Api\FilterCollection" public="false" /> | ||
</services> | ||
|
||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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\DataProvider; | ||
|
||
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; | ||
|
||
/** | ||
* Tries each configured data provider and returns the result of the first able to handle the resource class. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class ChainCollectionDataProvider implements CollectionDataProviderInterface | ||
{ | ||
private $dataProviders; | ||
|
||
/** | ||
* @param CollectionDataProviderInterface[] $dataProviders | ||
*/ | ||
public function __construct(array $dataProviders) | ||
{ | ||
$this->dataProviders = $dataProviders; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCollection(string $resourceClass, string $operationName = null) | ||
{ | ||
foreach ($this->dataProviders as $dataProvider) { | ||
try { | ||
return $dataProvider->getCollection($resourceClass, $operationName); | ||
} catch (ResourceClassNotSupportedException $e) { | ||
continue; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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\DataProvider; | ||
|
||
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; | ||
|
||
/** | ||
* Tries each configured data provider and returns the result of the first able to handle the resource class. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class ChainItemDataProvider implements ItemDataProviderInterface | ||
{ | ||
private $dataProviders; | ||
|
||
/** | ||
* @param ItemDataProviderInterface[] $dataProviders | ||
*/ | ||
public function __construct(array $dataProviders) | ||
{ | ||
$this->dataProviders = $dataProviders; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false) | ||
{ | ||
foreach ($this->dataProviders as $dataProviders) { | ||
try { | ||
return $dataProviders->getItem($resourceClass, $id, $operationName, $fetchData); | ||
} catch (ResourceClassNotSupportedException $e) { | ||
continue; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dunglas this one could be final as well