Skip to content

identifier normalizer code review #1820

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 1 commit into from
Apr 6, 2018
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ before_install:
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- npm install -g swagger-cli
- if [[ $lint = 1 ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.8.4/php-cs-fixer.phar; fi
- if [[ $lint = 1 ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.10.0/php-cs-fixer.phar; fi
- if [[ $lint = 1 ]]; then composer global require --dev 'phpstan/phpstan:^0.8'; fi
- export PATH="$PATH:$HOME/.composer/vendor/bin"

Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierNormalizer;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function getItem(string $resourceClass, $id, string $operationName = null
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);

if (!($context[ChainIdentifierNormalizer::HAS_IDENTIFIER_NORMALIZER] ?? false)) {
if (!($context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] ?? false)) {
$id = $this->normalizeIdentifiers($id, $manager, $resourceClass);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Doctrine/Orm/SubresourceDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierNormalizer;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -157,7 +157,7 @@ private function buildQuery(array $identifiers, array $context, QueryNameGenerat

if (isset($identifiers[$identifier])) {
// if it's an array it's already normalized, the IdentifierManagerTrait is deprecated
if ($context[ChainIdentifierNormalizer::HAS_IDENTIFIER_NORMALIZER] ?? false) {
if ($context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] ?? false) {
$normalizedIdentifiers = $identifiers[$identifier];
} else {
$normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
try {
return Uuid::fromString($data);
} catch (InvalidUuidStringException $e) {
throw new InvalidIdentifierException($e->getMessage());
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('security.xml');
}

if (!class_exists(Uuid::class)) {
$container->removeDefinition('api_platform.identifier.uuid_normalizer');
if (class_exists(Uuid::class)) {
$loader->load('ramsey_uuid.xml');
}

$useDoctrine = isset($bundles['DoctrineBundle']) && class_exists(Version::class);
Expand Down
18 changes: 6 additions & 12 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<argument type="service" id="api_platform.router" />
<argument type="service" id="api_platform.property_accessor" />
<argument type="service" id="api_platform.identifiers_extractor.cached" />
<argument type="service" id="api_platform.identifier.normalizer" />
<argument type="service" id="api_platform.identifier.denormalizer" />
</service>
<service id="ApiPlatform\Core\Api\IriConverterInterface" alias="api_platform.iri_converter" />

Expand Down Expand Up @@ -139,7 +139,7 @@
<argument type="service" id="api_platform.item_data_provider" />
<argument type="service" id="api_platform.subresource_data_provider" />
<argument type="service" id="api_platform.serializer.context_builder" />
<argument type="service" id="api_platform.identifier.normalizer" />
<argument type="service" id="api_platform.identifier.denormalizer" />

<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="4" />
</service>
Expand Down Expand Up @@ -229,20 +229,14 @@
<argument type="service" id="api_platform.resource_class_resolver" />
</service>

<service id="api_platform.identifier.normalizer" class="ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierNormalizer" public="false">
<service id="api_platform.identifier.denormalizer" class="ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer" public="false">
<argument type="service" id="api_platform.identifiers_extractor.cached" />
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
<argument type="tagged" tag="api_platform.identifier.normalizer" />
<argument type="tagged" tag="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.identifier.composite_identifier.normalizer" class="ApiPlatform\Core\Identifier\Normalizer\CompositeIdentifierNormalizer" public="false" />

<service id="api_platform.identifier.date_normalizer" class="ApiPlatform\Core\Identifier\Normalizer\DateTimeIdentifierNormalizer" public="false">
<tag name="api_platform.identifier.normalizer" />
</service>

<service id="api_platform.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
<tag name="api_platform.identifier.normalizer" />
<service id="api_platform.identifier.date_normalizer" class="ApiPlatform\Core\Identifier\Normalizer\DateTimeIdentifierDenormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>

<!-- Subresources -->
Expand Down
12 changes: 12 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>
</services>
</container>
19 changes: 11 additions & 8 deletions src/Bridge/Symfony/Routing/IriConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierNormalizer;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Util\ClassInfoTrait;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
* {@inheritdoc}
Expand All @@ -45,21 +44,25 @@ final class IriConverter implements IriConverterInterface
private $routeNameResolver;
private $router;
private $identifiersExtractor;
private $identifierNormalizer;
private $identifierDenormalizer;

public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, DenormalizerInterface $identifierNormalizer = null)
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, ChainIdentifierDenormalizer $identifierDenormalizer = null)
{
$this->itemDataProvider = $itemDataProvider;
$this->routeNameResolver = $routeNameResolver;
$this->router = $router;
$this->identifierNormalizer = $identifierNormalizer;
$this->identifierDenormalizer = $identifierDenormalizer;

if (null === $identifiersExtractor) {
@trigger_error('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3', E_USER_DEPRECATED);
$this->identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactory, $propertyMetadataFactory, $propertyAccessor ?? PropertyAccess::createPropertyAccessor());
} else {
$this->identifiersExtractor = $identifiersExtractor;
}

if (null === $identifierDenormalizer) {
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3.', ChainIdentifierDenormalizer::class), E_USER_DEPRECATED);
}
}

/**
Expand All @@ -79,9 +82,9 @@ public function getItemFromIri(string $iri, array $context = [])

$identifiers = $parameters['id'];

if ($this->identifierNormalizer) {
$identifiers = $this->identifierNormalizer->denormalize((string) $parameters['id'], $parameters['_api_resource_class']);
$context[ChainIdentifierNormalizer::HAS_IDENTIFIER_NORMALIZER] = true;
if ($this->identifierDenormalizer) {
$identifiers = $this->identifierDenormalizer->denormalize((string) $parameters['id'], $parameters['_api_resource_class']);
$context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true;
}

if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $identifiers, null, $context)) {
Expand Down
43 changes: 21 additions & 22 deletions src/EventListener/ReadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\Exception\InvalidIdentifierException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierNormalizer;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use ApiPlatform\Core\Util\RequestParser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
* Retrieves data from the applicable data provider and sets it as a request parameter called data.
Expand All @@ -38,20 +37,20 @@ final class ReadListener
private $itemDataProvider;
private $subresourceDataProvider;
private $serializerContextBuilder;
private $identifierNormalizer;
private $identifierDenormalizer;

public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider = null, SerializerContextBuilderInterface $serializerContextBuilder = null, DenormalizerInterface $identifierNormalizer = null)
public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider = null, SerializerContextBuilderInterface $serializerContextBuilder = null, ChainIdentifierDenormalizer $identifierDenormalizer = null)
{
$this->collectionDataProvider = $collectionDataProvider;
$this->itemDataProvider = $itemDataProvider;
$this->subresourceDataProvider = $subresourceDataProvider;
$this->serializerContextBuilder = $serializerContextBuilder;

if (null === $identifierNormalizer) {
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3.', ChainIdentifierNormalizer::class), E_USER_DEPRECATED);
if (null === $identifierDenormalizer) {
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3.', ChainIdentifierDenormalizer::class), E_USER_DEPRECATED);
}

$this->identifierNormalizer = $identifierNormalizer;
$this->identifierDenormalizer = $identifierDenormalizer;
}

/**
Expand Down Expand Up @@ -122,9 +121,9 @@ private function getItemData(Request $request, array $attributes, array $context
$context = [];

try {
if ($this->identifierNormalizer) {
$id = $this->identifierNormalizer->denormalize((string) $id, $attributes['resource_class']);
$context = [ChainIdentifierNormalizer::HAS_IDENTIFIER_NORMALIZER => true];
if ($this->identifierDenormalizer) {
$id = $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']);
$context = [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true];
}

$data = $this->itemDataProvider->getItem($attributes['resource_class'], $id, $attributes['item_operation_name'], $context);
Expand Down Expand Up @@ -155,24 +154,24 @@ private function getSubresourceData(Request $request, array $attributes, array $

$attributes['subresource_context'] += $context;
$identifiers = [];
if ($this->identifierNormalizer) {
$attributes['subresource_context'][ChainIdentifierNormalizer::HAS_IDENTIFIER_NORMALIZER] = true;
if ($this->identifierDenormalizer) {
$attributes['subresource_context'][ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true;
}

try {
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
if (false === $hasIdentifier) {
continue;
}
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
if (false === $hasIdentifier) {
continue;
}

$identifiers[$id] = $request->attributes->get($id);
$identifiers[$id] = $request->attributes->get($id);

if ($this->identifierNormalizer) {
$identifiers[$id] = $this->identifierNormalizer->denormalize((string) $identifiers[$id], $resourceClass);
if ($this->identifierDenormalizer) {
try {
$identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass);
} catch (InvalidIdentifierException $e) {
throw new NotFoundHttpException('Not Found');
}
}
} catch (InvalidIdentifierException $e) {
throw new NotFoundHttpException('Not Found');
}

$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']);
Expand Down
6 changes: 5 additions & 1 deletion src/Identifier/CompositeIdentifierParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
*/
final class CompositeIdentifierParser
{
private function __construct()
{
}

/*
* Normalize takes a string and gives back an array of identifiers.
*
* For example: foo=0;bar=2 returns ['foo' => 0, 'bar' => 2].
*/
public function parse(string $identifier): array
public static function parse(string $identifier): array
{
$matches = [];
$identifiers = [];
Expand Down
Loading