From c0dc531ebefd8912819f3b6c8bda1fed3c7e750c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 Jan 2024 06:11:00 +0300 Subject: make it simple, but not simpler --- engine/RequestDispatcher.php | 84 -------------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 engine/RequestDispatcher.php (limited to 'engine/RequestDispatcher.php') diff --git a/engine/RequestDispatcher.php b/engine/RequestDispatcher.php deleted file mode 100644 index adb61c9..0000000 --- a/engine/RequestDispatcher.php +++ /dev/null @@ -1,84 +0,0 @@ -router->find(self::path()); - if ($route === null) - throw new NotFoundException('Route not found'); - - $route = preg_split('/ +/', $route); - $handler_class = $route[0]; - if (($pos = strrpos($handler_class, '/')) !== false) { - $class_name = substr($handler_class, $pos+1); - $class_name = ucfirst(to_camel_case($class_name)); - $handler_class = str_replace('/', '\\', substr($handler_class, 0, $pos)).'\\'.$class_name; - } else { - $handler_class = ucfirst(to_camel_case($handler_class)); - } - $handler_class = 'handler\\'.$handler_class; - - if (!class_exists($handler_class)) - throw new NotFoundException($config['is_dev'] ? 'Handler class "'.$handler_class.'" not found' : ''); - - $router_input = []; - if (count($route) > 1) { - for ($i = 1; $i < count($route); $i++) { - $var = $route[$i]; - list($k, $v) = explode('=', $var); - $router_input[trim($k)] = trim($v); - } - } - - $skin = new Skin(); - $skin->static[] = 'css/common.css'; - $skin->static[] = 'js/common.js'; - - $lang = LangData::getInstance(); - $skin->addLangKeys($lang->search('/^theme_/')); - - /** @var RequestHandler $handler */ - $handler = new $handler_class($skin, $lang, $router_input); - $resp = $handler->beforeDispatch(); - if ($resp instanceof Response) { - $resp->send(); - return; - } - - $resp = call_user_func([$handler, strtolower($_SERVER['REQUEST_METHOD'])]); - } catch (NotFoundException $e) { - $resp = $this->getErrorResponse($e, 'not_found'); - } catch (ForbiddenException $e) { - $resp = $this->getErrorResponse($e, 'forbidden'); - } catch (NotImplementedException $e) { - $resp = $this->getErrorResponse($e, 'not_implemented'); - } catch (UnauthorizedException $e) { - $resp = $this->getErrorResponse($e, 'unauthorized'); - } - $resp->send(); - } - - protected function getErrorResponse(Exception $e, string $render_function): Response { - $ctx = new SkinContext('\\skin\\error'); - $html = call_user_func([$ctx, $render_function], $e->getMessage()); - return new Response($e->getCode(), $html); - } - - public static function path(): string { - $uri = $_SERVER['REQUEST_URI']; - if (($pos = strpos($uri, '?')) !== false) - $uri = substr($uri, 0, $pos); - return $uri; - } - -} -- cgit v1.2.3