. */ namespace SP\Modules\Web; 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\Core\Bootstrap\BootstrapInterface; use SP\Domain\Core\Bootstrap\ModuleInterface; use SP\Domain\Core\Exceptions\SessionTimeout; use SP\Domain\Http\Code; use function SP\__; 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:web'); try { $bootstrap->module = $initModule; $bootstrap->handleRequest(); } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { processException($e); die($e->getMessage()); } } protected function configureRouter(): void { $this->router->respond(['GET', 'POST'], '@(?!/api\.php)', $this->manageWebRequest()); } private function manageWebRequest(): Closure { return function (Request $request, Response $response) { try { logger('WEB route'); $controllerClass = self::getClassFor( $this->routeContextData->getController(), $this->routeContextData->getActionName() ); if (!method_exists($controllerClass, $this->routeContextData->getMethodName())) { logger($controllerClass . '::' . $this->routeContextData->getMethodName()); $response->code(Code::NOT_FOUND->value); $response->append(self::OOPS_MESSAGE); return $response; } $this->context->setTrasientKey(self::CONTEXT_ACTION_NAME, $this->routeContextData->getActionName()); $this->setCors($response); $this->initializeCommon(); $this->module->initialize($controllerClass); logger( sprintf( 'Routing call: %s::%s::%s', $controllerClass, $this->routeContextData->getMethodName(), print_r($this->routeContextData->getMethodParams(), true) ) ); return call_user_func_array( [$this->buildInstanceFor($controllerClass), $this->routeContextData->getMethodName()], $this->routeContextData->getMethodParams() ); } catch (SessionTimeout) { logger('Session timeout'); } catch (Exception $e) { processException($e); echo __($e->getMessage()); if (DEBUG) { echo $e->getTraceAsString(); } $response->code(Code::INTERNAL_SERVER_ERROR->value); $response->append($e->getMessage()); } return $response; }; } }