Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind SQL invokable object to the provider #30

Merged
merged 23 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '42 15 * * *'

jobs:
phpunit:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
tools: cs2pr
coverage: none

Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
tools: cs2pr
coverage: none

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
"bear/resource": "^1.15",
"doctrine/annotations": "^1.12",
"guzzlehttp/guzzle": "^6.3 || ^7.0",
"koriym/param-reader": "^1.0",
"koriym/query-locator": "^1.4",
"nikic/php-parser": "^v4.13",
"ray/aop": "^2.10.3",
"ray/aura-sql-module": "^1.10.0",
"ray/di": "^2.11",
"nikic/php-parser": "^v4.13"
"ray/di": "^2.11"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
25 changes: 25 additions & 0 deletions src/Annotation/Sql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Ray\Query\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

/**
* @Annotation
* @Target("PROPERTY")
* @NamedArgumentConstructor()
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
final class Sql
{
/** @var string */
public $sql;

public function __construct(string $sql)
{
$this->sql = $sql;
}
}
11 changes: 11 additions & 0 deletions src/Exception/SqlFileNotFoundException.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 LogicException;

class SqlFileNotFoundException extends LogicException
{
}
11 changes: 11 additions & 0 deletions src/Exception/SqlNotAnnotatedException.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 LogicException;

class SqlNotAnnotatedException extends LogicException
{
}
15 changes: 15 additions & 0 deletions src/InvokeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

interface InvokeInterface extends QueryInterface
{
/**
* @param array<string, mixed> ...$query
*
* @return iterable<mixed>
*/
public function __invoke(array ...$query): iterable;
}
36 changes: 36 additions & 0 deletions src/RowInterfaceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Aura\Sql\ExtendedPdoInterface;
use Ray\Di\InjectionPointInterface;
use Ray\Di\ProviderInterface;

final class RowInterfaceProvider implements ProviderInterface
{
/** @var InjectionPointInterface */
private $ip;

/** @var ExtendedPdoInterface */
private $pdo;

/** @var SqlFinder */
private $finder;

public function __construct(
InjectionPointInterface $ip,
ExtendedPdoInterface $pdo,
SqlFinder $finder
) {
$this->ip = $ip;
$this->pdo = $pdo;
$this->finder = $finder;
}

public function get(): SqlQueryRow
{
return new SqlQueryRow($this->pdo, ($this->finder)($this->ip->getParameter()));
}
}
2 changes: 1 addition & 1 deletion src/RowListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ray\Query;

interface RowListInterface extends QueryInterface
interface RowListInterface extends InvokeInterface
{
/**
* @param array<string, mixed> ...$query
Expand Down
36 changes: 36 additions & 0 deletions src/RowListInterfaceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Aura\Sql\ExtendedPdoInterface;
use Ray\Di\InjectionPointInterface;
use Ray\Di\ProviderInterface;

final class RowListInterfaceProvider implements ProviderInterface
{
/** @var InjectionPointInterface */
private $ip;

/** @var ExtendedPdoInterface */
private $pdo;

/** @var SqlFinder */
private $finder;

public function __construct(
InjectionPointInterface $ip,
ExtendedPdoInterface $pdo,
SqlFinder $finder
) {
$this->ip = $ip;
$this->pdo = $pdo;
$this->finder = $finder;
}

public function get(): SqlQueryRowList
{
return new SqlQueryRowList($this->pdo, ($this->finder)($this->ip->getParameter()));
}
}
16 changes: 16 additions & 0 deletions src/SqlDir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

final class SqlDir
{
/** @var string */
public $value;

public function __construct(string $value)
{
$this->value = $value;
}
}
53 changes: 53 additions & 0 deletions src/SqlFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Koriym\ParamReader\ParamReaderInterface;
use Ray\Query\Annotation\Sql;
use Ray\Query\Exception\SqlFileNotFoundException;
use Ray\Query\Exception\SqlNotAnnotatedException;
use ReflectionParameter;

use function file_exists;
use function file_get_contents;
use function sprintf;

