Skip to content

Commit

Permalink
Merge pull request #13 from ray-di/annotation-query
Browse files Browse the repository at this point in the history
Rename @AliasQuery to @query annotation
  • Loading branch information
koriym authored Jun 24, 2019
2 parents 9b973ad + 29bcb6d commit de1b138
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"autoload": {
"psr-4": {
"Ray\\Query\\": "src/"
"Ray\\Query\\": ["src/", "src-deprecated"]
}
},
"autoload-dev": {
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions demo/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace Ray\Query;

use Ray\Query\Annotation\AliasQuery;
use Ray\Query\Annotation\Query;

class Todo
{
/**
* @AliasQuery("todo_item_by_id")
* @Query("todo_item_by_id")
*/
public function get(string $id)
{
}

/**
* @AliasQuery(id="todo_insert?id={uuid}", templated=true)
* @Query(id="todo_insert?id={uuid}", templated=true)
*/
public function create(string $uuid, string $title)
{
Expand Down
2 changes: 1 addition & 1 deletion demo/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

passthru('php ' . __DIR__ . '/0-manual-injection.php');
passthru('php ' . __DIR__ . '/1-constructor-injection.php');
passthru('php ' . __DIR__ . '/2-alias-query.php');
passthru('php ' . __DIR__ . '/2-query-annotation.php');
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*
* @Annotation
* @Target("METHOD")
*
* @deprecated use MapQuery instead
*/
final class AliasQuery
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Ray\Di\InjectorInterface;
use Ray\Query\Annotation\AliasQuery;

/**
* @deprecated use MapQueryInterceptor instead
*/
class SqlAliasInterceptor implements MethodInterceptor
{
/**
Expand Down
35 changes: 35 additions & 0 deletions src/Annotation/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Ray\Query\Annotation;

/**
* Annotates your class methods into which the Injector should inject values
*
* @Annotation
* @Target("METHOD")
*/
final class Query
{
/**
* Query ID
*
* @var string
*/
public $id;

/**
* Is ID templated ?
*
* @var bool
*/
public $templated = false;

/**
* @Enum({"row", "row_list"})
*
* @var string
*/
public $type = 'row_list';
}
83 changes: 83 additions & 0 deletions src/QueryInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Ray\Query;

use BEAR\Resource\ResourceObject;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Ray\Aop\ReflectionMethod;
use Ray\Di\InjectorInterface;
use Ray\Query\Annotation\Query;

class QueryInterceptor implements MethodInterceptor
{
/**
* @var InjectorInterface
*/
private $injector;

public function __construct(InjectorInterface $injector)
{
$this->injector = $injector;
}

public function invoke(MethodInvocation $invocation)
{
/** @var ReflectionMethod $method */
$method = $invocation->getMethod();
/** @var Query $query */
$query = $method->getAnnotation(Query::class);
$namedArguments = (array) $invocation->getNamedArguments();
list($queryId, $params) = $query->templated ? $this->templated($query, $namedArguments) : [$query->id, $namedArguments];
$interface = $query->type === 'row' ? RowInterface::class : RowListInterface::class;
$query = $this->injector->getInstance($interface, $queryId);
if ($query instanceof QueryInterface) {
return $this->getQueryResult($invocation, $query, $params);
}

return $invocation->proceed();
}

private function getQueryResult(MethodInvocation $invocation, QueryInterface $query, array $param)
{
$result = $query($param);
$object = $invocation->getThis();
if ($object instanceof ResourceObject) {
return $this->returnRo($object, $invocation, $result);
}

return $result;
}

private function returnRo(ResourceObject $ro, MethodInvocation $invocation, $result) : ResourceObject
{
if (! $result) {
return $this->return404($ro);
}
$ro->body = $result;

return $invocation->proceed();
}

private function return404(ResourceObject $ro) : ResourceObject
{
$ro->code = 404;
$ro->body = [];

return $ro;
}

private function templated(Query $query, array $namedArguments) : array
{
$url = parse_url(uri_template($query->id, $namedArguments));
if (! $url) {
throw new \InvalidArgumentException($query->id);
}
$queryId = $url['path'];
isset($url['query']) ? parse_str($url['query'], $params) : $params = $namedArguments;

return [$queryId, $params + $namedArguments];
}
}
11 changes: 8 additions & 3 deletions src/SqlQueryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Ray\Query;

use Ray\Di\AbstractModule;
use Ray\Query\Annotation\AliasQuery;
use Ray\Query\Annotation\Query;

class SqlQueryModule extends AbstractModule
{
Expand Down Expand Up @@ -48,8 +48,13 @@ protected function configure()
}
$this->bindInterceptor(
$this->matcher->any(),
$this->matcher->annotatedWith(AliasQuery::class),
[SqlAliasInterceptor::class]
$this->matcher->annotatedWith(Query::class),
[QueryInterceptor::class]
);
$this->bindInterceptor(
$this->matcher->any(),
$this->matcher->annotatedWith(Query::class),
[QueryInterceptor::class]
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/FakeAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
*/
namespace Ray\Query;

use Ray\Query\Annotation\AliasQuery;
use Ray\Query\Annotation\Query;

class FakeAlias
{
/**
* @AliasQuery("todo_item_by_id", type="row")
* @Query("todo_item_by_id", type="row")
*/
public function get(string $id)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/FakeAliasNamed.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
*/
namespace Ray\Query;

use Ray\Query\Annotation\AliasQuery;
use Ray\Query\Annotation\Query;

class FakeAliasNamed
{
/**
* @AliasQuery(id="todo_item_by_id?id={a}", templated=true, type="row")
* @Query(id="todo_item_by_id?id={a}", templated=true, type="row")
*/
public function get(string $a)
{
Expand Down

0 comments on commit de1b138

Please sign in to comment.