Skip to content

Commit

Permalink
Refactor GetSql to FileGetContents
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koriym committed Jun 12, 2024
1 parent 02d3a82 commit 8252364
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 44 deletions.
7 changes: 4 additions & 3 deletions src/CallableQueryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use RegexIterator;
use SplFileInfo;

use function file_get_contents;
use function pathinfo;
use function trim;

Expand All @@ -29,7 +28,9 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null, ?cal
{
$this->sqlDir = $sqlDir;
$this->getSql = $getSql ?? static function (SplFileInfo $fileInfo): string {
return trim((string) file_get_contents($fileInfo->getPathname()));
$getContents = new FileGetContents();

return $getContents($fileInfo->getPathname());
};

parent::__construct($module);
Expand All @@ -40,7 +41,7 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null, ?cal
*/
protected function configure()
{
$this->bind(GetSqlInterface::class)->to(GetSql::class)->in(Scope::SINGLETON);
$this->bind(FileGetContentsInterface::class)->to(FileGetContents::class)->in(Scope::SINGLETON);
$this->bind(SqlDir::class)->toInstance(new SqlDir($this->sqlDir));
/** @var SplFileInfo $fileInfo */
foreach ($this->files($this->sqlDir) as $fileInfo) {
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/SqlFileNotReadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Ray\Query\Exception;

use RuntimeException;

class SqlFileNotReadableException extends RuntimeException
{
}
27 changes: 27 additions & 0 deletions src/FileGetContents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Ray\Query\Exception\SqlFileNotReadableException;

use function file_get_contents;

final class FileGetContents implements FileGetContentsInterface
{
/**
* {@inheritDoc}
*/
public function __invoke(string $filePath): string
{
$content = file_get_contents($filePath);
if ($content === false) {
// @codeCoverageIgnoreStart
throw new SqlFileNotReadableException($filePath);
// @codeCoverageIgnoreEnd
}

return $content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ray\Query;

interface GetSqlInterface
interface FileGetContentsInterface
{
public function __invoke(string $filePath): string;
}
15 changes: 11 additions & 4 deletions src/GetSqlWithFileName.php → src/FileGetContentsWithFileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@

use SplFileInfo;

use function file_get_contents;
use function sprintf;
use function trim;

final class GetSqlWithFileName implements GetSqlInterface
final class FileGetContentsWithFileName implements FileGetContentsInterface
{
/** @var FileGetContents */
private $getContents;

public function __construct(FileGetContents $getContents)
{
$this->getContents = $getContents;
}

/**
* {@inheritDoc}
*/
public function __invoke(string $filePath): string
{
$fileInfo = new SplFileInfo($filePath);
$content = ($this->getContents)($filePath);

return sprintf('/* %s */ %s', $fileInfo->getFilename(), trim((string) file_get_contents($filePath)));
return sprintf('/* %s */ %s', $fileInfo->getFilename(), $content);
}
}
18 changes: 0 additions & 18 deletions src/GetSql.php

This file was deleted.

19 changes: 12 additions & 7 deletions src/QueryInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Ray\Aop\ReflectionMethod;
use Ray\Query\Annotation\Query;
use Ray\Query\Exception\SqlFileNotFoundException;
use Ray\Query\Exception\SqlFileNotReadableException;

use function assert;
use function file_exists;
use function file_get_contents;
use function is_string;
use function parse_str;
use function parse_url;
Expand All @@ -28,12 +27,17 @@ class QueryInterceptor implements MethodInterceptor
/** @var ExtendedPdoInterface */
private $pdo;

/** @var FileGetContentsInterface */
private $fileGetContents;

public function __construct(
ExtendedPdoInterface $pdo,
SqlDir $sqlDir
SqlDir $sqlDir,
FileGetContentsInterface $fileGetContents
) {
$this->sqlDir = $sqlDir;
$this->pdo = $pdo;
$this->fileGetContents = $fileGetContents;
}

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

private function getsql(string $queryId, ReflectionMethod $method): string
{
$file = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);
if (! file_exists($file)) {
$filePath = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);

try {
return ($this->fileGetContents)($filePath);
} catch (SqlFileNotReadableException $e) {
throw new SqlFileNotFoundException((string) $method, $queryId);
}

return (string) file_get_contents($file);
}
}
9 changes: 4 additions & 5 deletions src/SqlFileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

use SplFileInfo;

use function file_get_contents;
use function sprintf;
use function trim;

final class SqlFileName
{
/**
* Return sql file name commented SQL
*/
public function __invoke(SplFileInfo $fileInfo): string
{
return sprintf('/* %s */ %s', $fileInfo->getFilename(), trim((string) file_get_contents($fileInfo->getPathname())));
$getFileContents = new FileGetContentsWithFileName(new FileGetContents());
$filePath = $fileInfo->getPathname();

return $getFileContents($filePath);
}
}
2 changes: 1 addition & 1 deletion src/SqlFileNameModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class SqlFileNameModule extends AbstractModule
*/
protected function configure(): void
{
$this->bind(GetSqlInterface::class)->to(GetSqlWithFileName::class)->in(Scope::SINGLETON);
$this->bind(FileGetContentsInterface::class)->to(FileGetContentsWithFileName::class)->in(Scope::SINGLETON);
}
}
4 changes: 2 additions & 2 deletions src/SqlFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ final class SqlFinder implements SqlFinderInterface
/** @var SqlDir */
private $sqlDir;

/** @var GetSqlInterface */
/** @var FileGetContentsInterface */
private $getSql;

/** @param ParamReaderInterface<object> $reader */
public function __construct(
ParamReaderInterface $reader,
SqlDir $sqlDir,
GetSqlInterface $getSql
FileGetContentsInterface $getSql
) {
$this->reader = $reader;
$this->sqlDir = $sqlDir;
Expand Down
3 changes: 2 additions & 1 deletion src/SqlQueryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null)
*/
protected function configure()
{
$this->bind(GetSqlInterface::class)->to(GetSql::class)->in(Scope::SINGLETON);
$this->bind(FileGetContents::class);
$this->bind(FileGetContentsInterface::class)->to(FileGetContents::class)->in(Scope::SINGLETON);
$this->bind(SqlDir::class)->toInstance(new SqlDir($this->sqlDir));
$this->bind(SqlFinderInterface::class)->to(SqlFinder::class)->in(Scope::SINGLETON);
$this->bind(ParamReaderInterface::class)->to(ParamReader::class)->in(Scope::SINGLETON);
Expand Down
35 changes: 34 additions & 1 deletion tests/Iso8601FormatModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(ExtendedPdo $pdo)
protected function configure()
{
$this->bind(ExtendedPdoInterface::class)->toInstance($this->pdo);
$this->install(new CallableQueryModule(__DIR__ . '/Fake/sql', null, new SqlFileName()));
$this->install(new CallableQueryModule(__DIR__ . '/Fake/sql', null));
$this->install(new Iso8601FormatModule(['created_at']));
}
};
Expand Down Expand Up @@ -77,4 +77,37 @@ public function testList(): void
];
$this->assertSame($expected, $actural);
}

