This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from ec-europa/construct-queries
Support construct queries.
- Loading branch information
Showing
24 changed files
with
512 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\rdf_export\Encoder; | ||
|
||
use Drupal\rdf_export\RdfEncoderInterface; | ||
use EasyRdf\Format; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* Adds RDF encoder support for the Serialization API. | ||
*/ | ||
class RdfEncoder implements RdfEncoderInterface { | ||
|
||
/** | ||
* Static cache for supported formats. | ||
* | ||
* @var \EasyRdf\Serialiser[] | ||
*/ | ||
protected static $supportedFormats; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsEncoding($format): bool { | ||
return !empty(static::getSupportedFormats()[$format]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($data, $format, array $context = []): string { | ||
if (!isset($data['_rdf_entity'])) { | ||
throw new UnexpectedValueException("Data to be encoded is missing the '_rdf_entity' key."); | ||
} | ||
return $data['_rdf_entity']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSupportedFormats(): array { | ||
if (!isset(static::$supportedFormats)) { | ||
$container_registered_formats = \Drupal::getContainer()->getParameter('rdf_export.encoders'); | ||
$rdf_serializers = Format::getFormats(); | ||
static::$supportedFormats = array_intersect_key($rdf_serializers, $container_registered_formats); | ||
} | ||
return static::$supportedFormats; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Drupal\rdf_export\EventSubscriber; | ||
|
||
use Drupal\rdf_export\Encoder\RdfEncoder; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Event subscriber for adding RDF content types to the request. | ||
*/ | ||
class RdfSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* Register content type formats on the request object. | ||
* | ||
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event | ||
* The Event to process. | ||
*/ | ||
public function onKernelRequest(GetResponseEvent $event) { | ||
/** @var \EasyRdf\Format $format */ | ||
foreach (RdfEncoder::getSupportedFormats() as $format) { | ||
$mime = array_keys($format->getMimeTypes()); | ||
$event->getRequest()->setFormat($format->getName(), $mime); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events[KernelEvents::REQUEST][] = ['onKernelRequest']; | ||
return $events; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Drupal\rdf_export\Normalizer; | ||
|
||
use Drupal\rdf_export\Encoder\RdfEncoder; | ||
use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase; | ||
|
||
/** | ||
* Base class for Normalizers. | ||
*/ | ||
abstract class NormalizerBase extends SerializationNormalizerBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function checkFormat($format = NULL) { | ||
return !empty(RdfEncoder::getSupportedFormats()[$format]); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Drupal\rdf_export\Normalizer; | ||
|
||
use Drupal\rdf_entity\RdfInterface; | ||
use Drupal\rdf_export\RdfSerializer; | ||
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait; | ||
|
||
/** | ||
* Converts the Drupal entity object structure to a HAL array structure. | ||
*/ | ||
class RdfEntityNormalizer extends NormalizerBase { | ||
|
||
use FieldableEntityNormalizerTrait; | ||
|
||
/** | ||
* The interface or class that this Normalizer supports. | ||
* | ||
* @var string | ||
*/ | ||
protected $supportedInterfaceOrClass = RdfInterface::class; | ||
|
||
/** | ||
* RdfEntityNormalizer constructor. | ||
* | ||
* @param \Drupal\rdf_export\RdfSerializer $rdf_serializer | ||
* RDF Serializer service. | ||
*/ | ||
public function __construct(RdfSerializer $rdf_serializer) { | ||
$this->rdfSerializer = $rdf_serializer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($entity, $format = NULL, array $context = []) { | ||
return ['_rdf_entity' => $this->rdfSerializer->serializeEntity($entity, $format)]; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\rdf_export; | ||
|
||
use EasyRdf\Format; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Collects all RDF encoders and stores them into a service container parameter. | ||
*/ | ||
class RdfEncoderCompilerPass implements CompilerPassInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container): void { | ||
$rdf_formats = array_keys(Format::getFormats()); | ||
$encoders = []; | ||
foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) { | ||
$class = $container->getDefinition($id)->getClass(); | ||
$interfaces = class_implements($class); | ||
$format = $attributes[0]['format']; | ||
if (isset($interfaces[RdfEncoderInterface::class]) && in_array($format, $rdf_formats)) { | ||
$encoders[$format] = $format; | ||
} | ||
$container->setParameter('rdf_export.encoders', $encoders); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\rdf_export; | ||
|
||
use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
|
||
/** | ||
* Provides an interface to RDF encoders. | ||
*/ | ||
interface RdfEncoderInterface extends EncoderInterface { | ||
|
||
/** | ||
* Builds a list of supported formats. | ||
* | ||
* @return \EasyRdf\Serialiser[] | ||
* List of supported formats. | ||
*/ | ||
public static function getSupportedFormats(): array; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\rdf_export; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drupal\Core\DependencyInjection\ServiceProviderInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PassConfig; | ||
|
||
/** | ||
* RDF Export dependency injection container. | ||
*/ | ||
class RdfExportServiceProvider implements ServiceProviderInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function register(ContainerBuilder $container) { | ||
// Run this compiler pass after the child definitions were resolved. | ||
$container->addCompilerPass(new RdfEncoderCompilerPass(), PassConfig::TYPE_OPTIMIZE, -10); | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"@id":"http://example.com/apple","@type":["http://example.com/type/fruit"],"http://example.com/fruit/uid":[{"@value":0}],"http://example.com/fruit/label":[{"@value":"Apple","@language":"en"}]},{"@id":"http://example.com/type/fruit"}] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@prefix ns0: <http://example.com/fruit/> . | ||
|
||
<http://example.com/apple> | ||
a <http://example.com/type/fruit> ; | ||
ns0:uid 0 ; | ||
ns0:label "Apple"@en . |
3 changes: 3 additions & 0 deletions
3
modules/rdf_export/tests/fixtures/content-negotiation/ntriples
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<http://example.com/apple> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/type/fruit> . | ||
<http://example.com/apple> <http://example.com/fruit/uid> "0"^^<http://www.w3.org/2001/XMLSchema#integer> . | ||
<http://example.com/apple> <http://example.com/fruit/label> "Apple"@en . |
11 changes: 11 additions & 0 deletions
11
modules/rdf_export/tests/fixtures/content-negotiation/rdfxml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
xmlns:ns0="http://example.com/fruit/"> | ||
|
||
<rdf:Description rdf:about="http://example.com/apple"> | ||
<rdf:type rdf:resource="http://example.com/type/fruit"/> | ||
<ns0:uid rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">0</ns0:uid> | ||
<ns0:label xml:lang="en">Apple</ns0:label> | ||
</rdf:Description> | ||
|
||
</rdf:RDF> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@prefix ns0: <http://example.com/fruit/> . | ||
|
||
<http://example.com/apple> | ||
a <http://example.com/type/fruit> ; | ||
ns0:uid 0 ; | ||
ns0:label "Apple"@en . |
Oops, something went wrong.