Last active
June 20, 2019 08:24
-
-
Save mumy81/2c35562c9aa0c7ba2b6b67dcd752a5e0 to your computer and use it in GitHub Desktop.
API Platform Custom Data Providers
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
<?php | |
namespace App\DataProvider; | |
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; | |
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; | |
use Doctrine\Common\Persistence\ManagerRegistry; | |
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; | |
final class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface | |
{ | |
private $collectionExtensions; | |
private $managerRegistry; | |
private $excludedResources; | |
// It can initalized by customizing | |
public function __construct(ManagerRegistry $managerRegistry, /* iterable */ $collectionExtensions = []) | |
{ | |
$this->excludedResources = ['App/Entity/ModuleRevision', 'TemplateRevision']; | |
$this->collectionExtensions = $collectionExtensions; | |
$this->managerRegistry = $managerRegistry; | |
} | |
// This method checks either the situation is suitable | |
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool | |
{ | |
return !in_array($resourceClass, $this->excludedResources); | |
} | |
// The method coming from CollectionDataProviderInterface | |
// We can create repository, query builder, and querynamegernator , with query builder we can customize our data providing way | |
public function getCollection(string $resourceClass, string $operationName = null, array $context = []) | |
{ | |
$manager = $this->managerRegistry->getManagerForClass($resourceClass); | |
$repository = $manager->getRepository($resourceClass); | |
$queryBuilder = $repository->createQueryBuilder('e'); | |
$queryNameGenerator = new QueryNameGenerator(); | |
//We add where clasue it provides only non-deleted items will be fetched | |
$queryBuilder->andWhere('e.isDeleted = :val') | |
->setParameter('val', false); | |
// This section, apply all extensions to our query builder | |
foreach ($this->collectionExtensions as $extension) { | |
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); | |
if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { | |
return $extension->getResult($queryBuilder, $resourceClass, $operationName, $context); | |
} | |
} | |
return $queryBuilder->getQuery()->getResult(); | |
} | |
} |
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
the content will come here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment