Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/Query/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ class QueryFactory {
"sql" => SqlQuery::class,
"php" => PhpQuery::class,
];
/** @var array<string, string> */
protected array $queryFilePathCache = [];

public function __construct(
protected string $queryHolder,
protected Driver $driver
) {}

public function findQueryFilePath(string $name):string {
if(isset($this->queryFilePathCache[$name])) {
return $this->queryFilePathCache[$name];
}

if(is_dir($this->queryHolder)) {
$queryFilePath = $this->findQueryFilePathInDirectory(
$this->queryHolder,
$name
);
if($queryFilePath) {
return $queryFilePath;
return $this->queryFilePathCache[$name] = $queryFilePath;
}
}
elseif(is_file($this->queryHolder)) {
Expand All @@ -46,11 +52,11 @@ public function findQueryFilePath(string $name):string {
);
}

return $queryFilePath;
return $this->queryFilePathCache[$name] = $queryFilePath;
}
}

return "$this->queryHolder::$name";
return $this->queryFilePathCache[$name] = "$this->queryHolder::$name";
}

throw new QueryNotFoundException($this->queryHolder . ", " . $name);
Expand Down
63 changes: 63 additions & 0 deletions test/phpunit/Query/QueryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,67 @@ public function testFindQueryFilePathPrefersSupportedExtensionOverEditorBackup()
Helper::deleteDir($basePath);
}
}

public function testFindQueryFilePathCachesResolvedPaths():void {
$basePath = Helper::getTmpDir();
$queryDirectory = implode(DIRECTORY_SEPARATOR, [
$basePath,
"query",
]);
mkdir($queryDirectory, 0775, true);
file_put_contents("$queryDirectory/report.sql", "select 1");

try {
$sut = $this->createCountingQueryFactory($queryDirectory);

$firstPath = $sut->findQueryFilePath("report");
$secondPath = $sut->findQueryFilePath("report");

self::assertSame($firstPath, $secondPath);
self::assertSame(1, $sut->directoryScanCount);
}
finally {
Helper::deleteDir($basePath);
}
}

public function testCreateCachesResolvedPathsBetweenCalls():void {
$basePath = Helper::getTmpDir();
$queryDirectory = implode(DIRECTORY_SEPARATOR, [
$basePath,
"query",
]);
mkdir($queryDirectory, 0775, true);
file_put_contents("$queryDirectory/report.sql", "select 1");

try {
$sut = $this->createCountingQueryFactory($queryDirectory);

$firstQuery = $sut->create("report");
$secondQuery = $sut->create("report");

self::assertSame($firstQuery->getFilePath(), $secondQuery->getFilePath());
self::assertSame(1, $sut->directoryScanCount);
}
finally {
Helper::deleteDir($basePath);
}
}

private function createCountingQueryFactory(string $queryDirectory):QueryFactory {
return new class(
$queryDirectory,
new Driver(new DefaultSettings())
) extends QueryFactory {
public int $directoryScanCount = 0;

protected function findQueryFilePathInDirectory(
string $directory,
string $name,
):?string {
$this->directoryScanCount++;
return parent::findQueryFilePathInDirectory($directory, $name);
}
};
}
}
Loading