Skip to content

Commit 6d6da00

Browse files
committed
Merge pull request #74 from netresearch/cache-schemas
Cache schemas
2 parents 603884a + 5c4a026 commit 6d6da00

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ public function retrieve($uri, $baseUri = null)
134134
$fetchUri = $resolver->generate($arParts);
135135
}
136136

137+
$jsonSchema = $this->loadSchema($fetchUri);
138+
139+
// Use the JSON pointer if specified
140+
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
141+
$jsonSchema->id = $resolvedUri;
142+
143+
return $jsonSchema;
144+
}
145+
146+
/**
147+
* Fetch a schema from the given URI, json-decode it and return it.
148+
* Caches schema objects.
149+
*
150+
* @param string $fetchUri Absolute URI
151+
*
152+
* @return object JSON schema object
153+
*/
154+
protected function loadSchema($fetchUri)
155+
{
156+
if (isset($this->schemaCache[$fetchUri])) {
157+
return $this->schemaCache[$fetchUri];
158+
}
159+
137160
$uriRetriever = $this->getUriRetriever();
138161
$contents = $this->uriRetriever->retrieve($fetchUri);
139162
$this->confirmMediaType($uriRetriever, $fetchUri);
@@ -143,10 +166,7 @@ public function retrieve($uri, $baseUri = null)
143166
throw new JsonDecodingException($error);
144167
}
145168

146-
// Use the JSON pointer if specified
147-
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
148-
$jsonSchema->id = $resolvedUri;
149-
169+
$this->schemaCache[$fetchUri] = $jsonSchema;
150170
return $jsonSchema;
151171
}
152172

0 commit comments

Comments
 (0)