Skip to content

Commit

Permalink
Merge pull request #57 from MFabse/master
Browse files Browse the repository at this point in the history
[BUGFIX] Replace BackendConfigurationManager with ConfigurationManage…
  • Loading branch information
MFabse committed May 21, 2024
2 parents 61f46c7 + e3485cb commit 72f49dc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 797,235 deletions.
23 changes: 19 additions & 4 deletions Classes/Middleware/LanguageRedirectMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,33 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;

final class LanguageRedirectMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly BackendConfigurationManager $backendConfigurationManager,
private readonly ConfigurationManager $configurationManager,
private readonly LinkService $link
) {}

/**
* @throws UnknownLinkHandlerException
* @throws InvalidConfigurationTypeException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->isErrorPage($request)) {
$typoScript = $this->backendConfigurationManager->getTypoScriptSetup();
$typoScript = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT,
);

if (isset($typoScript['config.']['tx_locate']) && (int)$typoScript['config.']['tx_locate'] === 1) {
$locateSetup = $typoScript['config.']['tx_locate.'];
$locateSetup = $typoScript['config.']['tx_locate.'] ?? [];

$config = [
'verdicts' => $locateSetup['verdicts.'] ?? [],
Expand All @@ -59,6 +68,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

/**
* @throws UnknownLinkHandlerException
*/
private function isErrorPage(ServerRequestInterface $request): bool
{
$siteConfig = $request->getAttribute('site')->getConfiguration();
Expand All @@ -76,6 +88,9 @@ private function isErrorPage(ServerRequestInterface $request): bool
return false;
}

/**
* @throws UnknownLinkHandlerException
*/
private function getErrorPageUids(array $errorHandlers): array
{
$errorPageUids = [];
Expand Down
33 changes: 24 additions & 9 deletions Classes/Processor/Court.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(array $configuration)
public function run(): ?ResponseInterface
{
// Exclude bots from redirects
if ((bool)($this->configuration['settings']['excludeBots'] ?? false) && class_exists('Jaybizzle\CrawlerDetect\CrawlerDetect')) {
if (((bool)$this->configuration['settings']['excludeBots'] ?? false) && class_exists('Jaybizzle\CrawlerDetect\CrawlerDetect')) {
$crawlerDetect = new CrawlerDetect(
$GLOBALS['TYPO3_REQUEST']->getHeaders(),
GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
Expand All @@ -73,7 +73,10 @@ public function run(): ?ResponseInterface

if ($decision !== null) {
if (!$decision->hasVerdict()) {
throw new \Exception('No verdict should be delivered. This might be a problem in you configuration', 1608653067);
throw new \Exception(
'No verdict should be delivered. This might be a problem in you configuration',
1608653067
);
}
return $this->enforceJudgement($decision->getVerdictName());
}
Expand All @@ -89,7 +92,7 @@ public function run(): ?ResponseInterface
*/
protected function processFacts(): void
{
foreach ($this->configuration['facts'] ?? [] as $key => $className) {
foreach (($this->configuration['facts'] ?? []) as $key => $className) {
if (!class_exists($className)) {
$this->logger->warning(sprintf('Class "%s" does not exist. Skip.', $className));
continue;
Expand Down Expand Up @@ -118,7 +121,7 @@ protected function callJudges(): ?Decision
$judgements = [];
$priorities = [];

foreach ($this->configuration['judges'] ?? [] as $key => $className) {
foreach (($this->configuration['judges'] ?? []) as $key => $className) {
// Since we have an TypoScript array, skip every key which has sub properties
if (!is_string($className)) {
continue;
Expand Down Expand Up @@ -152,12 +155,21 @@ protected function callJudges(): ?Decision
return !empty($judgements) ? $this->getDecision($judgements) : null;
}

protected function addJudgement(array &$judgements, array $configuration, $key, AbstractJudge $judge, array &$priorities): void
{
$fact = (isset($configuration['fact']) && isset($this->facts[$configuration['fact']])) ? $this->facts[$configuration['fact']] : new StaticFactProvider();
protected function addJudgement(
array &$judgements,
array $configuration,
$key,
AbstractJudge $judge,
array &$priorities
): void {
$fact = isset($configuration['fact'], $this->facts[$configuration['fact']])
? $this->facts[$configuration['fact']]
: new StaticFactProvider();

if ($fact instanceof AbstractFactProvider) {
$judge = $judge->withConfiguration($this->configuration['judges'][$key . '.'] ?? [])->adjudicate($fact, (int)$key);
$judge = $judge->withConfiguration(
$this->configuration['judges'][$key . '.'] ?? []
)->adjudicate($fact, (int)$key);

if ($judge->hasDecision() && !isset($decisions[$judge->getDecision()->getPriority()])) {
$decision = $judge->getDecision();
Expand Down Expand Up @@ -206,7 +218,10 @@ protected function enforceJudgement(string $actionName): ?ResponseInterface
$this->logger->info(sprintf('Verdict with name %s will be delivered', $actionName));

if ($this->dryRun === false) {
$configuration = array_merge($this->configuration['settings'], $this->configuration['verdicts'][$actionName . '.'] ?? []);
$configuration = array_merge(
$this->configuration['settings'],
$this->configuration['verdicts'][$actionName . '.'] ?? []
);
$verdict = $verdict->withConfiguration($configuration);

return $verdict->execute();
Expand Down
Loading

0 comments on commit 72f49dc

Please sign in to comment.