Skip to content

Commit e9b0797

Browse files
committed
Add file existence check and exception handling
Imported the file_exists function in FileGetContents.php and implemented a check for file existence before getting its content. In QueryInterceptor.php, added InjectorInterface for dependency injection and handled SqlFileNotReadableException to maintain backward compatibility.
1 parent 8252364 commit e9b0797

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/FileGetContents.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Ray\Query\Exception\SqlFileNotReadableException;
88

9+
use function file_exists;
910
use function file_get_contents;
1011

1112
final class FileGetContents implements FileGetContentsInterface
@@ -15,6 +16,10 @@ final class FileGetContents implements FileGetContentsInterface
1516
*/
1617
public function __invoke(string $filePath): string
1718
{
19+
if (! file_exists($filePath)) {
20+
throw new SqlFileNotReadableException($filePath);
21+
}
22+
1823
$content = file_get_contents($filePath);
1924
if ($content === false) {
2025
// @codeCoverageIgnoreStart

src/QueryInterceptor.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Ray\Aop\MethodInterceptor;
1010
use Ray\Aop\MethodInvocation;
1111
use Ray\Aop\ReflectionMethod;
12+
use Ray\Di\InjectorInterface;
1213
use Ray\Query\Annotation\Query;
1314
use Ray\Query\Exception\SqlFileNotFoundException;
1415
use Ray\Query\Exception\SqlFileNotReadableException;
@@ -30,14 +31,19 @@ class QueryInterceptor implements MethodInterceptor
3031
/** @var FileGetContentsInterface */
3132
private $fileGetContents;
3233

34+
/** @var InjectorInterface */
35+
private $injector;
36+
3337
public function __construct(
3438
ExtendedPdoInterface $pdo,
3539
SqlDir $sqlDir,
36-
FileGetContentsInterface $fileGetContents
40+
FileGetContentsInterface $fileGetContents,
41+
InjectorInterface $injector
3742
) {
3843
$this->sqlDir = $sqlDir;
3944
$this->pdo = $pdo;
4045
$this->fileGetContents = $fileGetContents;
46+
$this->injector = $injector;
4147
}
4248

4349
/** @return ResourceObject|mixed */
@@ -50,7 +56,18 @@ public function invoke(MethodInvocation $invocation)
5056
$namedArguments = (array) $invocation->getNamedArguments();
5157
[$queryId, $params] = $query->templated ? $this->templated($query, $namedArguments) : [$query->id, $namedArguments];
5258
assert(is_string($queryId));
53-
$sql = $this->getsql($queryId, $method);
59+
$filePath = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);
60+
try {
61+
$sql = ($this->fileGetContents)($filePath);
62+
} catch (SqlFileNotReadableException $e) {
63+
// For BC
64+
// @codeCoverageIgnoreStart
65+
$sqlQuery = $this->injector->getInstance(RowListInterface::class, $queryId);
66+
67+
return $this->getQueryResult($invocation, $sqlQuery, $params);
68+
// @codeCoverageIgnoreEnd
69+
}
70+
5471
$sqlQuery = $query->type === 'row' ? new SqlQueryRow($this->pdo, $sql) : new SqlQueryRowList($this->pdo, $sql);
5572

5673
/** @var array<string, mixed> $params */

0 commit comments

Comments
 (0)