/**
  * Builds default context.
  *
  * @param ContextBuilderEvent $event
  */
 public function onContextBuilder(ContextBuilderEvent $event)
 {
     $resource = $event->getResource();
     if (null === $resource) {
         return;
     }
     $context = $event->getContext();
     $prefixedShortName = sprintf('#%s', $resource->getShortName());
     $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
     $identifierName = $classMetadata->getIdentifierName();
     foreach ($classMetadata->getAttributesMetadata() as $attributeName => $attributeMetadata) {
         if ($identifierName === $attributeName) {
             continue;
         }
         $convertedName = $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName;
         if (!($id = $attributeMetadata->getIri())) {
             $id = sprintf('%s/%s', $prefixedShortName, $convertedName);
         }
         if ($attributeMetadata->isNormalizationLink()) {
             $context[$convertedName] = ['@id' => $id, '@type' => '@id'];
         } else {
             $context[$convertedName] = $id;
         }
     }
     $event->setContext($context);
 }
 /**
  * {@inheritdoc}
  */
 public function getApiDocumentation()
 {
     $classes = [];
     $entrypointProperties = [];
     foreach ($this->resourceCollection as $resource) {
         $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
         $shortName = $resource->getShortName();
         $prefixedShortName = ($iri = $classMetadata->getIri()) ? $iri : '#' . $shortName;
         $collectionOperations = [];
         foreach ($resource->getCollectionOperations() as $collectionOperation) {
             $collectionOperations[] = $this->getHydraOperation($resource, $collectionOperation, $prefixedShortName, true);
         }
         if (!empty($collectionOperations)) {
             $entrypointProperties[] = ['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => sprintf('#Entrypoint/%s', lcfirst($shortName)), '@type' => 'hydra:Link', 'domain' => '#Entrypoint', 'rdfs:label' => sprintf('The collection of %s resources', $shortName), 'range' => 'hydra:PagedCollection', 'hydra:supportedOperation' => $collectionOperations], 'hydra:title' => sprintf('The collection of %s resources', $shortName), 'hydra:readable' => true, 'hydra:writable' => false];
         }
         $class = ['@id' => $prefixedShortName, '@type' => 'hydra:Class', 'rdfs:label' => $resource->getShortName(), 'hydra:title' => $resource->getShortName(), 'hydra:description' => $classMetadata->getDescription()];
         if ($description = $classMetadata->getDescription()) {
             $class['hydra:description'] = $description;
         }
         $properties = [];
         $identifierName = $classMetadata->getIdentifierName();
         foreach ($classMetadata->getAttributesMetadata() as $attributeName => $attributeMetadata) {
             if ($identifierName === $attributeName && !$attributeMetadata->isWritable()) {
                 continue;
             }
             if ($attributeMetadata->isNormalizationLink()) {
                 $type = 'Hydra:Link';
             } else {
                 $type = 'rdf:Property';
             }
             $property = ['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => ($iri = $attributeMetadata->getIri()) ? $iri : sprintf('#%s/%s', $shortName, $attributeName), '@type' => $type, 'rdfs:label' => $attributeName, 'domain' => $prefixedShortName], 'hydra:title' => $attributeName, 'hydra:required' => $attributeMetadata->isRequired(), 'hydra:readable' => $identifierName === $attributeName ? false : $attributeMetadata->isReadable(), 'hydra:writable' => $attributeMetadata->isWritable()];
             if ($range = $this->getRange($attributeMetadata)) {
                 $property['hydra:property']['range'] = $range;
             }
             if ($description = $attributeMetadata->getDescription()) {
                 $property['hydra:description'] = $description;
             }
             $properties[] = $property;
         }
         $class['hydra:supportedProperty'] = $properties;
         $operations = [];
         foreach ($resource->getItemOperations() as $itemOperation) {
             $operations[] = $this->getHydraOperation($resource, $itemOperation, $prefixedShortName, false);
         }
         $class['hydra:supportedOperation'] = $operations;
         $classes[] = $class;
     }
     // Entrypoint
     $classes[] = ['@id' => '#Entrypoint', '@type' => 'hydra:Class', 'hydra:title' => 'The API entrypoint', 'hydra:supportedProperty' => $entrypointProperties, 'hydra:supportedOperation' => ['@type' => 'hydra:Operation', 'hydra:method' => 'GET', 'rdfs:label' => 'The API entrypoint.', 'returns' => '#EntryPoint']];
     // Constraint violation
     $classes[] = ['@id' => '#ConstraintViolation', '@type' => 'hydra:Class', 'hydra:title' => 'A constraint violation', 'hydra:supportedProperty' => [['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => '#ConstraintViolation/propertyPath', '@type' => 'rdf:Property', 'rdfs:label' => 'propertyPath', 'domain' => '#ConstraintViolation', 'range' => 'xmls:string'], 'hydra:title' => 'propertyPath', 'hydra:description' => 'The property path of the violation', 'hydra:readable' => true, 'hydra:writable' => false], ['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => '#ConstraintViolation/message', '@type' => 'rdf:Property', 'rdfs:label' => 'message', 'domain' => '#ConstraintViolation', 'range' => 'xmls:string'], 'hydra:title' => 'message', 'hydra:description' => 'The message associated with the violation', 'hydra:readable' => true, 'hydra:writable' => false]]];
     // Constraint violation list
     $classes[] = ['@id' => '#ConstraintViolationList', '@type' => 'hydra:Class', 'subClassOf' => 'hydra:Error', 'hydra:title' => 'A constraint violation list', 'hydra:supportedProperty' => [['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => '#ConstraintViolationList/violation', '@type' => 'rdf:Property', 'rdfs:label' => 'violation', 'domain' => '#ConstraintViolationList', 'range' => '#ConstraintViolation'], 'hydra:title' => 'violation', 'hydra:description' => 'The violations', 'hydra:readable' => true, 'hydra:writable' => false]]];
     return ['@context' => $this->getContext(), '@id' => $this->router->generate('api_hydra_vocab'), 'hydra:title' => $this->title, 'hydra:description' => $this->description, 'hydra:entrypoint' => $this->router->generate('api_jsonld_entrypoint'), 'hydra:supportedClass' => $classes];
 }
Ejemplo n.º 3
0
 /**
  * Gets metadata for the given resource with the current context.
  *
  * Fallback to the resource own groups if no context is provided.
  *
  * @param ResourceInterface $resource
  * @param array             $context
  *
  * @return ClassMetadataInterface
  */
 private function getMetadata(ResourceInterface $resource, array $context)
 {
     return $this->apiClassMetadataFactory->getMetadataFor($resource->getEntityClass(), isset($context['jsonld_normalization_groups']) ? $context['jsonld_normalization_groups'] : $resource->getNormalizationGroups(), isset($context['jsonld_denormalization_groups']) ? $context['jsonld_denormalization_groups'] : $resource->getDenormalizationGroups(), isset($context['jsonld_validation_groups']) ? $context['jsonld_validation_groups'] : $resource->getValidationGroups());
 }
Ejemplo n.º 4
0
 /**
  * Gets the identifier name.
  *
  * @param ResourceInterface $resource
  *
  * @return string
  */
 private function getIdentifierNameFromResource(ResourceInterface $resource)
 {
     $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
     return $classMetadata->getIdentifierName();
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function warmUp($cacheDir)
 {
     foreach ($this->resourceCollection as $resource) {
         $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
     }
 }