浏览代码

XClass the typo3 SiteMatcher

Olivier Massot 4 年之前
父节点
当前提交
54a223efe7

+ 43 - 7
ot_core/Classes/Website/OtWebsiteRepository.php

@@ -42,7 +42,7 @@ class OtWebsiteRepository
             ->execute()
             ->execute()
             ->fetch();
             ->fetch();
         if (!isset($website['uid'])) {
         if (!isset($website['uid'])) {
-            throw new NoSuchWebsiteException('No website found for organization ' . $organizationId);
+            throw new NoSuchWebsiteException('No website found with uid ' . $uid);
         }
         }
         return $website;
         return $website;
     }
     }
@@ -177,18 +177,16 @@ class OtWebsiteRepository
      * Generate an array as it would be loaded from the site.yaml configuration
      * Generate an array as it would be loaded from the site.yaml configuration
      * file of the given website
      * file of the given website
      *
      *
-     * @param int $rootUid
-     * @param string $domain
+     * @param array $website
      * @return Site
      * @return Site
      * @throws InvalidWebsiteConfigurationException
      * @throws InvalidWebsiteConfigurationException
-     * @throws NoSuchWebsiteException
      */
      */
-    public function generateWebsiteConfiguration(int $rootUid, string $domain): Site
+    public function generateWebsiteConfiguration(array $website): Site
     {
     {
-        $website = $this->getWebsiteByPageUid($rootUid);
+        $rootUid = $this->getWebsiteRootUid($website['uid']);
 
 
         return new Site(
         return new Site(
-            $website['uid'],
+            (string)$website['organization_id'],
             $rootUid,
             $rootUid,
             [
             [
                 'base' => $this->resolveWebsiteBaseUri($website),
                 'base' => $this->resolveWebsiteBaseUri($website),
@@ -228,6 +226,44 @@ class OtWebsiteRepository
         );
         );
     }
     }
 
 
+    /**
+     * Try to retrieve the website matching the given Uri
+     *
+     * @param \Psr\Http\Message\UriInterface $uri
+     * @param bool $devMode
+     * @return Site
+     * @throws NoSuchWebsiteException
+     */
+    public function matchUriToWebsite(\Psr\Http\Message\UriInterface $uri, bool $devMode=false): Site
+    {
+        $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
+
+        $q = $queryBuilder
+            ->select('*')
+            ->from('ot_websites');
+
+        if ($devMode) {
+            preg_match("/(\w+)(?:\/.*)?/", $uri->getPath(), $m);
+            $q = $q->where($queryBuilder->expr()->eq('subdomain', $queryBuilder->expr()->literal($m[1])));
+        } else {
+            preg_match("/([\w]+).opentalent.fr/", $uri->getHost(), $m);
+            if (count($m) > 0) {
+                $q = $q->where($queryBuilder->expr()->eq('subdomain', $queryBuilder->expr()->literal($m[1])));
+            } else {
+                $q = $q->where($queryBuilder->expr()->eq('custom_domain', $queryBuilder->expr()->literal($uri->getHost())));
+            }
+        }
+
+        $website = $q->execute()
+                    ->fetch();
+
+        if (!isset($website['uid'])) {
+            throw new NoSuchWebsiteException('No website found for this URI: ' . $uri);
+        }
+
+        return $this->generateWebsiteConfiguration($website);
+    }
+
     /**
     /**
      * Try to find the config file of the website in the less resource-consuming way
      * Try to find the config file of the website in the less resource-consuming way
      * and parse it.
      * and parse it.

+ 0 - 59
ot_optimizer/Classes/XClass/Routing/FeRouter.php

@@ -1,59 +0,0 @@
-<?php
-namespace Opentalent\OtCore\Middleware;
-
-use Opentalent\OtCore\Routing\Resolver;
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Server\MiddlewareInterface;
-use Psr\Http\Server\RequestHandlerInterface;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Object\ObjectManager;
-use TYPO3\CMS\Frontend\Controller\ErrorController;
-use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
-
-/**
- * Hooks into the frontend request and use the index table to resolve a page uid
- *
- * @internal
- */
-class FeRouter implements MiddlewareInterface
-{
-    /**
-     *
-     * @param ServerRequestInterface $request
-     * @param RequestHandlerInterface $handler
-     * @return ResponseInterface
-     */
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
-    {
-//        if (TYPO3_MODE === 'FE') {
-//            $uri = $request->getUri();
-//            $resolver = GeneralUtility::makeInstance(ObjectManager::class)->get(Resolver::class);
-//            if ($_SERVER['TYPO3_CONTEXT'] == 'Development') {
-//                $pageUid = $resolver->resolveDevRoute(
-//                    $uri->getPath()
-//                );
-//            } else {
-//                $pageUid = $resolver->resolveRoute(
-//                    $uri->getHost(),
-//                    $uri->getPath(),
-//                );
-//            }
-//
-//            if ($pageUid) {
-//                $params = $request->getQueryParams();
-//                $params['id'] = $pageUid;
-//                $request = $request->withQueryParams($params);
-//            } else {
-//                return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
-//                    $request,
-//                    'The requested page does not exist',
-//                    ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
-//                );
-//            }
-//        }
-
-        // just pass the plate to the next middleware...
-        return $handler->handle($request);
-    }
-}

+ 46 - 0
ot_optimizer/Classes/XClass/Routing/SiteMatcher.php

@@ -0,0 +1,46 @@
+<?php
+namespace Opentalent\OtOptimizer\XClass\Routing;
+
+use Opentalent\OtCore\Website\OtWebsiteRepository;
+use Psr\Http\Message\ServerRequestInterface;
+use TYPO3\CMS\Core\Routing\SiteRouteResult;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
+/**
+ * Hooks into the frontend request and use the index table to resolve a page uid
+ *
+ * @internal
+ */
+class SiteMatcher extends \TYPO3\CMS\Core\Routing\SiteMatcher
+{
+    /**
+     * Override the default Typo3 \TYPO3\CMS\Core\Routing\SiteMatcher
+     *
+     * @param ServerRequestInterface $request
+     * @return \TYPO3\CMS\Core\Routing\RouteResultInterface
+     * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
+     */
+    public function matchRequest(ServerRequestInterface $request): \TYPO3\CMS\Core\Routing\RouteResultInterface
+    {
+        if (!$request->getQueryParams()['optimize'] == 1) {
+            return parent::matchRequest($request);
+        }
+
+        $devMode = $_SERVER['TYPO3_CONTEXT'] == "Development";
+
+        $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
+
+        $site = $otWebsiteRepository->matchUriToWebsite($request->getUri(), $devMode);
+
+        if ($devMode) {
+            preg_match("/\w+\/(.*)/", $request->getUri()->getPath(), $m);
+            $tail = $m[1] ?? "";
+        } else {
+            $tail = $request->getUri()->getPath();
+        }
+
+        $language = $site->getDefaultLanguage();
+        return new SiteRouteResult($request->getUri(), $site, $language, $tail);
+    }
+}

+ 4 - 0
ot_optimizer/ext_localconf.php

@@ -1,6 +1,10 @@
 <?php
 <?php
 defined('TYPO3_MODE') || die();
 defined('TYPO3_MODE') || die();
 
 
+$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Core\Routing\SiteMatcher::class] = [
+    'className' => Opentalent\OtOptimizer\XClass\Routing\SiteMatcher::class
+];
+
 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Core\Routing\PageRouter::class] = [
 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Core\Routing\PageRouter::class] = [
     'className' => Opentalent\OtOptimizer\XClass\Routing\OtPageRouter::class
     'className' => Opentalent\OtOptimizer\XClass\Routing\OtPageRouter::class
 ];
 ];