/**
*
* @param Request $request
* @throws AppException
* @return ApplicationUri
*/
public function match($request)
{
$found = null;
$uris = ApplicationUriQuery::create()->joinApplication()->filterByHttphost($request->getHttpHost())->find();
$requestUri = Text::create($request->getRequestUri())->trimRight('/');
foreach ($uris as $uri) {
$basepath = new Text($uri->getBasepath());
// either request uri and uri basepath are both empty
// or request uri starts with basepath
if ($basepath->isEmpty() && $uri->getHttphost() == $request->getHttpHost() || $requestUri->startsWith($basepath)) {
// assign when it's the first found
if ($found === null) {
$found = $uri;
} else {
if ($basepath->count('/') > Text::create($found->getBasepath())->count('/')) {
$found = $uri;
}
}
}
}
if ($found === null) {
throw new AppException(sprintf('No app found on %s', $request->getUri()), 404);
}
$this->destination = str_replace($found->getBasepath(), '', $request->getRequestUri());
return $found;
}