-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47e7481
commit 916d685
Showing
8 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
data-access-kit/src/Repository/Method/SQLFileMethodCompiler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Method; | ||
|
||
use DataAccessKit\Repository\Attribute\SQL; | ||
use DataAccessKit\Repository\Attribute\SQLFile; | ||
use DataAccessKit\Repository\MethodCompilerInterface; | ||
use DataAccessKit\Repository\Result; | ||
use DataAccessKit\Repository\ResultMethod; | ||
use LogicException; | ||
use function dirname; | ||
use function file_get_contents; | ||
use function preg_match; | ||
use function preg_quote; | ||
use function rtrim; | ||
use function sprintf; | ||
use function var_dump; | ||
use const DIRECTORY_SEPARATOR; | ||
|
||
/** | ||
* @implements MethodCompilerInterface<SQLFile> | ||
*/ | ||
class SQLFileMethodCompiler implements MethodCompilerInterface | ||
{ | ||
public function __construct( | ||
private readonly SQLMethodCompiler $sqlMethodCompiler, | ||
) | ||
{ | ||
} | ||
|
||
public function compile(Result $result, ResultMethod $method, $attribute): void | ||
{ | ||
$file = $attribute->file; | ||
if ($file === "") { | ||
throw new LogicException(sprintf( | ||
"SQL file for method [%s:%s] is blank, please specify a file path.", | ||
$result->reflection->getName(), | ||
$method->reflection->getName(), | ||
)); | ||
|
||
} | ||
if (!preg_match('~^(\w+://)?(\w:)?' . preg_quote(DIRECTORY_SEPARATOR, '~') . '~', $file)) { | ||
$file = dirname($result->reflection->getFileName()) . DIRECTORY_SEPARATOR . $file; | ||
} | ||
|
||
$contents = file_get_contents($file); | ||
if ($contents === false) { | ||
throw new LogicException(sprintf( | ||
"SQL file for method [%s:%s] does not exist or is not readable.", | ||
$result->reflection->getName(), | ||
$method->reflection->getName(), | ||
)); | ||
} | ||
|
||
$this->sqlMethodCompiler->compile($result, $method, new SQL(rtrim($contents), $attribute->itemType)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
data-access-kit/test/Repository/Fixture/AbsoluteSQLFileRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\Repository\Attribute\Repository; | ||
use DataAccessKit\Repository\Attribute\SQLFile; | ||
|
||
#[Repository(Foo::class)] | ||
interface AbsoluteSQLFileRepositoryInterface | ||
{ | ||
#[SQLFile(__DIR__ . "/SQLFileRepository.findByTitle.sql")] | ||
public function findByTitle(string $title): array; | ||
} |
13 changes: 13 additions & 0 deletions
13
data-access-kit/test/Repository/Fixture/RelativeSQLFileRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\Repository\Attribute\Repository; | ||
use DataAccessKit\Repository\Attribute\SQLFile; | ||
|
||
#[Repository(Foo::class)] | ||
interface RelativeSQLFileRepositoryInterface | ||
{ | ||
#[SQLFile("SQLFileRepository.findByTitle.sql")] | ||
public function findByTitle(string $title): array; | ||
} |
3 changes: 3 additions & 0 deletions
3
data-access-kit/test/Repository/Fixture/SQLFileRepository.findByTitle.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT id, title | ||
FROM foos | ||
WHERE title = @title |
24 changes: 24 additions & 0 deletions
24
data-access-kit/test/Repository/Snapshot/CompilerTest.testCompile.absoluteSQLFile.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\PersistenceInterface; | ||
|
||
final class AbsoluteSQLFileRepository implements AbsoluteSQLFileRepositoryInterface | ||
{ | ||
public function __construct( | ||
private readonly PersistenceInterface $persistence, | ||
) | ||
{ | ||
} | ||
|
||
public function findByTitle( | ||
string $title, | ||
): array | ||
{ | ||
$result = $this->persistence->query(Foo::class, 'SELECT id, title | ||
FROM foos | ||
WHERE title = ?', [$title]); | ||
return iterator_to_array($result); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
data-access-kit/test/Repository/Snapshot/CompilerTest.testCompile.relativeSQLFile.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\PersistenceInterface; | ||
|
||
final class RelativeSQLFileRepository implements RelativeSQLFileRepositoryInterface | ||
{ | ||
public function __construct( | ||
private readonly PersistenceInterface $persistence, | ||
) | ||
{ | ||
} | ||
|
||
public function findByTitle( | ||
string $title, | ||
): array | ||
{ | ||
$result = $this->persistence->query(Foo::class, 'SELECT id, title | ||
FROM foos | ||
WHERE title = ?', [$title]); | ||
return iterator_to_array($result); | ||
} | ||
} |