vendor/api-platform/core/src/JsonLd/ContextBuilder.php line 157

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\JsonLd;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Api\UrlGeneratorInterface;
  14. use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface as LegacyPropertyMetadataFactoryInterface;
  15. use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface as LegacyPropertyNameCollectionFactoryInterface;
  16. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  17. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  18. use ApiPlatform\Metadata\ApiProperty;
  19. use ApiPlatform\Metadata\HttpOperation;
  20. use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  21. use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
  22. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  23. use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  24. use ApiPlatform\Util\ClassInfoTrait;
  25. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  26. /**
  27. * {@inheritdoc}
  28. * TODO: 3.0 simplify or remove the class.
  29. *
  30. * @author Kévin Dunglas <dunglas@gmail.com>
  31. */
  32. final class ContextBuilder implements AnonymousContextBuilderInterface
  33. {
  34. use ClassInfoTrait;
  35. public const FORMAT = 'jsonld';
  36. private $resourceNameCollectionFactory;
  37. /**
  38. * @param ResourceMetadataFactoryInterface|ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory
  39. */
  40. private $resourceMetadataFactory;
  41. /**
  42. * @var LegacyPropertyNameCollectionFactoryInterface|PropertyNameCollectionFactoryInterface
  43. */
  44. private $propertyNameCollectionFactory;
  45. /**
  46. * @var LegacyPropertyMetadataFactoryInterface|PropertyMetadataFactoryInterface
  47. */
  48. private $propertyMetadataFactory;
  49. private $urlGenerator;
  50. /**
  51. * @var NameConverterInterface|null
  52. */
  53. private $nameConverter;
  54. private $iriConverter;
  55. public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, $resourceMetadataFactory, $propertyNameCollectionFactory, $propertyMetadataFactory, UrlGeneratorInterface $urlGenerator, NameConverterInterface $nameConverter = null, IriConverterInterface $iriConverter = null)
  56. {
  57. $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
  58. $this->resourceMetadataFactory = $resourceMetadataFactory;
  59. $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
  60. $this->propertyMetadataFactory = $propertyMetadataFactory;
  61. $this->urlGenerator = $urlGenerator;
  62. $this->nameConverter = $nameConverter;
  63. $this->iriConverter = $iriConverter;
  64. if ($resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  65. trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL): array
  72. {
  73. return [
  74. '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
  75. 'hydra' => self::HYDRA_NS,
  76. ];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH): array
  82. {
  83. $context = $this->getBaseContext($referenceType);
  84. foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
  85. // TODO: remove in 3.0
  86. if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  87. $shortName = $this->resourceMetadataFactory->create($resourceClass)->getShortName();
  88. } else {
  89. $shortName = $this->resourceMetadataFactory->create($resourceClass)[0]->getShortName();
  90. }
  91. $resourceName = lcfirst($shortName);
  92. $context[$resourceName] = [
  93. '@id' => 'Entrypoint/'.$resourceName,
  94. '@type' => '@id',
  95. ];
  96. }
  97. return $context;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
  103. {
  104. // TODO: Remove in 3.0
  105. if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  106. $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
  107. if (null === $shortName = $resourceMetadata->getShortName()) {
  108. return [];
  109. }
  110. if ($resourceMetadata->getAttribute('normalization_context')['iri_only'] ?? false) {
  111. $context = $this->getBaseContext($referenceType);
  112. $context['hydra:member']['@type'] = '@id';
  113. return $context;
  114. }
  115. return $this->getResourceContextWithShortname($resourceClass, $referenceType, $shortName);
  116. }
  117. $operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation();
  118. if (null === $shortName = $operation->getShortName()) {
  119. return [];
  120. }
  121. if ($operation->getNormalizationContext()['iri_only'] ?? false) {
  122. $context = $this->getBaseContext($referenceType);
  123. $context['hydra:member']['@type'] = '@id';
  124. return $context;
  125. }
  126. return $this->getResourceContextWithShortname($resourceClass, $referenceType, $shortName, $operation);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getResourceContextUri(string $resourceClass, int $referenceType = null): string
  132. {
  133. // TODO: remove in 3.0
  134. if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  135. $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
  136. if (null === $referenceType) {
  137. $referenceType = $resourceMetadata->getAttribute('url_generation_strategy');
  138. }
  139. return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType);
  140. }
  141. $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)[0];
  142. if (null === $referenceType) {
  143. $referenceType = $resourceMetadata->getUrlGenerationStrategy();
  144. }
  145. return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getAnonymousResourceContext($object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array
  151. {
  152. $outputClass = $this->getObjectClass($object);
  153. $shortName = (new \ReflectionClass($outputClass))->getShortName();
  154. $jsonLdContext = [
  155. '@context' => $this->getResourceContextWithShortname(
  156. $outputClass,
  157. $referenceType,
  158. $shortName
  159. ),
  160. '@type' => $shortName,
  161. ];
  162. if (!isset($context['iri']) || false !== $context['iri']) {
  163. // Not using an IriConverter here is deprecated in 2.7, avoid spl_object_hash as it may collide
  164. if (isset($this->iriConverter)) {
  165. $jsonLdContext['@id'] = $context['iri'] ?? $this->iriConverter->getIriFromResource($object);
  166. } else {
  167. $jsonLdContext['@id'] = $context['iri'] ?? '/.well-known/genid/'.bin2hex(random_bytes(10));
  168. }
  169. }
  170. if ($this->iriConverter && isset($context['gen_id']) && true === $context['gen_id']) {
  171. $jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
  172. }
  173. if (false === ($context['iri'] ?? null)) {
  174. trigger_deprecation('api-platform/core', '2.7', 'An anonymous resource will use a Skolem IRI in API Platform 3.0. Use #[ApiProperty(genId: false)] to keep this behavior in 3.0.');
  175. }
  176. if ($context['has_context'] ?? false) {
  177. unset($jsonLdContext['@context']);
  178. }
  179. // here the object can be different from the resource given by the $context['api_resource'] value
  180. if (isset($context['api_resource'])) {
  181. if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  182. $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))->getShortName();
  183. } else {
  184. $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
  185. }
  186. }
  187. return $jsonLdContext;
  188. }
  189. private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
  190. {
  191. $context = $this->getBaseContext($referenceType);
  192. if ($this->propertyMetadataFactory instanceof LegacyPropertyMetadataFactoryInterface) {
  193. $propertyContext = [];
  194. } else {
  195. $propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
  196. }
  197. foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
  198. /** @var PropertyMetadata|ApiProperty */
  199. $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $propertyContext);
  200. if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
  201. continue;
  202. }
  203. $convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT) : $propertyName;
  204. if ($propertyMetadata instanceof PropertyMetadata) {
  205. $jsonldContext = ($propertyMetadata->getAttributes() ?? [])['jsonld_context'] ?? [];
  206. $id = $propertyMetadata->getIri();
  207. } else {
  208. $jsonldContext = $propertyMetadata->getJsonldContext() ?? [];
  209. if ($id = $propertyMetadata->getIris()) {
  210. $id = 1 === \count($id) ? $id[0] : $id;
  211. }
  212. }
  213. if (!$id) {
  214. $id = sprintf('%s/%s', $shortName, $convertedName);
  215. }
  216. if (false === $propertyMetadata->isReadableLink()) {
  217. $jsonldContext += [
  218. '@id' => $id,
  219. '@type' => '@id',
  220. ];
  221. }
  222. if (empty($jsonldContext)) {
  223. $context[$convertedName] = $id;
  224. } else {
  225. $context[$convertedName] = $jsonldContext + [
  226. '@id' => $id,
  227. ];
  228. }
  229. }
  230. return $context;
  231. }
  232. }
  233. class_alias(ContextBuilder::class, \ApiPlatform\Core\JsonLd\ContextBuilder::class);