-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ray-di/annotation-query
Rename @AliasQuery to @query annotation
- Loading branch information
Showing
11 changed files
with
140 additions
and
12 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
File renamed without changes.
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
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
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
* | ||
* @Annotation | ||
* @Target("METHOD") | ||
* | ||
* @deprecated use MapQuery instead | ||
*/ | ||
final class AliasQuery | ||
{ | ||
|
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
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,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'; | ||
} |
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,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]; | ||
} | ||
} |
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
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
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