. */ namespace SP\Modules\Api; use Closure; use Exception; use Klein\Request; use Klein\Response; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Core\Bootstrap\BootstrapBase; use SP\Domain\Api\Ports\ApiRequestService; use SP\Domain\Api\Services\JsonRpcResponse; use SP\Domain\Core\Bootstrap\BootstrapInterface; use SP\Domain\Core\Bootstrap\ModuleInterface; use SP\Domain\Http\Code; use function SP\logger; use function SP\processException; /** * Class Bootstrap */ final class Bootstrap extends BootstrapBase { protected ModuleInterface $module; public static function run(BootstrapInterface $bootstrap, ModuleInterface $initModule): void { logger('------------'); logger('Boostrap:api'); try { $bootstrap->module = $initModule; $bootstrap->handleRequest(); } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { processException($e); die($e->getMessage()); } } protected function configureRouter(): void { $this->router->respond('POST', '@/api\.php', $this->manageApiRequest()); } private function manageApiRequest(): Closure { return function (Request $request, Response $response) { try { logger('API route'); $response->headers()->set('Content-type', 'application/json; charset=utf-8'); $apiRequest = $this->buildInstanceFor(ApiRequestService::class); [$controllerName, $actionName] = explode('/', $apiRequest->getMethod()); $controllerClass = self::getClassFor($this->module->getName(), $controllerName, $actionName); $method = $actionName . 'Action'; if (!method_exists($controllerClass, $method)) { logger($controllerClass . '::' . $method); $response->code(Code::NOT_FOUND->value); return $response->body( JsonRpcResponse::getResponseError( self::OOPS_MESSAGE, JsonRpcResponse::METHOD_NOT_FOUND, $apiRequest->getId() ) ); } $this->context->setTrasientKey(self::CONTEXT_ACTION_NAME, $actionName); $this->initializeCommon(); $this->module->initialize($controllerName); logger('Routing call: ' . $controllerClass . '::' . $method); return call_user_func([$this->buildInstanceFor($controllerClass), $method]); } catch (Exception $e) { processException($e); $response->code(Code::INTERNAL_SERVER_ERROR->value); return $response->body(JsonRpcResponse::getResponseException($e, 0)); } finally { $this->router->skipRemaining(); } }; } }