public function testSqlFileName(): void
{
$pdo = new ExtendedPdo('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$pdo->query('CREATE TABLE IF NOT EXISTS todo (
id INTEGER,
title TEXT,
created_at TIMESTAMP)');
$pdo->perform('INSERT INTO todo (id, title, created_at) VALUES (:id, :title, :created_at)', ['id' => '1', 'title' => 'run', 'created_at' => '1970-01-01 00:00:00']);

$module = new class ($pdo) extends AbstractModule {
/** @var ExtendedPdo */
private $pdo;

public function __construct(ExtendedPdo $pdo)
{
$this->pdo = $pdo;

parent::__construct();
}

protected function configure()
{
$this->bind(ExtendedPdoInterface::class)->toInstance($this->pdo);
$this->install(new CallableQueryModule(__DIR__ . '/Fake/sql', null, new SqlFileName()));
$this->install(new Iso8601FormatModule(['created_at']));
}
};
$injector = new Injector($this->module, __DIR__ . '/tmp');
$todo = $injector->getInstance(FakeTodo::class);
$this->assertInstanceOf(FakeTodo::class, $todo);
}
}
2 changes: 1 addition & 1 deletion tests/SqlQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testSqlNotAnnotated(): void
$injector->getInstance(FakeTodoProviderSqlNotAnnotated::class);
}

public function testDevSqlModule(): void
public function testSqlFileNameModule(): void
{
$pdo = new ExtendedPdo('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
Expand Down

0 comments on commit 8252364

Please sign in to comment.