From 825236406cad5494ec7c1b375c540d89841a0a2b Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 13 Jun 2024 01:42:01 +0900 Subject: [PATCH] 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. --- src/CallableQueryModule.php | 7 ++-- src/Exception/SqlFileNotReadableException.php | 11 ++++++ src/FileGetContents.php | 27 ++++++++++++++ ...rface.php => FileGetContentsInterface.php} | 2 +- ...me.php => FileGetContentsWithFileName.php} | 15 +++++--- src/GetSql.php | 18 ---------- src/QueryInterceptor.php | 19 ++++++---- src/SqlFileName.php | 9 +++-- src/SqlFileNameModule.php | 2 +- src/SqlFinder.php | 4 +-- src/SqlQueryModule.php | 3 +- tests/Iso8601FormatModuleTest.php | 35 ++++++++++++++++++- tests/SqlQueryModuleTest.php | 2 +- 13 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 src/Exception/SqlFileNotReadableException.php create mode 100644 src/FileGetContents.php rename src/{GetSqlInterface.php => FileGetContentsInterface.php} (76%) rename src/{GetSqlWithFileName.php => FileGetContentsWithFileName.php} (50%) delete mode 100644 src/GetSql.php diff --git a/src/CallableQueryModule.php b/src/CallableQueryModule.php index d458c9b..d3a9b7c 100644 --- a/src/CallableQueryModule.php +++ b/src/CallableQueryModule.php @@ -13,7 +13,6 @@ use RegexIterator; use SplFileInfo; -use function file_get_contents; use function pathinfo; use function trim; @@ -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); @@ -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) { diff --git a/src/Exception/SqlFileNotReadableException.php b/src/Exception/SqlFileNotReadableException.php new file mode 100644 index 0000000..ff02f47 --- /dev/null +++ b/src/Exception/SqlFileNotReadableException.php @@ -0,0 +1,11 @@ +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); } } diff --git a/src/GetSql.php b/src/GetSql.php deleted file mode 100644 index 5ae338e..0000000 --- a/src/GetSql.php +++ /dev/null @@ -1,18 +0,0 @@ -sqlDir = $sqlDir; $this->pdo = $pdo; + $this->fileGetContents = $fileGetContents; } /** @return ResourceObject|mixed */ @@ -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); } } diff --git a/src/SqlFileName.php b/src/SqlFileName.php index ab45591..0a016a5 100644 --- a/src/SqlFileName.php +++ b/src/SqlFileName.php @@ -6,10 +6,6 @@ use SplFileInfo; -use function file_get_contents; -use function sprintf; -use function trim; - final class SqlFileName { /** @@ -17,6 +13,9 @@ final class SqlFileName */ 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); } } diff --git a/src/SqlFileNameModule.php b/src/SqlFileNameModule.php index 52cc054..fda2de8 100644 --- a/src/SqlFileNameModule.php +++ b/src/SqlFileNameModule.php @@ -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); } } diff --git a/src/SqlFinder.php b/src/SqlFinder.php index 8391fae..8527699 100644 --- a/src/SqlFinder.php +++ b/src/SqlFinder.php @@ -21,14 +21,14 @@ final class SqlFinder implements SqlFinderInterface /** @var SqlDir */ private $sqlDir; - /** @var GetSqlInterface */ + /** @var FileGetContentsInterface */ private $getSql; /** @param ParamReaderInterface $reader */ public function __construct( ParamReaderInterface $reader, SqlDir $sqlDir, - GetSqlInterface $getSql + FileGetContentsInterface $getSql ) { $this->reader = $reader; $this->sqlDir = $sqlDir; diff --git a/src/SqlQueryModule.php b/src/SqlQueryModule.php index ab46b10..9a89a9d 100644 --- a/src/SqlQueryModule.php +++ b/src/SqlQueryModule.php @@ -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); diff --git a/tests/Iso8601FormatModuleTest.php b/tests/Iso8601FormatModuleTest.php index 0e22ae0..78434ff 100644 --- a/tests/Iso8601FormatModuleTest.php +++ b/tests/Iso8601FormatModuleTest.php @@ -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'])); } }; @@ -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); + } } diff --git a/tests/SqlQueryModuleTest.php b/tests/SqlQueryModuleTest.php index 4d01653..e93b53c 100644 --- a/tests/SqlQueryModuleTest.php +++ b/tests/SqlQueryModuleTest.php @@ -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);