瀏覽代碼

replace the implementation method of SiteResolver

Olivier Massot 4 年之前
父節點
當前提交
333c6c1ad7

+ 96 - 0
ot_optimizer/Classes/Site/SiteFinder.php

@@ -0,0 +1,96 @@
+<?php
+
+namespace Opentalent\OtOptimizer\XClass\Core\Site;
+
+use Opentalent\OtCore\Exception\NoSuchWebsiteException;
+use Opentalent\OtCore\Website\OtWebsiteRepository;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
+use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
+class SiteFinder extends \TYPO3\CMS\Core\Site\SiteFinder
+{
+    private $fallback;
+
+    /**
+     * Fetches all existing configurations as Site objects
+     */
+    public function __construct($siteConfiguration = null, $fallback = true)
+    {
+        $this->fallback = $fallback;
+        if ($this->fallback) {
+            parent::__construct($siteConfiguration);
+        }
+    }
+
+    /**
+     * Find a site by given root page id
+     *
+     * @param int $rootPageId the page ID (default language)
+     * @return Site
+     * @throws SiteNotFoundException
+     * @internal only for usage in some places for managing Site Configuration, might be removed without further notice
+     */
+    public function getSiteByRootPageId(int $rootPageId): Site
+    {
+        if ($this->fallback) {
+            return parent::getSiteByRootPageId($rootPageId);
+        }
+
+        $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
+        try {
+            $website = $otWebsiteRepository->getWebsiteByPageUid($rootPageId);
+            return $otWebsiteRepository->generateWebsiteConfiguration($website);
+        } catch (NoSuchWebsiteException $e) {
+            throw new SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
+        }
+    }
+
+    /**
+     * Find a site by given identifier
+     *
+     * @param string $identifier
+     * @return Site
+     * @throws SiteNotFoundException
+     */
+    public function getSiteByIdentifier(string $identifier): Site
+    {
+        if ($this->fallback) {
+            return parent::getSiteByIdentifier($identifier);
+        }
+
+        $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
+
+        try {
+            $website = $otWebsiteRepository->getWebsiteByConfigIdentifier($identifier);
+            return $otWebsiteRepository->generateWebsiteConfiguration($website);
+        } catch (NoSuchWebsiteException $e) {
+            throw new SiteNotFoundException('No site found for identifier ' . $identifier, 1521716628);
+        }
+    }
+
+    /**
+     *
+     *
+     * @param int $pageId
+     * @param array $rootLine
+     * @param string|null $mountPointParameter
+     * @return Site
+     * @throws SiteNotFoundException
+     */
+    public function getSiteByPageId(int $pageId, array $rootLine = null, string $mountPointParameter = null): Site
+    {
+        if ($this->fallback) {
+            return parent::getSiteByPageId($pageId, $rootLine, $mountPointParameter);
+        }
+
+        $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
+        try {
+            $website = $otWebsiteRepository->getWebsiteByPageUid($pageId);
+            return $otWebsiteRepository->generateWebsiteConfiguration($website);
+        } catch (NoSuchWebsiteException $e) {
+            throw new SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
+        }
+    }
+}

+ 53 - 0
ot_optimizer/Classes/XClass/Frontend/Middleware/SiteResolver.php

@@ -0,0 +1,53 @@
+<?php
+declare(strict_types = 1);
+namespace Opentalent\OtOptimizer\XClass\Frontend\Middleware;
+
+use Opentalent\OtOptimizer\Routing\SiteMatcher;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
+/**
+ */
+class SiteResolver extends \TYPO3\CMS\Frontend\Middleware\SiteResolver
+{
+    private bool $shallFallback;
+
+    public function __construct($matcher = null)
+    {
+        $this->shallFallback = $_COOKIE['optimize'] != 1;
+
+        if ($this->shallFallback) {
+            parent::__construct($matcher);
+        }
+    }
+
+    /**
+     * Resolve the site/language information by checking the page ID or the URL.
+     *
+     * @param ServerRequestInterface $request
+     * @param RequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        if ($this->shallFallback) {
+            return parent::process($request, $handler);
+        }
+
+        $matcher = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteMatcher::class);
+        $routeResult = $matcher->matchRequest($request);
+
+        $request = $request->withAttribute('site', $routeResult->getSite());
+        $request = $request->withAttribute('language', $routeResult->getLanguage());
+        $request = $request->withAttribute('routing', $routeResult);
+
+        // At this point, we later get further route modifiers
+        // for bw-compat we update $GLOBALS[TYPO3_REQUEST] to be used later in TSFE.
+        $GLOBALS['TYPO3_REQUEST'] = $request;
+
+        return $handler->handle($request);
+    }
+}

+ 18 - 0
ot_optimizer/Configuration/RequestMiddlewares.php

@@ -0,0 +1,18 @@
+<?php
+
+/**
+ * Register middlewares, which will be triggered at each request
+ */
+return [
+    'frontend' => [
+        'typo3/cms-frontend/site' => [
+            'target' => Opentalent\OtOptimizer\XClass\Frontend\Middleware\SiteResolver::class,
+            'before' => [
+                'typo3/cms-adminpanel/initiator'
+            ],
+            'after' => [
+                'typo3/cms-adminpanel/sql-logging'
+            ],
+        ],
+    ],
+];

+ 2 - 3
ot_optimizer/ext_localconf.php

@@ -5,6 +5,5 @@ $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Core\Routing\PageRouter:
     'className' => Opentalent\OtOptimizer\XClass\Core\Routing\OtPageRouter::class
 ];
 
-$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Frontend\Middleware\SiteResolver::class] = [
-    'className' => Opentalent\OtOptimizer\XClass\Frontend\Middleware\SiteResolver::class
-];
+// \TYPO3\CMS\Frontend\Middleware\SiteResolver is overridden but not xclassed here; instead it is
+//   replaced in the middlewares array