Skip to content

Commit

Permalink
feat: suppport callAction while dispathcing controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcht committed Nov 12, 2024
1 parent 2d2630c commit 4a3c961
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/http/src/CoreMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace SwooleTW\Hyperf\Http;

use Closure;
use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\CoreMiddleware as HyperfCoreMiddleware;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\View\RenderInterface;
use Hyperf\ViewEngine\Contract\ViewInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SwooleTW\Hyperf\HttpMessage\Exceptions\ServerErrorHttpException;
use Swow\Psr7\Message\ResponsePlusInterface;

class CoreMiddleware extends HyperfCoreMiddleware
Expand All @@ -31,4 +34,32 @@ protected function transferToResponse($response, ServerRequestInterface $request

return parent::transferToResponse($response, $request);
}

/**
* Handle the response when found.
*
* @return array|Arrayable|mixed|ResponseInterface|string
*/
protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request): mixed
{
if ($dispatched->handler->callback instanceof Closure) {
$parameters = $this->parseClosureParameters($dispatched->handler->callback, $dispatched->params);
$callback = $dispatched->handler->callback;

return $callback(...$parameters);
}

[$controller, $action] = $this->prepareHandler($dispatched->handler->callback);
$controllerInstance = $this->container->get($controller);
if (! method_exists($controllerInstance, $action)) {
throw new ServerErrorHttpException("{$controller}@{$action} does not exist.");
}

$parameters = $this->parseMethodParameters($controller, $action, $dispatched->params);
if (method_exists($controllerInstance, 'callAction')) {
return $controllerInstance->callAction($action, $parameters);
}

return $controllerInstance->{$action}(...$parameters);
}
}

0 comments on commit 4a3c961

Please sign in to comment.