Skip to content

Cache ref files #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
class Reference implements SpecObjectInterface, DocumentContextInterface
{
static $file_cache = [];

private $_to;
private $_ref;
private $_jsonReference;
Expand Down Expand Up @@ -206,7 +208,7 @@ public function resolve(ReferenceContext $context = null)
// resolve in external document
$file = $context->resolveRelativeUri($jsonReference->getDocumentUri());
// TODO could be a good idea to cache loaded files in current context to avoid loading the same files over and over again
$referencedDocument = $this->fetchReferencedFile($file);
$referencedDocument = $this->getReferencedFile($file);
$referencedData = $jsonReference->getJsonPointer()->evaluate($referencedDocument);

if ($referencedData === null) {
Expand Down Expand Up @@ -258,6 +260,18 @@ public function resolve(ReferenceContext $context = null)
}
}

/**
* @throws UnresolvableReferenceException
*/
private function getReferencedFile($uri)
{
if(!isset(self::$file_cache[$uri])) {
self::$file_cache[$uri] = $this->fetchReferencedFile($uri);
}

return self::$file_cache[$uri];
}

/**
* @throws UnresolvableReferenceException
*/
Expand Down