Permalink
Browse files

Inject DocumentLoader into Processor

This is a first step to add support for PSR-7 HTTP libraries (see #4).

Squashed commit of the following:

commit b682a81cf94e21a8a579ea9187a27a97a19476da
Author: Markus Lanthaler <mark_lanthaler@gmx.net>
Date:   Mon Oct 10 17:35:51 2016 +1300

    Various fixes

commit ab499e0011df1528ed7e009fbdb8c3fffff75e58
Author: zema <zlobzn@gmail.com>
Date:   Fri Jul 29 12:01:34 2016 +0300

    injection of RemoteDocumentLoader

commit 967c1a2593396cba470356e100af42366c48d1b1
Author: zema <zlobzn@gmail.com>
Date:   Fri Jul 29 11:57:19 2016 +0300

    injection of RemoteDocumentLoader

commit 252f65deaf9e258b8e573e18659b1d0403a45e74
Author: zema <zlobzn@gmail.com>
Date:   Fri Jul 29 11:52:17 2016 +0300

    injection of RemoteDocumentLoader

commit 86d04d2a1671e7e09e86689239a5f4c53735d6f8
Author: zema <zlobzn@gmail.com>
Date:   Fri Jul 29 10:23:13 2016 +0300

    injection of RemoteDocumentLoader

This closes #72.
  • Loading branch information...
1 parent 8eaca98 commit f10bbfe79e6e7471df375d351e61ad4b8b3671d7 @lanthaler committed Oct 10, 2016
Showing with 147 additions and 68 deletions.
  1. +31 −0 DocumentLoaderInterface.php
  2. +5 −11 FileGetContentsLoader.php
  3. +63 −19 JsonLD.php
  4. +33 −32 Processor.php
  5. +11 −2 Test/GraphTest.php
  6. +4 −4 Test/ValueTest.php
@@ -0,0 +1,31 @@
+<?php
+
+/*
+ * (c) Markus Lanthaler <mail@markus-lanthaler.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ML\JsonLD;
+
+use ML\JsonLD\Exception\JsonLdException;
+
+/**
+ * Interface for (remote) document loaders
+ *
+ * @author Markus Lanthaler <mail@markus-lanthaler.com>
+ */
+interface DocumentLoaderInterface
+{
+ /**
+ * Load a (remote) document or context
+ *
+ * @param string $url The URL or path of the document to load.
+ *
+ * @return RemoteDocument The loaded document.
+ *
+ * @throws JsonLdException
+ */
+ public function loadDocument($url);
+}
@@ -17,18 +17,12 @@
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
-class FileGetContentsLoader
+class FileGetContentsLoader implements DocumentLoaderInterface
{
/**
- * Loads a remote document or context
- *
- * @param string $url The URL of the document to load.
- *
- * @return RemoteDocument The remote document.
- *
- * @throws JsonLdException If loading the document failed.
+ * {@inheritdoc}
*/
- public static function loadDocument($url)
+ public function loadDocument($url)
{
// if input looks like a file, try to retrieve it
$input = trim($url);
@@ -80,7 +74,7 @@ function ($code, $severity, $msg, $msgCode, $bytesTx, $bytesMax) use (
}
}
- $linkHeaderValues = self::parseContextLinkHeaders($linkHeaderValues, new IRI($url));
+ $linkHeaderValues = $this->parseContextLinkHeaders($linkHeaderValues, new IRI($url));
if (count($linkHeaderValues) === 1) {
$remoteDocument->contextUrl = reset($linkHeaderValues);
@@ -129,7 +123,7 @@ function ($code, $severity, $msg, $msgCode, $bytesTx, $bytesMax) use (
*
* @return array An array of parsed HTTP Link headers
*/
- private static function parseContextLinkHeaders(array $values, IRI $baseIri)
+ private function parseContextLinkHeaders(array $values, IRI $baseIri)
{
// Separate multiple links contained in a single header value
for ($i = 0, $total = count($values); $i < $total; $i++) {
View
@@ -38,6 +38,8 @@ class JsonLD
/** Identifier for the merged graph */
const MERGED_GRAPH = '@merged';
+ private static $documentLoader = null;
+
/**
* Load and parse a JSON-LD document
*
@@ -62,6 +64,9 @@ class JsonLD
*
* <dt>documentFactory</dt>
* <dd>The document factory.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -78,9 +83,11 @@ class JsonLD
*/
public static function getDocument($input, $options = null)
{
+ $options = self::mergeOptions($options);
+
$input = self::expand($input, $options);
- $processor = new Processor(self::mergeOptions($options));
+ $processor = new Processor($options);
return $processor->getDocument($input);
}
@@ -106,6 +113,9 @@ public static function getDocument($input, $options = null)
* <dt>expandContext</dt>
* <dd>An optional context to use additionally to the context embedded
* in input when expanding the input.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -129,7 +139,7 @@ public static function expand($input, $options = null)
$activectx = array('@base' => null);
if (is_string($input)) {
- $remoteDocument = FileGetContentsLoader::loadDocument($input);
+ $remoteDocument = $options->documentLoader->loadDocument($input);
$input = $remoteDocument->document;
$activectx['@base'] = new IRI($remoteDocument->documentUrl);
@@ -191,6 +201,9 @@ public static function expand($input, $options = null)
* <dt>compactArrays</dt>
* <dd>If set to true, arrays holding just one element are compacted
* to scalars, otherwise the arrays are kept as arrays.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -226,7 +239,7 @@ public static function compact($input, $context = null, $options = null)
* @param array $input The JSON-LD document to
* compact.
* @param null|string|object|array $context The context.
- * @param null|object $options Options to configure the
+ * @param object $options Options to configure the
* compaction process.
* @param bool $alwaysGraph If set to true, the resulting
* document will always explicitly
@@ -237,10 +250,10 @@ public static function compact($input, $context = null, $options = null)
*
* @throws JsonLdException
*/
- private static function doCompact($input, $context = null, $options = null, $alwaysGraph = false)
+ private static function doCompact($input, $context, $options, $alwaysGraph = false)
{
- if (null !== $context) {
- $context = Processor::loadDocument($context);
+ if (is_string($context)) {
+ $context = $options->documentLoader->loadDocument($context)->document;
}
if (is_object($context) && property_exists($context, '@context')) {
@@ -314,6 +327,9 @@ private static function doCompact($input, $context = null, $options = null, $alw
* The default graph is identified by {@link DEFAULT_GRAPH} and the
* merged dataset graph by {@link MERGED_GRAPH}. If <em>null</em> is
* passed, all graphs will be returned.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -370,6 +386,9 @@ public static function flatten($input, $context = null, $options = null)
* <dt>expandContext</dt>
* <dd>An optional context to use additionally to the context embedded
* in input when expanding the input.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -418,6 +437,9 @@ public static function toRdf($input, $options = null)
*
* <dt>useRdfType</dt>
* <dd>If set to true, <em>rdf:type</em> will be used instead of <em>@type</em>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -473,6 +495,9 @@ public static function fromRdf(array $quads, $options = null)
* <dt>compactArrays</dt>
* <dd>If set to true, arrays holding just one element are compacted
* to scalars, otherwise the arrays are kept as arrays.</dd>
+ *
+ * <dt>documentLoader</dt>
+ * <dd>The document loader.</dd>
* </dl>
*
* The options parameter might be passed as associative array or as
@@ -494,7 +519,9 @@ public static function frame($input, $frame, $options = null)
$options = self::mergeOptions($options);
$input = self::expand($input, $options);
- $frame = Processor::loadDocument($frame);
+ $frame = (is_string($frame))
+ ? $options->documentLoader->loadDocument($frame)->document
+ : $frame;
if (false === is_object($frame)) {
throw new JsonLdException(
@@ -592,7 +619,8 @@ private static function mergeOptions($options)
'useNativeTypes' => false,
'useRdfType' => false,
'produceGeneralizedRdf' => false,
- 'documentFactory' => null
+ 'documentFactory' => null,
+ 'documentLoader' => new FileGetContentsLoader()
);
if (is_array($options) || is_object($options)) {
@@ -606,17 +634,6 @@ private static function mergeOptions($options)
throw new \InvalidArgumentException('The "base" option must be set to null or an absolute IRI.');
}
}
- if (property_exists($options, 'expandContext')) {
- if (is_string($options->expandContext)) {
- $result->expandContext = Processor::loadDocument($options->expandContext);
- } elseif (is_object($options->expandContext)) {
- $result->expandContext = $options->expandContext;
- }
-
- if (is_object($result->expandContext) && property_exists($result->expandContext, '@context')) {
- $result->expandContext = $result->expandContext->{'@context'};
- }
- }
if (property_exists($options, 'compactArrays') && is_bool($options->compactArrays)) {
$result->compactArrays = $options->compactArrays;
}
@@ -639,8 +656,35 @@ private static function mergeOptions($options)
($options->documentFactory instanceof DocumentFactoryInterface)) {
$result->documentFactory = $options->documentFactory;
}
+ if (property_exists($options, 'documentLoader') &&
+ ($options->documentLoader instanceof DocumentLoaderInterface)) {
+ $result->documentLoader = $options->documentLoader;
+ }
+ if (property_exists($options, 'expandContext')) {
+ if (is_string($options->expandContext)) {
+ $result->expandContext = $result->documentLoader->loadDocument($options->expandContext)->document;
+ } elseif (is_object($options->expandContext)) {
+ $result->expandContext = $options->expandContext;
+ }
+ if (is_object($result->expandContext) && property_exists($result->expandContext, '@context')) {
+ $result->expandContext = $result->expandContext->{'@context'};
+ }
+ }
}
return $result;
}
+
+ /**
+ * Set the default document loader.
+ *
+ * It can be overridden in individual operations by setting the
+ * `documentLoader` option.
+ *
+ * @param DocumentLoaderInterface $documentLoader
+ */
+ public static function setDefaultDocumentLoader(DocumentLoaderInterface $documentLoader)
+ {
+ self::$documentLoader = $documentLoader;
+ }
}
View
@@ -118,6 +118,11 @@ class Processor
private $documentFactory = null;
/**
+ * @var DocumentLoaderInterface The document loader
+ */
+ private $documentLoader = null;
+
+ /**
* Constructor
*
* The options parameter must be passed and all off the following properties
@@ -160,37 +165,7 @@ public function __construct($options)
$this->useRdfType = (bool) $options->useRdfType;
$this->generalizedRdf = (bool) $options->produceGeneralizedRdf;
$this->documentFactory = $options->documentFactory;
- }
-
- /**
- * Load a JSON-LD document
- *
- * The document can be supplied directly as string, by passing a file
- * path, or by passing a URL.
- *
- * Usage:
- * <code>
- * $document = Processor::loadDocument('document.jsonld');
- * print_r($document);
- * </code>
- *
- * @param null|string|array|object $input The JSON-LD document or a path
- * or URL pointing to one.
- *
- * @return mixed The loaded JSON-LD document
- *
- * @throws JsonLdException
- */
- public static function loadDocument($input)
- {
- if (false === is_string($input)) {
- // Return as is - it has already been parsed
- return $input;
- }
-
- $document = FileGetContentsLoader::loadDocument($input);
-
- return $document->document;
+ $this->documentLoader = $options->documentLoader;
}
/**
@@ -1772,7 +1747,7 @@ public function processContext($loclctx, &$activectx, $remotectxs = array())
$remotectxs[] = $remoteContext;
try {
- $remoteContext = self::loadDocument($remoteContext);
+ $remoteContext = $this->loadDocument($remoteContext);
} catch (JsonLdException $e) {
throw new JsonLdException(
JsonLdException::LOADING_REMOTE_CONTEXT_FAILED,
@@ -1800,6 +1775,31 @@ public function processContext($loclctx, &$activectx, $remotectxs = array())
}
/**
+ * Load a JSON-LD document
+ *
+ * The document can be supplied directly as string, by passing a file
+ * path, or by passing a URL.
+ *
+ * @param null|string|array|object $input The JSON-LD document or a path
+ * or URL pointing to one.
+ *
+ * @return mixed The loaded JSON-LD document
+ *
+ * @throws JsonLdException
+ */
+ private function loadDocument($input)
+ {
+ if (false === is_string($input)) {
+ // Return as is - it has already been parsed
+ return $input;
+ }
+
+ $document = $this->documentLoader->loadDocument($input);
+
+ return $document->document;
+ }
+
+ /**
* Creates an inverse context to simplify IRI compaction
*
* The inverse context is a multidimensional array that has the
@@ -2508,6 +2508,7 @@ public function frame($element, $frame)
$procOptions->useRdfType = $this->useRdfType;
$procOptions->produceGeneralizedRdf = $this->generalizedRdf;
$procOptions->documentFactory = $this->documentFactory;
+ $procOptions->documentLoader = $this->documentLoader;
$processor = new Processor($procOptions);
Oops, something went wrong.

0 comments on commit f10bbfe

Please sign in to comment.