/** * Match with Request * * - merge with current params * * - manipulate params on match * exp. when match host it contain host param * with matched value * * @param iHttpRequest $request * * @return iHRouter|false */ function match(iHttpRequest $request) { $uri = $request->getUri(); $scheme = $uri->getScheme(); if ($scheme !== $this->inOptions()->getScheme()) { return false; } $routeMatch = clone $this; return $routeMatch; }
protected function __match(iHttpRequest $request, $criteria, array $regexDef) { $path = $request->getUri()->getPath(); /*if ($this->_translationKeys) { if (!isset($options['translator']) || !$options['translator'] instanceof Translator) { throw new \RuntimeException('No translator provided'); } $translator = $options['translator']; $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default'); $locale = (isset($options['locale']) ? $options['locale'] : null); foreach ($this->_translationKeys as $key) { $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex); } }*/ # match criteria: $parts = $this->__parseRouteDefinition($criteria); $regex = $this->__buildRegex($parts, $regexDef); ## hash meta for router segment, unique for each file call /*$backTrace = debug_backtrace(null, 1); $hashMeta = end($backTrace)['file'];*/ $hashMeta = 'ds'; $pathOffset = $this->inOptions()->getPathOffset(); $routerSegment = $request->meta()->__router_segment__; if ($routerSegment) { $routerSegment = isset($routerSegment[$hashMeta]) ? $routerSegment = $routerSegment[$hashMeta] : null; } if (!$pathOffset && $routerSegment) { $pathOffset = $routerSegment; $pathOffset = [end($pathOffset), null]; ### offset from last match to end(null), used on split } if ($pathOffset !== null) { ## extract path offset to match $path = call_user_func_array([$path, 'split'], $pathOffset); } $regex = $this->inOptions()->getExactMatch() ? "(^{$regex}\$)" : "(^{$regex})"; ## only start with criteria "/pages[/other/paths]" $result = preg_match($regex, $path->toString(), $matches); if ($result) { ## calculate matched path offset $curMatchDepth = (new SeqPathJoinUri($matches[0]))->getDepth(); if (!$pathOffset) { $start = null; $end = $curMatchDepth; } else { $start = current($pathOffset) + $curMatchDepth; $end = $start + $curMatchDepth; } $pathOffset = [$start, $end]; } if (!$result) { return false; } ### inject offset as metadata to get back on linked routers if ($pathOffset) { // $this->options()->setPathOffset($pathOffset); ### using on assemble things and ... $rSegement =& $request->meta()->__router_segment__; if (!is_array($rSegement)) { $rSegement = []; } $rSegement[$hashMeta] = $pathOffset; } $params = []; foreach ($this->_paramMap as $index => $name) { if (isset($matches[$index]) && $matches[$index] !== '') { $params[$name] = $this->_decode($matches[$index]); } } $routerMatch = clone $this; $routerMatch->params()->from(new Entity($params)); return $routerMatch; }