Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MiddlewareActionAbstract #53

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Classes/Middleware/MiddlewareActionAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

// Copyright JAKOTA Design Group GmbH. All rights reserved.
declare(strict_types=1);

namespace JAKOTA\Typo3ToolBox\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;

abstract class MiddlewareActionAbstract {
protected ?LanguageService $languageService = null;

protected LanguageServiceFactory $languageServiceFactory;

/**
* @var array<string, mixed>
*/
protected array $pathParams = [];

/**
* @var array<string, mixed>
*/
protected array $queryParams = [];

protected ServerRequestInterface $request;

protected ?Site $site;

protected ?SiteLanguage $siteLanguage;

protected UriBuilder $uriBuilder;

/**
* @param array<string, mixed> $pathParams
*
* @throws \InvalidArgumentException
*/
public function __construct(ServerRequestInterface $request, $pathParams) {
$this->request = $request;
$this->pathParams = $pathParams;
$this->queryParams = $this->request->getQueryParams();
$this->site = $this->request->getAttribute('site');

$langId = $this->queryParams['L'] ?? false;
if (false != $langId) {
$this->siteLanguage = $this->site?->getLanguageById(intval($langId));
} else {
$this->siteLanguage = $this->request->getAttribute('language');
}

$this->languageServiceFactory = GeneralUtility::makeInstance(LanguageServiceFactory::class);
$this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

// Set LanguageAspect for exbase
$context = GeneralUtility::makeInstance(Context::class);
$context->setAspect('language', new LanguageAspect($this->siteLanguage?->getLanguageId() ?? 0));

if (null !== $this->siteLanguage) {
$this->languageService = $this->languageServiceFactory->createFromSiteLanguage($this->siteLanguage);
$GLOBALS['LANG'] = $this->languageService;
}
}

protected function getAbsPath(?FileReference $file): string {
if (null == $file) {
return '';
}
if (null == $this->siteLanguage) {
return '';
}

return $this->siteLanguage->getBase()->getScheme().'://'.$this->siteLanguage->getBase()->getHost().'/'.$file->getOriginalResource()->getPublicUrl();
}

protected function getLocalizationFromKey(string $keyToTranslate, string $filePath): string {
if (null === $this->siteLanguage || null === $this->languageService) {
return $keyToTranslate;
}

$translatedString = $this->languageService->sL('LLL:'.$filePath.':'.$keyToTranslate);

if (!empty($translatedString)) {
return $translatedString;
}

return $keyToTranslate;
}
}
Loading