public function findRequestedApplication(RequestContextInterface $requestContext)
{
// prepare the request URL we want to match
$webappsDir = $this->getServerContext()->getServerConfig()->getDocumentRoot();
$relativeRequestPath = strstr($requestContext->getServerVar(ServerVars::DOCUMENT_ROOT) . $requestContext->getServerVar(ServerVars::X_REQUEST_URI), $webappsDir);
$proposedAppName = strstr(str_replace($webappsDir . '/', '', $relativeRequestPath), '/', true);
// try to match a registered application with the passed request
foreach ($this->applications as $application) {
if ($application->getName() === $proposedAppName) {
return $application;
}
}
// if we did not find anything we should throw a bad request exception
throw new BadRequestException(sprintf('Can\'t find application for URL %s%s', $requestContext->getServerVar(ServerVars::HTTP_HOST), $requestContext->getServerVar(ServerVars::X_REQUEST_URI)), 404);
}