|
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Vendor\MyExtension\EventListener; |
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
use Psr\Container\NotFoundExceptionInterface; |
|
use Psr\Log\LoggerInterface; |
|
use TYPO3\CMS\Core\Attribute\AsEventListener; |
|
use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
|
use TYPO3\CMS\Core\Log\LogManager; |
|
use TYPO3\CMS\Core\Site\SiteFinder; |
|
use TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent; |
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
|
|
#[AsEventListener('vendor-myextension/site-settings-for-tsconfig')] |
|
final readonly class SiteSettingsForTsConfig |
|
{ |
|
private LoggerInterface $logger; |
|
|
|
public function __construct() |
|
{ |
|
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); |
|
} |
|
|
|
public function __invoke(EvaluateModifierFunctionEvent $event): void |
|
{ |
|
$functionName = $event->getFunctionName(); |
|
|
|
if ($functionName === 'SiteSettings' || $functionName === 'SiteConfig') { |
|
$functionArgument = $event->getFunctionArgument(); |
|
$functionArguments = explode(':', $functionArgument); |
|
|
|
if (count($functionArguments) < 2) { |
|
$this->logger->warning( |
|
sprintf( |
|
'Missing arguments for SiteSettingsForTsConfig function "%s". Expected at least "identifier:key", got "%s".', |
|
$functionName, |
|
$functionArgument |
|
), |
|
['functionName' => $functionName, 'argument' => $functionArgument] |
|
); |
|
$event->setValue(''); |
|
return; |
|
} |
|
|
|
$siteIdentifier = $functionArguments[0]; |
|
$key = $functionArguments[1]; |
|
|
|
try { |
|
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
|
$site = $siteFinder->getSiteByIdentifier($siteIdentifier); |
|
|
|
switch ($functionName) { |
|
case 'SiteConfig': |
|
$configuration = $site->getConfiguration(); |
|
if (isset($configuration[$key])) { |
|
$event->setValue((string)$configuration[$key]); |
|
} else { |
|
$this->logger->warning( |
|
sprintf('Configuration key "%s" not found for site "%s" in SiteSettingsForTsConfig function "%s".', $key, $siteIdentifier, $functionName), |
|
['siteIdentifier' => $siteIdentifier, 'key' => $key, 'functionName' => $functionName] |
|
); |
|
$event->setValue(''); |
|
} |
|
break; |
|
|
|
case 'SiteSettings': |
|
$settings = $site->getSettings(); |
|
if ($settings->has($key)) { |
|
$event->setValue((string)$settings->get($key)); |
|
} else { |
|
$this->logger->warning( |
|
sprintf('Setting key "%s" not found for site "%s" in SiteSettingsForTsConfig function "%s".', $key, $siteIdentifier, $functionName), |
|
['siteIdentifier' => $siteIdentifier, 'key' => $key, 'functionName' => $functionName] |
|
); |
|
$event->setValue(''); |
|
} |
|
break; |
|
} |
|
} catch (SiteNotFoundException $e) { |
|
$this->logger->error( |
|
sprintf('Site not found for SiteSettingsForTsConfig function "%s" with identifier "%s": %s', $functionName, $siteIdentifier, $e->getMessage()), |
|
['siteIdentifier' => $siteIdentifier, 'functionName' => $functionName, 'exception' => $e] |
|
); |
|
$event->setValue(''); |
|
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) { |
|
$this->logger->critical( |
|
sprintf('Dependency injection error in SiteSettingsForTsConfig function "%s": %s', $functionName, $e->getMessage()), |
|
['functionName' => $functionName, 'exception' => $e] |
|
); |
|
$event->setValue(''); |
|
} catch (\Throwable $e) { |
|
$this->logger->error( |
|
sprintf('An unexpected error occurred in SiteSettingsForTsConfig function "%s": %s', $functionName, $e->getMessage()), |
|
['functionName' => $functionName, 'exception' => $e] |
|
); |
|
$event->setValue(''); |
|
} |
|
} |
|
} |
|
} |