Skip to content

Commit

Permalink
Merge pull request #24 from PcComponentes/feature/cache-schemas
Browse files Browse the repository at this point in the history
feat: Cache external reference files
  • Loading branch information
zoilomora authored Aug 14, 2023
2 parents 40016d8 + 869a4b4 commit 0002fc2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Behat/ValidatorApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

abstract class ValidatorApiContext
{
protected function checkSchemaFile($filename): void
private array $dataExternalReferences = [];

protected function checkSchemaFile(string $filename): void
{
if (false === \is_file($filename)) {
throw new \RuntimeException(
Expand All @@ -25,15 +27,21 @@ protected function getDataExternalReferences(array $allSpec, string $originalPat
foreach ($externalReferences as $externalReference) {
[$pathExternalReference] = \explode('#', $externalReference);
$newPath = \realpath(\dirname($originalPath) . '/' . $pathExternalReference);
$dataExternalReferences[$pathExternalReference] = $this->getDataExternalReferencesCached($newPath);
}

$this->checkSchemaFile($newPath);
$data = Yaml::parse(\file_get_contents($newPath));
$data = $this->getDataExternalReferences($data, $newPath);
return \array_merge($allSpec, $dataExternalReferences);
}

$dataExternalReferences[$pathExternalReference] = $data;
private function getDataExternalReferencesCached(string $path): array
{
if (false === \array_key_exists($path, $this->dataExternalReferences)) {
$this->checkSchemaFile($path);
$data = Yaml::parse(\file_get_contents($path));
$this->dataExternalReferences[$path] = $this->getDataExternalReferences($data, $path);
}

return \array_merge($allSpec, $dataExternalReferences);
return $this->dataExternalReferences[$path];
}

private function externalReferencesExtractor(array $array): array
Expand Down

0 comments on commit 0002fc2

Please sign in to comment.