Skip to content

Commit

Permalink
Handle Request
Browse files Browse the repository at this point in the history
  • Loading branch information
thalysmarciobn committed May 19, 2024
1 parent 940f242 commit b62e65b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Aurora/Middleware/MiddlewareDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$closure();
}

return $finalHandler;
return $finalHandler->handle($request);
}
}
39 changes: 22 additions & 17 deletions src/Aurora/Request/RouterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace AuroraLumina\Request;

use Closure;
use stdClass;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;
use ReflectionParameter;
use AuroraLumina\Container;
use AuroraLumina\Routing\Route;
use Psr\Http\Message\RequestInterface;
use AuroraLumina\Request\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use AuroraLumina\Http\Response\Response;
use AuroraLumina\Interface\ServiceInterface;
use AuroraLumina\Http\Response\EmptyResponse;
use AuroraLumina\Interface\ControllerInterface;
use AuroraLumina\Interface\RouterRequestInterface;
use AuroraLumina\Request\ServerRequest;
use Psr\Http\Message\RequestInterface;

class RouterRequest implements RouterRequestInterface
{
Expand Down Expand Up @@ -68,11 +72,11 @@ protected function resolveConstructorDependencies(array $params): array
/**
* Resolve a single constructor parameter dependency.
*
* @param \ReflectionParameter $param The parameter to resolve.
* @param ReflectionParameter $param The parameter to resolve.
* @return ServiceInterface The resolved dependency.
* @throws \RuntimeException If the dependency cannot be resolved.
* @throws RuntimeException If the dependency cannot be resolved.
*/
protected function resolveDependency(\ReflectionParameter $param): ServiceInterface
protected function resolveDependency(ReflectionParameter $param): ServiceInterface
{
$name = $param->getType()->getName();

Expand Down Expand Up @@ -125,18 +129,18 @@ protected function instantiateController(string $class): mixed
*
* @param mixed $controller The controller instance to validate.
*
* @throws \RuntimeException If the controller could not be instantiated or if it does not implement ControllerInterface.
* @throws RuntimeException If the controller could not be instantiated or if it does not implement ControllerInterface.
*/
protected function validateController(mixed $controller): void
{
if (!$controller)
{
throw new \RuntimeException("Controller could not be instantiated.");
throw new RuntimeException("Controller could not be instantiated.");
}

if (!$controller instanceof ControllerInterface)
{
throw new \RuntimeException("Invalid controller. Expected instance of ControllerInterface.");
throw new RuntimeException("Invalid controller. Expected instance of ControllerInterface.");
}
}

Expand All @@ -153,7 +157,7 @@ protected function validateMethod(ControllerInterface $controller, string $metho
$reflectionMethod = $this->getReflectionMethod($controller, $method);

if (!$reflectionMethod->isPublic()) {
throw new \RuntimeException("Method in controller class is not public.");
throw new RuntimeException("Method in controller class is not public.");
}
}

Expand All @@ -167,13 +171,13 @@ protected function validateMethod(ControllerInterface $controller, string $metho
*
* @throws \RuntimeException If the method is not found in the controller class.
*/
private function getReflectionMethod(ControllerInterface $controller, string $method): \ReflectionMethod
private function getReflectionMethod(ControllerInterface $controller, string $method): ReflectionMethod
{
if (!method_exists($controller, $method)) {
throw new \RuntimeException("Method not found in controller class.");
throw new RuntimeException("Method not found in controller class.");
}

return new \ReflectionMethod($controller, $method);
return new ReflectionMethod($controller, $method);
}

/**
Expand All @@ -196,12 +200,13 @@ private function callControllerMethod($controller, string $method, ServerRequest
* @param array $parameters The router parameters.
* @return callable The middleware callback.
*/
protected function buildCallback(mixed $action, array $parameters): callable
protected function buildCallback(mixed $action): callable
{
return function (ServerRequest $request) use ($action, $parameters) {
if ($action instanceof \Closure)
return function (ServerRequest $request, array $parameters) use ($action)
{
if ($action instanceof Closure)
{
return $action($request);
return $action($request, $parameters);
}

if (is_array($action))
Expand Down Expand Up @@ -423,7 +428,7 @@ public function handle(RequestInterface $request): ResponseInterface
{
$route = $result->route;

$callback = $this->buildCallback($route->getAction(), $route->getParameters())($request);
$callback = $this->buildCallback($route->getAction())($request, $route->getParameters());

if (is_string($callback))
{
Expand Down
10 changes: 10 additions & 0 deletions src/Aurora/Request/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ public function getPostParams(): array
return $this->postParams;
}

/**
* Get post parameter
*
* @return ?string
*/
public function getPost(string $param): ?string
{
return isset($this->postParams[$param]) ? $this->postParams[$param] : null;
}

/**
* Return a new instance with the specified query parameters
*
Expand Down

0 comments on commit b62e65b

Please sign in to comment.