/**
* Finds the correct route for the current request.
* It loads the correct data with the content mapper.
*
* @param Request $request A request against which to match.
*
* @return \Symfony\Component\Routing\RouteCollection with all Routes that
* could potentially match $request. Empty collection if nothing can
* match.
*/
public function getRouteCollectionForRequest(Request $request)
{
$collection = new RouteCollection();
$htmlExtension = '.html';
$resourceLocator = $this->requestAnalyzer->getResourceLocator();
if ($this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_REDIRECT || $this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_PARTIAL) {
// redirect webspace correctly with language
$collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute($request));
} elseif ($request->getRequestFormat() === 'html' && substr($request->getPathInfo(), -strlen($htmlExtension)) === $htmlExtension) {
// redirect *.html to * (without url)
$collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->getUrlWithoutEndingTrailingSlash($resourceLocator)));
} else {
// just show the page
$portal = $this->requestAnalyzer->getPortal();
$language = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
try {
// load content by url ignore ending trailing slash
$content = $this->contentMapper->loadByResourceLocator(rtrim($resourceLocator, '/'), $portal->getWebspace()->getKey(), $language);
if (preg_match('/\\/$/', $resourceLocator) && $this->requestAnalyzer->getResourceLocatorPrefix() && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
// redirect page to page without slash at the end
$collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute($request));
} elseif ($content->getNodeType() === Structure::NODE_TYPE_INTERNAL_LINK && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
// redirect internal link
$collection->add($content->getKey() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $content->getResourceLocator()));
} elseif ($content->getNodeType() === Structure::NODE_TYPE_EXTERNAL_LINK && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
$collection->add($content->getKey() . '_' . uniqid(), $this->getRedirectRoute($request, $content->getResourceLocator()));
} elseif ($content->getNodeState() === StructureInterface::STATE_TEST || !$content->getHasTranslation() || !$this->checkResourceLocator()) {
// error 404 page not published
throw new ResourceLocatorNotFoundException();
} else {
// show the page
$collection->add($content->getKey() . '_' . uniqid(), $this->getStructureRoute($request, $content));
}
} catch (ResourceLocatorNotFoundException $exc) {
// just do not add any routes to the collection
} catch (ResourceLocatorMovedException $exc) {
// old url resource was moved
$collection->add($exc->getNewResourceLocatorUuid() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $exc->getNewResourceLocator()));
} catch (RepositoryException $exc) {
// just do not add any routes to the collection
}
}
return $collection;
}