vendor/api-platform/core/src/Symfony/Routing/SkolemIriConverter.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Routing;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Api\UrlGeneratorInterface;
  14. use ApiPlatform\Exception\ItemNotFoundException;
  15. use ApiPlatform\Metadata\Operation;
  16. use Symfony\Component\Routing\RouterInterface;
  17. /**
  18. * {@inheritdoc}
  19. *
  20. * @author Antoine Bluchet <soyuka@gmail.com>
  21. */
  22. final class SkolemIriConverter implements IriConverterInterface
  23. {
  24. public static $skolemUriTemplate = '/.well-known/genid/{id}';
  25. private $objectHashMap;
  26. private $classHashMap = [];
  27. private $router;
  28. public function __construct(RouterInterface $router)
  29. {
  30. $this->router = $router;
  31. $this->objectHashMap = new \SplObjectStorage();
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null)
  37. {
  38. throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getIriFromResource($item, int $referenceType = UrlGeneratorInterface::ABS_PATH, Operation $operation = null, array $context = []): ?string
  44. {
  45. $referenceType = $operation ? ($operation->getUrlGenerationStrategy() ?? $referenceType) : $referenceType;
  46. if (($isObject = \is_object($item)) && $this->objectHashMap->contains($item)) {
  47. return $this->router->generate('api_genid', ['id' => $this->objectHashMap[$item]], $referenceType);
  48. }
  49. if (\is_string($item) && isset($this->classHashMap[$item])) {
  50. return $this->router->generate('api_genid', ['id' => $this->classHashMap[$item]], $referenceType);
  51. }
  52. $id = bin2hex(random_bytes(10));
  53. if ($isObject) {
  54. $this->objectHashMap[$item] = $id;
  55. } else {
  56. $this->classHashMap[$item] = $id;
  57. }
  58. return $this->router->generate('api_genid', ['id' => $id], $referenceType);
  59. }
  60. }