public function generate($url_name, $parameters = []) { $routes = Application::Router()->getRoutes(); $translator = Application::Translator(); if (!isset($routes[$url_name])) { return $url_name; } // Ha szükséges be kell rakni a nyelvet is a paraméterek közé if ($translator != null && $translator->getStrategy() != Translator::STRATEGY_ONLY_COOKIE && !$routes[$url_name]["locale"] == LanguageRouter::LOCALE_NOT_ALLOWED) { $parameters["locale"] = $translator->getLocale(); } $url = $routes[$url_name]["url"]; $resultUrl = ""; $index = 0; while (($firstPos = strpos($url, "{", $index)) != false) { $lastPos = strpos($url, "}", $firstPos); $name = explode(":", substr($url, $firstPos + 1, $lastPos - $firstPos - 1))[0]; $resultUrl .= substr($url, $index, $firstPos - $index) . (isset($parameters[$name]) ? $parameters[$name] : ""); $index = $lastPos + 1; } if ($index != strlen($url)) { $resultUrl .= substr($url, $index); } // Ha nincs semmi, akkor nem volt paraméter, másoljuk az egész url-t. if ($resultUrl === "") { $resultUrl = $url; } return $resultUrl; }
/** * @param $method * @param $uri * @return array * @throws HttpNotFoundException */ public function findRoute($method, $uri) { $routes = $this->getRoutes(); //Hozzá kell még adni a nyelvi url-t, ha van. $translator = Application::Translator(); if ($translator != null) { $langRoute = $translator->getCookieSetUrl(); if ($langRoute != null) { $routes["langSetRoute"] = ["method" => $langRoute["method"], "url" => $langRoute["url"], "handler" => $langRoute["handler"]]; } } $this->dispatcher = simpleDispatcher(function (RouteCollector $r) use($routes) { foreach ($routes as $route) { $r->addRoute($route["method"], $route["url"], $route["handler"]); } }); // Strip query string (?foo=bar) and decode URI if (false !== ($pos = strpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $this->dispatcher->dispatch($method, $uri); switch ($routeInfo[0]) { case RouterInterface::NOT_FOUND: throw new HttpNotFoundException(); case RouterInterface::METHOD_NOT_ALLOWED: throw new HttpNotFoundException(); case RouterInterface::FOUND: foreach ($routes as $route) { if ($route["handler"] == $routeInfo[1]) { $this->actualRoute = $route; break; } } return ["handler" => $routeInfo[1], "params" => $routeInfo[2]]; default: return []; } }
/** * Visszaadja a paraméterben megadott adatok alapján, a megfelelő handler-t és az ahhoz tartozó esetleges * paramétereket. * * Ha nem talál egyezést abban az esetben HttpNotFoundException hibát dob. * * * @param $method * @param $uri * @return array * @throws HttpNotFoundException */ public function findRoute($method, $uri) { // Strip query string (?foo=bar) and decode URI if (false !== ($pos = strpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $this->dispatcher->dispatch($method, $uri); switch ($routeInfo[0]) { case RouterInterface::NOT_FOUND: throw new HttpNotFoundException(); case RouterInterface::METHOD_NOT_ALLOWED: throw new HttpNotFoundException(); case RouterInterface::FOUND: $params = $routeInfo[2]; //Ha van locale, akkor azt beállítjuk és töröljük a paraméter listából if (isset($params["locale"])) { Application::Translator()->setLocale($params["locale"]); unset($params["locale"]); } foreach ($this->routes as $route) { if ($route["handler"] == $routeInfo[1]) { $this->actualRoute = $route; break; } } return ["handler" => $routeInfo[1], "params" => $params]; default: return []; } }
/** * Alkalmazás indítása * @param ApplicationConfiguration $configuration * @throws HttpAccessDeniedException */ public function run(ApplicationConfiguration $configuration) { try { if (!$configuration) { throw new HttpInternalErrorException("Missing configuration"); } $configuration->initializeApplication($this); //Csekkolni kell, hogy kell-e erőltetni a https-t if ($this->environment["force_https"] && $_SERVER['HTTPS'] != "on") { $this->redirectHttps(); return; } $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; //Jogosultság ellenőrzése, ha van definiálva security $security = self::Security(); if ($security != null && !$security->checkPermissions($uri)) { throw new HttpAccessDeniedException(); } //Routeból lekérdezni, hogy mit kell futtatni $router = self::Router(); $routingResult = $router->findRoute($httpMethod, $uri); $controllerParts = explode("::", $routingResult["handler"]); $controllerName = $controllerParts[0]; $controllerMethod = $controllerParts[1]; if ($controllerName === "Translator") { $translator = Application::Translator(); $result = $translator->{$controllerMethod}($routingResult["params"]); } else { //Megfelelő controller betöltése if (!$this->loadController($controllerName) && $controllerName != "Translator") { throw new HttpInternalErrorException($controllerName . " has been not found!"); } //Futtatás $controllerFullName = $this->environment["controller_namespace"] . $controllerName; $controller = new $controllerFullName(); $result = $controller->{$controllerMethod}($routingResult["params"]); } //Eredmény printelése print $result; return; } catch (HttpException $e) { $handler = $this->callableErrorHandler; print $handler($e); } catch (\Exception $e) { print "Unhandled exception!\n"; if (Application::isDevelopmentEnv()) { print $e->getMessage() . "\n"; print str_replace("#", "<br>#", $e->getTraceAsString()); } } }