Skip to content

Commit 8252364

Browse files
committed
Refactor GetSql to FileGetContents
The GetSql class and interface have been renamed to FileGetContents to better describe the functionality they provide. Associated classes and tests have been updated for this change. A new exception, SqlFileNotReadableException, has been introduced to address issues when files cannot be read. Also, the contents manipulation from GetSqlWithFileName constructor and SqlFileName class have been shifted to the new FileGetContents class.
1 parent 02d3a82 commit 8252364

13 files changed

+110
-44
lines changed

src/CallableQueryModule.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use RegexIterator;
1414
use SplFileInfo;
1515

16-
use function file_get_contents;
1716
use function pathinfo;
1817
use function trim;
1918

@@ -29,7 +28,9 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null, ?cal
2928
{
3029
$this->sqlDir = $sqlDir;
3130
$this->getSql = $getSql ?? static function (SplFileInfo $fileInfo): string {
32-
return trim((string) file_get_contents($fileInfo->getPathname()));
31+
$getContents = new FileGetContents();
32+
33+
return $getContents($fileInfo->getPathname());
3334
};
3435

3536
parent::__construct($module);
@@ -40,7 +41,7 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null, ?cal
4041
*/
4142
protected function configure()
4243
{
43-
$this->bind(GetSqlInterface::class)->to(GetSql::class)->in(Scope::SINGLETON);
44+
$this->bind(FileGetContentsInterface::class)->to(FileGetContents::class)->in(Scope::SINGLETON);
4445
$this->bind(SqlDir::class)->toInstance(new SqlDir($this->sqlDir));
4546
/** @var SplFileInfo $fileInfo */
4647
foreach ($this->files($this->sqlDir) as $fileInfo) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ray\Query\Exception;
6+
7+
use RuntimeException;
8+
9+
class SqlFileNotReadableException extends RuntimeException
10+
{
11+
}

src/FileGetContents.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ray\Query;
6+
7+
use Ray\Query\Exception\SqlFileNotReadableException;
8+
9+
use function file_get_contents;
10+
11+
final class FileGetContents implements FileGetContentsInterface
12+
{
13+
/**
14+
* {@inheritDoc}
15+
*/
16+
public function __invoke(string $filePath): string
17+
{
18+
$content = file_get_contents($filePath);
19+
if ($content === false) {
20+
// @codeCoverageIgnoreStart
21+
throw new SqlFileNotReadableException($filePath);
22+
// @codeCoverageIgnoreEnd
23+
}
24+
25+
return $content;
26+
}
27+
}

src/GetSqlInterface.php renamed to src/FileGetContentsInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Ray\Query;
66

7-
interface GetSqlInterface
7+
interface FileGetContentsInterface
88
{
99
public function __invoke(string $filePath): string;
1010
}

src/GetSqlWithFileName.php renamed to src/FileGetContentsWithFileName.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66

77
use SplFileInfo;
88

9-
use function file_get_contents;
109
use function sprintf;
11-
use function trim;
1210

13-
final class GetSqlWithFileName implements GetSqlInterface
11+
final class FileGetContentsWithFileName implements FileGetContentsInterface
1412
{
13+
/** @var FileGetContents */
14+
private $getContents;
15+
16+
public function __construct(FileGetContents $getContents)
17+
{
18+
$this->getContents = $getContents;
19+
}
20+
1521
/**
1622
* {@inheritDoc}
1723
*/
1824
public function __invoke(string $filePath): string
1925
{
2026
$fileInfo = new SplFileInfo($filePath);
27+
$content = ($this->getContents)($filePath);
2128

22-
return sprintf('/* %s */ %s', $fileInfo->getFilename(), trim((string) file_get_contents($filePath)));
29+
return sprintf('/* %s */ %s', $fileInfo->getFilename(), $content);
2330
}
2431
}

src/GetSql.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/QueryInterceptor.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
use Ray\Aop\ReflectionMethod;
1212
use Ray\Query\Annotation\Query;
1313
use Ray\Query\Exception\SqlFileNotFoundException;
14+
use Ray\Query\Exception\SqlFileNotReadableException;
1415

1516
use function assert;
16-
use function file_exists;
17-
use function file_get_contents;
1817
use function is_string;
1918
use function parse_str;
2019
use function parse_url;
@@ -28,12 +27,17 @@ class QueryInterceptor implements MethodInterceptor
2827
/** @var ExtendedPdoInterface */
2928
private $pdo;
3029

30+
/** @var FileGetContentsInterface */
31+
private $fileGetContents;
32+
3133
public function __construct(
3234
ExtendedPdoInterface $pdo,
33-
SqlDir $sqlDir
35+
SqlDir $sqlDir,
36+
FileGetContentsInterface $fileGetContents
3437
) {
3538
$this->sqlDir = $sqlDir;
3639
$this->pdo = $pdo;
40+
$this->fileGetContents = $fileGetContents;
3741
}
3842

3943
/** @return ResourceObject|mixed */
@@ -111,11 +115,12 @@ private function templated(Query $query, array $namedArguments): array
111115

112116
private function getsql(string $queryId, ReflectionMethod $method): string
113117
{
114-
$file = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);
115-
if (! file_exists($file)) {
118+
$filePath = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);
119+
120+
try {
121+
return ($this->fileGetContents)($filePath);
122+
} catch (SqlFileNotReadableException $e) {
116123
throw new SqlFileNotFoundException((string) $method, $queryId);
117124
}
118-
119-
return (string) file_get_contents($file);
120125
}
121126
}

src/SqlFileName.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66

77
use SplFileInfo;
88

9-
use function file_get_contents;
10-
use function sprintf;
11-
use function trim;
12-
139
final class SqlFileName
1410
{
1511
/**
1612
* Return sql file name commented SQL
1713
*/
1814
public function __invoke(SplFileInfo $fileInfo): string
1915
{
20-
return sprintf('/* %s */ %s', $fileInfo->getFilename(), trim((string) file_get_contents($fileInfo->getPathname())));
16+
$getFileContents = new FileGetContentsWithFileName(new FileGetContents());
17+
$filePath = $fileInfo->getPathname();
18+
19+
return $getFileContents($filePath);
2120
}
2221
}

src/SqlFileNameModule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ class SqlFileNameModule extends AbstractModule
1414
*/
1515
protected function configure(): void
1616
{
17-
$this->bind(GetSqlInterface::class)->to(GetSqlWithFileName::class)->in(Scope::SINGLETON);
17+
$this->bind(FileGetContentsInterface::class)->to(FileGetContentsWithFileName::class)->in(Scope::SINGLETON);
1818
}
1919
}

src/SqlFinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ final class SqlFinder implements SqlFinderInterface
2121
/** @var SqlDir */
2222
private $sqlDir;
2323

24-
/** @var GetSqlInterface */
24+
/** @var FileGetContentsInterface */
2525
private $getSql;
2626

2727
/** @param ParamReaderInterface<object> $reader */
2828
public function __construct(
2929
ParamReaderInterface $reader,
3030
SqlDir $sqlDir,
31-
GetSqlInterface $getSql
31+
FileGetContentsInterface $getSql
3232
) {
3333
$this->reader = $reader;
3434
$this->sqlDir = $sqlDir;

0 commit comments

Comments
 (0)