final class SqlFinder
{
/** @var ParamReaderInterface<object> */
private $reader;

/** @var SqlDir */
private $sqlDir;

/**
* @param ParamReaderInterface<object> $reader
*/
public function __construct(
ParamReaderInterface $reader,
SqlDir $sqlDir
) {
$this->reader = $reader;
$this->sqlDir = $sqlDir;
}

public function __invoke(ReflectionParameter $param): string
{
/** @var ?Sql $sqlAnnotation */
$sqlAnnotation = $this->reader->getParametrAnnotation($param, Sql::class);
koriym marked this conversation as resolved.
Show resolved Hide resolved
if ($sqlAnnotation === null) {
throw new SqlNotAnnotatedException((string) $param);
}

$file = sprintf('%s/%s.sql', $this->sqlDir->value, $sqlAnnotation->sql);
if (! file_exists($file)) {
$msg = sprintf('%s:%s', (string) $param, $file);

throw new SqlFileNotFoundException($msg);
}

return (string) file_get_contents($file);
}
}
1 change: 1 addition & 0 deletions src/SqlQueryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(string $sqlDir, ?AbstractModule $module = null, ?cal
*/
protected function configure()
{
$this->bind(SqlDir::class)->toInstance(new SqlDir($this->sqlDir));
/** @var SplFileInfo $fileInfo */
foreach ($this->files($this->sqlDir) as $fileInfo) {
$name = pathinfo((string) $fileInfo->getRealPath())['filename'];
Expand Down
30 changes: 30 additions & 0 deletions src/SqlQueryProviderModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Koriym\ParamReader\ParamReader;
use Koriym\ParamReader\ParamReaderInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;

class SqlQueryProviderModule extends AbstractModule
{
public function __construct(?AbstractModule $module = null)
{
parent::__construct($module);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->bind(SqlFinder::class)->in(Scope::SINGLETON);
$this->bind(ParamReaderInterface::class)->to(ParamReader::class)->in(Scope::SINGLETON);
$this->bind(RowInterface::class)->toProvider(RowInterfaceProvider::class)->in(Scope::class);
$this->bind(RowListInterface::class)->toProvider(RowListInterfaceProvider::class)->in(Scope::class);
$this->bind(InvokeInterface::class)->toProvider(RowListInterfaceProvider::class)->in(Scope::class);
koriym marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 1 addition & 2 deletions src/SqlQueryRowList.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public function __invoke(array ...$queries): iterable
}

$lastQuery = $result
? strtolower(trim((string) $result->queryString, "\\ \t\n\r\0\x0B"))
: '';
? strtolower(trim((string) $result->queryString, "\\ \t\n\r\0\x0B")) : '';
if ($result instanceof PDOStatement && strpos($lastQuery, 'select') === 0) {
return (array) $result->fetchAll(PDO::FETCH_ASSOC);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Fake/FakeTodoProviderSqlNotAnnotated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

class FakeTodoProviderSqlNotAnnotated
{
public $todoCreate;

public function __construct(
InvokeInterface $todoCreate
){
$this->todoCreate = $todoCreate;
}
}
21 changes: 21 additions & 0 deletions tests/Fake/FakeTodoProviderSqlNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Ray\Query\Annotation\Sql;

class FakeTodoProviderSqlNotFound
{
/**
* @Sql("__invalid")
*/
public $todoCreate;

public function __construct(
InvokeInterface $todoCreate
){
$this->todoCreate = $todoCreate;
}
}
35 changes: 35 additions & 0 deletions tests/Fake/FakeTodoRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use Ray\Query\Annotation\Sql;

class FakeTodoRepository
{
/**
* @Sql("todo_insert")
*/
public $todoCreate;

/**
* @Sql("todo_item_by_id")
*/
public $todoItem;

/**
* @Sql("todo_list")
*/
public $todoList;

public function __construct(
InvokeInterface $todoCreate,
RowInterface $todoItem,
RowListInterface $todoList
){
$this->todoCreate = $todoCreate;
$this->todoItem = $todoItem;
$this->todoList = $todoList;
}
}
Loading