Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created September 6, 2018 10:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soyuka/a7a58732792872f000a09f79741042a4 to your computer and use it in GitHub Desktop.
Save soyuka/a7a58732792872f000a09f79741042a4 to your computer and use it in GitHub Desktop.
Override IriConverter and allow an ApiResource to not have any item route associated with it.
resources:
AppBundle\Api\DTO\SomeDTOWithNoItemOperations:
collectionOperations:
get:
method: 'GET'
itemOperations: []
attributes:
normalization_context: {'groups': []}
iri: 'referencing_with_conditions'
<?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 AppBundle\Api\Routing;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Util\ClassInfoTrait;
/**
* {@inheritdoc}
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class IriConverter implements IriConverterInterface
{
use ClassInfoTrait;
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $decorated)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->decorated = $decorated;
}
/**
* {@inheritdoc}
*/
public function getItemFromIri(string $iri, array $context = [])
{
return $this->decorated->getItemFromIri($iri, $context);
}
/**
* {@inheritdoc}
*/
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
{
$resourceClass = $this->getObjectClass($item);
$metadata = $this->resourceMetadataFactory->create($resourceClass);
if ($metadata->getAttribute('iri')) {
return $metadata->getAttribute('iri');
}
return $this->decorated->getIriFromItem($item, $referenceType);
}
/**
* {@inheritdoc}
*/
public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
{
return $this->decorated->getIriFromResourceClass($resourceClass, $referenceType);
}
/**
* {@inheritdoc}
*/
public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
{
return $this->decorated->getItemIriFromResourceClass($resourceClass, $identifiers, $referenceType);
}
/**
* {@inheritdoc}
*/
public function getSubresourceIriFromResourceClass(string $resourceClass, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
{
return $this->decorated->getSubresourceIriFromResourceClass($resourceClass, $context, $referenceType);
}
}
services:
AppBundle\Api\Routing\IriConverter:
decorates: 'api_platform.iri_converter'
arguments:
$resourceMetadataFactory: '@api_platform.metadata.resource.metadata_factory'
$decorated: '@AppBundle\Api\Routing\IriConverter.inner'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment