Skip to content

Commit

Permalink
Local cache for Reference contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Dec 7, 2019
1 parent 3626276 commit bca4e50
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/JsonSchemaRuntime/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class Reference
{
private static $cache = [];

private $resolved;

private $referenceUri;
Expand Down Expand Up @@ -75,18 +77,24 @@ public function resolve(callable $deserializeCallback = null)
*/
protected function doResolve()
{
// @TODO Better handling of getting the content of a file
$json = file_get_contents((string) $this->mergedUri->withFragment(''));
$fragment = (string) $this->mergedUri->withFragment('');

if (!\array_key_exists($fragment, self::$cache)) {
$contents = file_get_contents($fragment);

if (!json_decode($contents) || JSON_ERROR_NONE !== json_last_error()) {
$decoded = Yaml::parse($contents,
Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_DATETIME | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
$contents = json_encode($decoded);
}

if (!json_decode($json) || JSON_ERROR_NONE !== json_last_error()) {
$decoded = Yaml::parse($json, Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_DATETIME | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
$json = json_encode($decoded);
self::$cache[$fragment] = $contents;
}

$pointer = new Pointer($json);
$pointer = new Pointer(self::$cache[$fragment]);

if ('' === $this->mergedUri->getFragment()) {
return json_decode($json);
return json_decode(self::$cache[$fragment]);
}

return $pointer->get($this->mergedUri->getFragment());
Expand Down

0 comments on commit bca4e50

Please sign in to comment.