-
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.
Merge pull request #11 from kawanamiyuu/request-mapper
Request Mapping
- Loading branch information
Showing
14 changed files
with
312 additions
and
95 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
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework\Demo; | ||
|
||
use K9u\RequestMapper\Annotation\GetMapping; | ||
use K9u\RequestMapper\PathParams; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class FakeController | ||
{ | ||
/** | ||
* @GetMapping("/") | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function index(): array | ||
{ | ||
return ['path' => '/']; | ||
} | ||
|
||
/** | ||
* @GetMapping("/blogs") | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getBlogs(): array | ||
{ | ||
return ['path' => '/blogs']; | ||
} | ||
|
||
/** | ||
* @GetMapping("/blogs/{id}") | ||
* | ||
* @param ServerRequestInterface $request | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getBlogById(ServerRequestInterface $request): array | ||
{ | ||
$pathParams = $request->getAttribute(PathParams::class); | ||
assert($pathParams instanceof PathParams); | ||
|
||
return ['path' => "/blogs/{$pathParams['id']}"]; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework\Demo; | ||
|
||
use Ray\Aop\MethodInterceptor; | ||
use Ray\Aop\MethodInvocation; | ||
|
||
class FakeInterceptor implements MethodInterceptor | ||
{ | ||
public function invoke(MethodInvocation $invocation) | ||
{ | ||
$result = $invocation->proceed(); | ||
assert(is_array($result)); | ||
|
||
$method = $invocation->getMethod(); | ||
$class = $method->getDeclaringClass(); | ||
|
||
return $result + ['handler' => sprintf('%s::%s', $class->getName(), $method->getName())]; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use K9u\RequestMapper\HandlerClassFactoryInterface; | ||
use Ray\Di\InjectorInterface; | ||
|
||
class HandlerClassFactory implements HandlerClassFactoryInterface | ||
{ | ||
private InjectorInterface $injector; | ||
|
||
public function __construct(InjectorInterface $injector) | ||
{ | ||
$this->injector = $injector; | ||
} | ||
|
||
public function __invoke(string $class): object | ||
{ | ||
return $this->injector->getInstance($class); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use K9u\RequestMapper\HandlerMethodArgumentsResolverInterface; | ||
use K9u\RequestMapper\NamedArguments; | ||
use K9u\RequestMapper\PathParams; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use ReflectionMethod; | ||
use ReflectionNamedType; | ||
|
||
class HandlerMethodArgumentsResolver implements HandlerMethodArgumentsResolverInterface | ||
{ | ||
public function __invoke( | ||
ReflectionMethod $method, | ||
ServerRequestInterface $request, | ||
PathParams $pathParams | ||
): NamedArguments { | ||
|
||
$request = $request->withAttribute(PathParams::class, $pathParams); | ||
|
||
$args = []; | ||
foreach ($method->getParameters() as $param) { | ||
if ($param->hasType()) { | ||
$type = $param->getType(); | ||
assert($type instanceof ReflectionNamedType); | ||
|
||
if (is_a($type->getName(), ServerRequestInterface::class, true)) { | ||
$args[$param->getName()] = $request; | ||
continue; | ||
} | ||
} | ||
|
||
// TODO: assign Path params, Query params, Parsed body, ... | ||
$args[$param->getName()] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; | ||
} | ||
|
||
return new NamedArguments($args); | ||
} | ||
} |
Oops, something went wrong.