Created
September 6, 2018 10:19
-
-
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.
This file contains 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
resources: | |
AppBundle\Api\DTO\SomeDTOWithNoItemOperations: | |
collectionOperations: | |
get: | |
method: 'GET' | |
itemOperations: [] | |
attributes: | |
normalization_context: {'groups': []} | |
iri: 'referencing_with_conditions' |
This file contains 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 | |
/* | |
* 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); | |
} | |
} |
This file contains 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
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