/** * Maps HTTP request to a PresenterRequest object. * @param Nette\Web\IHttpRequest * @return PresenterRequest|NULL */ public function match(IHttpRequest $context) { if (!preg_match('#^/([a-z0-9]{1,3})/(.*?)/?$#', $context->getUri()->path, $matches)) { return NULL; } $lang = $matches[1]; $categories = $matches[2]; $pom = explode("/", $categories); $last = end($pom); if (dibi::fetchSingle("\n\t\t\tSELECT\n\t\t\t COUNT(*)\n\t\t\tFROM\n\t\t\t category\n\t\t\t JOIN category_lang USING(id_category)\n\t\t\t JOIN [lang] USING(id_lang)\n\t\t\tWHERE iso=%s", $lang, "AND link_rewrite = %s", $last)) { $presenter = 'Front:Eshop'; } elseif (dibi::fetchSingle("SELECT COUNT(*) FROM menu_item WHERE lang = %s", $lang, "AND url_identifier=%s", $last)) { $presenter = 'Front:List'; } else { return NULL; } // alternativa: použít jednu tabulku s páry URL -> jméno Presenteru // výhoda: jeden lookup místo (až) tří, neměřitelně vyšší rychlost ;) // nevýhoda: nutnost ji udržovat :-( // alternativa č.2: místo COUNT(*) načíst z DB celý záznam a předat v parametru presenteru // výhoda: stejně jej bude potřebovat // nevýhoda: nadstandardní závislost mezi routerem a presenterem $params = $context->getQuery(); // $params['link_rewrite'] = $last; $params['lang'] = $lang; return new NPresenterRequest($presenter, $context->getMethod(), $params, $context->getPost(), $context->getFiles(), array('secured' => $context->isSecured())); }
function match(IHttpRequest $httpRequest) { $url = $httpRequest->getUrl(); $lang = $this->getDefaultLang(); $langs = Environment::getVariable("langs"); $langDomains = Environment::getVariable("langDomains"); //domains for languages or just directory prefix? if (count($langDomains)) { $path = '//' . $url->getHost() . $url->getPath(); foreach ($langDomains as $lang => $pattern) { $pattern = preg_quote($pattern, '~'); $pattern = preg_replace('~^//(www\\.)?~', '//(www\\.)?', $pattern); //www not mandatory if (preg_match("~^{$pattern}~", $path, $m)) { //matching absolute path $seoname = substr($path, strlen($m[0])); //what follows break; } } } else { //just language prefixes $path = $url->pathInfo; foreach ($langs as $lang => $txt) { if (preg_match("~^{$lang}/~", $path, $m)) { //matching relative path $seoname = substr($path, strlen($m[0])); //what follows break; } } } if (!isset($seoname)) { //default language possible without prefix $keys = array_keys($langs); $lang = array_shift($keys); $seoname = $url->pathInfo; } if (substr($seoname, -1) == '/') { $seoname = substr($seoname, 0, -1); } $page = PagesModel::getPageBySeoname('/' . $seoname, $lang); //just one level if (!$page) { if (preg_match('~^p([0-9]+)(-|$)~', $seoname, $matches)) { $page = PagesModel::getPageById($matches[1], $lang); if (!$page) { return NULL; } } else { return NULL; } } $params = array(); $params += $httpRequest->getQuery(); $params['id_page'] = $page['id_page']; $params['lang'] = $lang; return new PresenterRequest(self::PRESENTER, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured())); }
/** * Maps HTTP request to a PresenterRequest object. * @param IHttpRequest * @return PresenterRequest|NULL */ public function match(IHttpRequest $httpRequest) { // combine with precedence: get, (post,) defaults $params = $httpRequest->getQuery(); $params += $this->defaults; if (!isset($params[self::PRESENTER_KEY])) { throw new InvalidStateException('Missing presenter.'); } $presenter = $this->module . $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); return new PresenterRequest($presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured())); }
/** * Maps HTTP request to a Request object. * @param IHttpRequest * @return NPresenterRequest|NULL */ public function match(IHttpRequest $httpRequest) { // combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults // 1) URL MASK $url = $httpRequest->getUrl(); if ($this->type === self::HOST) { $path = '//' . $url->getHost() . $url->getPath(); } elseif ($this->type === self::RELATIVE) { $basePath = $url->getBasePath(); if (strncmp($url->getPath(), $basePath, strlen($basePath)) !== 0) { return NULL; } $path = (string) substr($url->getPath(), strlen($basePath)); } else { $path = $url->getPath(); } if ($path !== '') { $path = rtrim($path, '/') . '/'; } if (!$matches = NStrings::match($path, $this->re)) { // stop, not matched return NULL; } // deletes numeric keys, restore '-' chars $params = array(); foreach ($matches as $k => $v) { if (is_string($k) && $v !== '') { $params[str_replace('___', '-', $k)] = $v; // trick } } // 2) CONSTANT FIXITY foreach ($this->metadata as $name => $meta) { if (isset($params[$name])) { //$params[$name] = $this->flags & self::CASE_SENSITIVE === 0 ? strtolower($params[$name]) : */$params[$name]; // strtolower damages UTF-8 } elseif (isset($meta['fixity']) && $meta['fixity'] !== self::OPTIONAL) { $params[$name] = NULL; // cannot be overwriten in 3) and detected by isset() in 4) } } // 3) QUERY if ($this->xlat) { $params += self::renameKeys($httpRequest->getQuery(), array_flip($this->xlat)); } else { $params += $httpRequest->getQuery(); } // 4) APPLY FILTERS & FIXITY foreach ($this->metadata as $name => $meta) { if (isset($params[$name])) { if (!is_scalar($params[$name])) { } elseif (isset($meta[self::FILTER_TABLE][$params[$name]])) { // applies filterTable only to scalar parameters $params[$name] = $meta[self::FILTER_TABLE][$params[$name]]; } elseif (isset($meta[self::FILTER_TABLE]) && !empty($meta[self::FILTER_STRICT])) { return NULL; // rejected by filterTable } elseif (isset($meta[self::FILTER_IN])) { // applies filterIn only to scalar parameters $params[$name] = call_user_func($meta[self::FILTER_IN], (string) $params[$name]); if ($params[$name] === NULL && !isset($meta['fixity'])) { return NULL; // rejected by filter } } } elseif (isset($meta['fixity'])) { $params[$name] = $meta[self::VALUE]; } } // 5) BUILD Request if (!isset($params[self::PRESENTER_KEY])) { throw new InvalidStateException('Missing presenter in route definition.'); } if (isset($this->metadata[self::MODULE_KEY])) { if (!isset($params[self::MODULE_KEY])) { throw new InvalidStateException('Missing module in route definition.'); } $presenter = $params[self::MODULE_KEY] . ':' . $params[self::PRESENTER_KEY]; unset($params[self::MODULE_KEY], $params[self::PRESENTER_KEY]); } else { $presenter = $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); } return new NPresenterRequest( $presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(NPresenterRequest::SECURED => $httpRequest->isSecured()) ); }
function match(IHttpRequest $httpRequest) { if ($httpRequest->getUri()->getPathInfo() !== '') { return NULL; } $params = $httpRequest->getQuery(); $params += $this->defaults; if (!isset($params[self::PRESENTER_KEY])) { throw new InvalidStateException('Missing presenter.'); } $presenter = $this->module . $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); return new NPresenterRequest($presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(NPresenterRequest::SECURED => $httpRequest->isSecured())); }
/** * Maps HTTP request to a Request object. * @return NPresenterRequest|NULL */ public function match(IHttpRequest $httpRequest) { if ($httpRequest->getUrl()->getPathInfo() !== '') { return NULL; } // combine with precedence: get, (post,) defaults $params = $httpRequest->getQuery(); $params += $this->defaults; if (!isset($params[self::PRESENTER_KEY]) || !is_string($params[self::PRESENTER_KEY])) { return NULL; } $presenter = $this->module . $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); return new NPresenterRequest( $presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(NPresenterRequest::SECURED => $httpRequest->isSecured()) ); }