Get site config from request attribute in middleware

This commit is contained in:
Björn Fromme
2022-05-29 19:44:47 +02:00
parent d65cc0a66e
commit f39c40b592
@@ -2,31 +2,6 @@
namespace EP\EpProducts\Middleware; namespace EP\EpProducts\Middleware;
/***************************************************************
*
* Copyright notice
*
* (c) 2019 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use EP\EpProducts\Service\FilterService; use EP\EpProducts\Service\FilterService;
use EP\EpProducts\Service\FilterSettingsEncoder; use EP\EpProducts\Service\FilterSettingsEncoder;
use EP\EpProducts\Traits\DbConnectionTrait; use EP\EpProducts\Traits\DbConnectionTrait;
@@ -35,12 +10,9 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Http\NormalizedParams; use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
class StaticSearchParamsMiddleware implements MiddlewareInterface class StaticSearchParamsMiddleware implements MiddlewareInterface
{ {
@@ -82,11 +54,7 @@ class StaticSearchParamsMiddleware implements MiddlewareInterface
return $handler->handle($request); return $handler->handle($request);
} }
/** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */ $filterService = GeneralUtility::makeInstance(FilterService::class);
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var \EP\EpProducts\Service\FilterService $filterService */
$filterService = $objectManager->get(FilterService::class);
$filterSettings = $filterService->getFilterSettingsFromStaticSearchParams($row); $filterSettings = $filterService->getFilterSettingsFromStaticSearchParams($row);
$filterSettingsEncoder = new FilterSettingsEncoder(); $filterSettingsEncoder = new FilterSettingsEncoder();
@@ -95,18 +63,9 @@ class StaticSearchParamsMiddleware implements MiddlewareInterface
$searchPageUid = GeneralUtility::makeInstance(ExtensionConfiguration::class) $searchPageUid = GeneralUtility::makeInstance(ExtensionConfiguration::class)
->get('ep_products', 'searchPageUid'); ->get('ep_products', 'searchPageUid');
/** @var SiteFinder $siteFinder */ $site = $request->getAttribute('site');
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
try {
$site = $siteFinder->getSiteByPageId((int)$searchPageUid);
}
catch (SiteNotFoundException $exception) {
return $handler->handle($request);
}
$redirectUri = $site->getRouter()->generateUri($searchPageUid, ['f' => $encodedFilterSettings]); $redirectUri = $site->getRouter()->generateUri($searchPageUid, ['f' => $encodedFilterSettings]);
return new RedirectResponse($redirectUri, self::REDIRECT_RESPONSE_CODE); return new RedirectResponse($redirectUri, self::REDIRECT_RESPONSE_CODE);
} }
} }