Skip to content

Commit

Permalink
Merge pull request #436 from teohhanhui/rename_metadata
Browse files Browse the repository at this point in the history
Rename metadata interfaces and classes
  • Loading branch information
dunglas committed Mar 7, 2016
2 parents 0057fd1 + 66b9c48 commit f2e6594
Show file tree
Hide file tree
Showing 46 changed files with 741 additions and 743 deletions.
10 changes: 5 additions & 5 deletions src/Api/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ApiPlatform\Core\Api;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Metadata\Resource\Factory\CollectionMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Util\ClassInfoTrait;

/**
Expand All @@ -25,11 +25,11 @@ final class ResourceClassResolver implements ResourceClassResolverInterface
{
use ClassInfoTrait;

private $collectionMetadataFactory;
private $resourceNameCollectionFactory;

public function __construct(CollectionMetadataFactoryInterface $collectionMetadataFactory)
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
{
$this->collectionMetadataFactory = $collectionMetadataFactory;
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
}

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ public function getResourceClass($value, string $resourceClass = null, bool $str
*/
public function isResourceClass(string $type) : bool
{
foreach ($this->collectionMetadataFactory->create() as $resourceClass) {
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
if ($type === $resourceClass) {
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Bridge/Doctrine/Orm/Extension/FilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use ApiPlatform\Core\Api\FilterCollection;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ItemMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Doctrine\ORM\QueryBuilder;

/**
Expand All @@ -24,12 +24,12 @@
*/
final class FilterExtension implements QueryCollectionExtensionInterface
{
private $itemMetadataFactory;
private $resourceMetadataFactory;
private $filters;

public function __construct(ItemMetadataFactoryInterface $itemMetadataFactory, FilterCollection $filters)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, FilterCollection $filters)
{
$this->itemMetadataFactory = $itemMetadataFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->filters = $filters;
}

Expand All @@ -38,8 +38,8 @@ public function __construct(ItemMetadataFactoryInterface $itemMetadataFactory, F
*/
public function applyToCollection(QueryBuilder $queryBuilder, string $resourceClass, string $operationName = null)
{
$itemMetadata = $this->itemMetadataFactory->create($resourceClass);
$resourceFilters = $itemMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);

if (empty($resourceFilters)) {
return;
Expand Down
28 changes: 14 additions & 14 deletions src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryChecker;
use ApiPlatform\Core\Metadata\Resource\Factory\ItemMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ItemMetadata;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
Expand All @@ -31,7 +31,7 @@ class PaginationExtension implements QueryResultExtensionInterface
{
private $managerRegistry;
private $requestStack;
private $itemMetadataFactory;
private $resourceMetadataFactory;
private $enabled;
private $clientEnabled;
private $clientItemsPerPage;
Expand All @@ -40,11 +40,11 @@ class PaginationExtension implements QueryResultExtensionInterface
private $enabledParameterName;
private $itemsPerPageParameterName;

public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, ItemMetadataFactoryInterface $itemMetadataFactory, bool $enabled = true, bool $clientEnabled = false, bool $clientItemsPerPage = false, int $itemsPerPage = 30, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', string $itemsPerPageParameterName = 'itemsPerPage')
public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, ResourceMetadataFactoryInterface $resourceMetadataFactory, bool $enabled = true, bool $clientEnabled = false, bool $clientItemsPerPage = false, int $itemsPerPage = 30, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', string $itemsPerPageParameterName = 'itemsPerPage')
{
$this->managerRegistry = $managerRegistry;
$this->requestStack = $requestStack;
$this->itemMetadataFactory = $itemMetadataFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->enabled = $enabled;
$this->clientEnabled = $clientEnabled;
$this->clientItemsPerPage = $clientItemsPerPage;
Expand All @@ -64,13 +64,13 @@ public function applyToCollection(QueryBuilder $queryBuilder, string $resourceCl
return;
}

$itemMetadata = $this->itemMetadataFactory->create($resourceClass);
if (!$this->isPaginationEnabled($request, $itemMetadata, $operationName)) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) {
return;
}

$itemsPerPage = $itemMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true);
if ($itemMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) {
$itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true);
if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) {
$itemsPerPage = (int) $request->query->get($this->itemsPerPageParameterName, $itemsPerPage);
}

Expand All @@ -90,9 +90,9 @@ public function supportsResult(string $resourceClass, string $operationName = nu
return false;
}

$itemMetadata = $this->itemMetadataFactory->create($resourceClass);
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

return $this->isPaginationEnabled($request, $itemMetadata, $operationName);
return $this->isPaginationEnabled($request, $resourceMetadata, $operationName);
}

/**
Expand All @@ -106,10 +106,10 @@ public function getResult(QueryBuilder $queryBuilder)
return new Paginator($doctrineOrmPaginator);
}

private function isPaginationEnabled(Request $request, ItemMetadata $itemMetadata, string $operationName = null) : bool
private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null) : bool
{
$enabled = $itemMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true);
$clientEnabled = $itemMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true);
$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true);
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true);

if ($clientEnabled) {
$enabled = filter_var($request->query->get($this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN);
Expand Down
30 changes: 15 additions & 15 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Metadata\Property\Factory\CollectionMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\ItemMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;

Expand All @@ -29,23 +29,23 @@
class ItemDataProvider implements ItemDataProviderInterface
{
private $managerRegistry;
private $collectionMetadataFactory;
private $itemMetadataFactory;
private $propertyNameCollectionFactory;
private $propertyMetadataFactory;
private $itemExtensions;
private $decorated;

/**
* @param ManagerRegistry $managerRegistry
* @param CollectionMetadataFactoryInterface $collectionMetadataFactory
* @param ItemMetadataFactoryInterface $itemMetadataFactory
* @param QueryItemExtensionInterface[] $itemExtensions
* @param ItemDataProviderInterface|null $decorated
* @param ManagerRegistry $managerRegistry
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory
* @param QueryItemExtensionInterface[] $itemExtensions
* @param ItemDataProviderInterface|null $decorated
*/
public function __construct(ManagerRegistry $managerRegistry, CollectionMetadataFactoryInterface $collectionMetadataFactory, ItemMetadataFactoryInterface $itemMetadataFactory, array $itemExtensions = [], ItemDataProviderInterface $decorated = null)
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [], ItemDataProviderInterface $decorated = null)
{
$this->managerRegistry = $managerRegistry;
$this->collectionMetadataFactory = $collectionMetadataFactory;
$this->itemMetadataFactory = $itemMetadataFactory;
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
$this->propertyMetadataFactory = $propertyMetadataFactory;
$this->itemExtensions = $itemExtensions;
$this->decorated = $decorated;
}
Expand All @@ -72,10 +72,10 @@ public function getItem(string $resourceClass, $id, string $operationName = null
$identifiers = [];
$i = 0;

foreach ($this->collectionMetadataFactory->create($resourceClass) as $propertyName) {
$itemMetadata = $this->itemMetadataFactory->create($resourceClass, $propertyName);
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);

$identifier = $itemMetadata->isIdentifier();
$identifier = $propertyMetadata->isIdentifier();
if (null === $identifier || false === $identifier) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property;

use ApiPlatform\Core\Metadata\Property\Factory\ItemMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\ItemMetadata;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* Use Doctrine metadata to populate the identifier property.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class ItemMetadataFactory implements ItemMetadataFactoryInterface
final class DoctrineOrmPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
private $decorated;
private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry, ItemMetadataFactoryInterface $decorated)
public function __construct(ManagerRegistry $managerRegistry, PropertyMetadataFactoryInterface $decorated)
{
$this->managerRegistry = $managerRegistry;
$this->decorated = $decorated;
Expand All @@ -34,39 +34,39 @@ public function __construct(ManagerRegistry $managerRegistry, ItemMetadataFactor
/**
* {@inheritdoc}
*/
public function create(string $resourceClass, string $property, array $options = []) : ItemMetadata
public function create(string $resourceClass, string $property, array $options = []) : PropertyMetadata
{
$itemMetadata = $this->decorated->create($resourceClass, $property, $options);
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);

if (null !== $itemMetadata->isIdentifier()) {
return $itemMetadata;
if (null !== $propertyMetadata->isIdentifier()) {
return $propertyMetadata;
}

$manager = $this->managerRegistry->getManagerForClass($resourceClass);
if (!$manager) {
return $itemMetadata;
return $propertyMetadata;
}

$doctrineClassMetadata = $manager->getClassMetadata($resourceClass);
if (!$doctrineClassMetadata) {
return $itemMetadata;
return $propertyMetadata;
}

$identifiers = $doctrineClassMetadata->getIdentifier();
foreach ($identifiers as $identifier) {
if ($identifier === $property) {
$itemMetadata = $itemMetadata->withIdentifier(true);
$itemMetadata = $itemMetadata->withReadable(false);
$itemMetadata = $itemMetadata->withWritable($doctrineClassMetadata->isIdentifierNatural());
$propertyMetadata = $propertyMetadata->withIdentifier(true);
$propertyMetadata = $propertyMetadata->withReadable(false);
$propertyMetadata = $propertyMetadata->withWritable($doctrineClassMetadata->isIdentifierNatural());

break;
}
}

if (null === $itemMetadata->isIdentifier()) {
$itemMetadata = $itemMetadata->withIdentifier(false);
if (null === $propertyMetadata->isIdentifier()) {
$propertyMetadata = $propertyMetadata->withIdentifier(false);
}

return $itemMetadata;
return $propertyMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<!-- Metadata loader -->

<service id="api_platform.doctrine.orm.metadata.property.factory.item" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\ItemMetadataFactory" decorates="api_platform.metadata.property.factory.item" public="false">
<service id="api_platform.doctrine.orm.metadata.property.factory.item" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\DoctrineOrmPropertyMetadataFactory" decorates="api_platform.metadata.property.factory.item" public="false">
<argument type="service" id="doctrine" />
<argument type="service" id="api_platform.doctrine.orm.metadata.property.factory.item.inner" />
</service>
Expand Down

0 comments on commit f2e6594

Please sign in to comment.