SiteMatcher.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Opentalent\OtOptimizer\Routing;
  3. use Opentalent\OtCore\Website\OtWebsiteRepository;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use TYPO3\CMS\Core\Routing\SiteRouteResult;
  6. use TYPO3\CMS\Core\Site\SiteFinder;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Object\ObjectManager;
  9. /**
  10. * Hooks into the frontend request and use the ot_websites table to resolve the site configuration
  11. * (frontend only)
  12. *
  13. * @internal
  14. */
  15. class SiteMatcher extends \TYPO3\CMS\Core\Routing\SiteMatcher
  16. {
  17. public function __construct($finder = null)
  18. {
  19. }
  20. /**
  21. * Override the default Typo3 \TYPO3\CMS\Core\Routing\SiteMatcher
  22. *
  23. * @param ServerRequestInterface $request
  24. * @return \TYPO3\CMS\Core\Routing\RouteResultInterface
  25. * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
  26. */
  27. public function matchRequest(ServerRequestInterface $request): \TYPO3\CMS\Core\Routing\RouteResultInterface
  28. {
  29. $devMode = $_SERVER['TYPO3_CONTEXT'] == "Development";
  30. $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
  31. $otWebsite = $otWebsiteRepository->matchUriToWebsite($request->getUri(), $devMode);
  32. $site = $otWebsiteRepository->generateWebsiteConfiguration($otWebsite);
  33. if ($devMode) {
  34. preg_match("/\w+\/(.*)/", $request->getUri()->getPath(), $m);
  35. $tail = $m[1] ?? "";
  36. } else {
  37. $tail = rtrim($request->getUri()->getPath(), '/');
  38. }
  39. $language = $site->getDefaultLanguage();
  40. return new SiteRouteResult($request->getUri(), $site, $language, $tail);
  41. }
  42. }