Skip to content

Commit

Permalink
SQLFileMethodCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkulhan committed May 24, 2024
1 parent 47e7481 commit 916d685
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data-access-kit/src/Repository/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use DataAccessKit\Repository\Attribute\Find;
use DataAccessKit\Repository\Attribute\Repository;
use DataAccessKit\Repository\Attribute\SQL;
use DataAccessKit\Repository\Attribute\SQLFile;
use DataAccessKit\Repository\Method\CountMethodCompiler;
use DataAccessKit\Repository\Method\DelegateMethodCompiler;
use DataAccessKit\Repository\Method\FindMethodCompiler;
use DataAccessKit\Repository\Method\SQLFileMethodCompiler;
use DataAccessKit\Repository\Method\SQLMethodCompiler;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -46,6 +48,7 @@ public function __construct(
$this->registerMethodCompiler(SQL::class, $sqlMethodCompiler = new SQLMethodCompiler());
$this->registerMethodCompiler(Find::class, new FindMethodCompiler($registry, $sqlMethodCompiler));
$this->registerMethodCompiler(Count::class, new CountMethodCompiler($registry, $sqlMethodCompiler));
$this->registerMethodCompiler(SQLFile::class, new SQLFileMethodCompiler($sqlMethodCompiler));
$this->registerMethodCompiler(Delegate::class, new DelegateMethodCompiler());
}

Expand Down
57 changes: 57 additions & 0 deletions data-access-kit/src/Repository/Method/SQLFileMethodCompiler.php
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));
}
}
4 changes: 4 additions & 0 deletions data-access-kit/test/Repository/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use DataAccessKit\Repository\Fixture\NullableGetRepositoryInterface;
use DataAccessKit\Repository\Fixture\PassClassAttributesRepositoryInterface;
use DataAccessKit\Repository\Fixture\PassMethodAttributesRepositoryInterface;
use DataAccessKit\Repository\Fixture\RelativeSQLFileRepositoryInterface;
use DataAccessKit\Repository\Fixture\SimpleSQLArrayRepositoryInterface;
use DataAccessKit\Repository\Fixture\SimpleSQLIterableRepositoryInterface;
use DataAccessKit\Repository\Fixture\SimpleSQLNullableObjectRepositoryInterface;
use DataAccessKit\Repository\Fixture\SimpleSQLObjectRepositoryInterface;
use DataAccessKit\Repository\Fixture\AbsoluteSQLFileRepositoryInterface;
use DataAccessKit\Repository\Fixture\VariableSQLRepositoryInterface;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -80,6 +82,8 @@ public static function provideCompile()
SimpleSQLObjectRepositoryInterface::class,
SimpleSQLNullableObjectRepositoryInterface::class,
VariableSQLRepositoryInterface::class,
AbsoluteSQLFileRepositoryInterface::class,
RelativeSQLFileRepositoryInterface::class,
DelegateToClassRepositoryInterface::class,
DelegateToInterfaceRepositoryInterface::class,
DelegateToTraitRepositoryInterface::class,
Expand Down
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;
}
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT id, title
FROM foos
WHERE title = @title
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);
}
}
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);
}
}

0 comments on commit 916d685

Please sign in to comment.