ew_base/Classes/Configuration/AdditionalConfiguration.php
2024-05-20 20:02:24 +02:00

171 lines
5.4 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Evoweb\EwBase\Configuration;
/*
* This file is part of TYPO3 CMS-based extension "container" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class AdditionalConfiguration
{
protected string $extensionKey = 'ew_base';
protected array $developConfig = [
'BE' => [
'debug' => true,
'loginRateLimit' => 10000,
],
'FE' => [
'debug' => true,
'loginRateLimit' => 10000,
],
'GFX' => [
'processor' => 'GraphicsMagick',
'processor_colorspace' => 'RGB',
'processor_path' => '/usr/bin/',
'processor_path_lzw' => '/usr/bin/',
],
'SYS' => [
'devIPmask' => '*',
'displayErrors' => 1,
'systemLogLevel' => 0,
'exceptionalErrors' => 12290,
'errorHandlerErrors' => E_ALL & ~(
E_STRICT | E_NOTICE | E_WARNING | E_COMPILE_WARNING | E_COMPILE_ERROR
| E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR
),
],
];
/**
* This is needed to override all mail settings instead of merging them.
*
* @var array|string[]
*/
protected array $mailConfig = [
'transport' => 'smtp',
'transport_smtp_server' => '127.0.0.1:1025',
'defaultMailFromAddress' => 'test@dev.arpa',
];
public function initialize(array $configuration = []): void
{
$this->addContextToSitename();
$this->addContextConfiguration($configuration);
if (Environment::getContext() == 'Development') {
$this->addBaseUrl();
$this->addDebugConfiguration();
}
$this->addFurtherConfigurationFiles();
}
protected function addContextToSitename(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] .= ' - ' . Environment::getContext();
}
/**
* Set the baseurl on local environments automatically
*/
protected function addBaseUrl(): void
{
if (Environment::isCli()) {
return;
}
$remoteHost = GeneralUtility::getIndpEnv('HTTP_HOST');
ExtensionManagementUtility::addTypoScript(
$this->extensionKey,
'constants',
'
// condition should trigger different cache hashes
[request && request.getNormalizedParams().getHttpHost() == \'' . $remoteHost . '\']
config.baseURL = ' . $remoteHost . '
[end]
',
'defaultContentRendering'
);
}
protected function addDebugConfiguration(): void
{
$GLOBALS['TYPO3_CONF_VARS'] = $this->arrayMergeRecursive(
$GLOBALS['TYPO3_CONF_VARS'],
$this->developConfig
);
$layoutRootPaths = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['layoutRootPaths'] ?: [];
$partialRootPaths = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['partialRootPaths'] ?: [];
$templateRootPaths = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] ?: [];
$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->mailConfig;
if (!empty($layoutRootPaths)) {
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['layoutRootPaths'] = $layoutRootPaths;
}
if (!empty($partialRootPaths)) {
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['partialRootPaths'] = $partialRootPaths;
}
if (!empty($templateRootPaths)) {
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] = $templateRootPaths;
}
}
protected function addContextConfiguration(array $configuration): void
{
$context = (string)Environment::getContext();
if (isset($configuration[$context]) && is_array($configuration[$context])) {
$GLOBALS['TYPO3_CONF_VARS'] = $this->arrayMergeRecursive(
$GLOBALS['TYPO3_CONF_VARS'],
$configuration[$context]
);
}
}
/**
* Loads additional configuration two levels up relative to instance
* or relative to ADDITIONAL_CONFIG_FILE configuration
*/
protected function addFurtherConfigurationFiles(): void
{
$paths = [
Environment::getPublicPath() . '/../../../AdditionalConfiguration.php',
Environment::getPublicPath() . '/' . ($_SERVER['ADDITIONAL_CONFIG_FILE'] ?? time() . '.php'),
];
foreach ($paths as $path) {
$path = realpath($path);
if ($path && @file_exists($path)) {
/** @noinspection */
require_once $path;
}
}
}
protected function arrayMergeRecursive(array $array1, array $array2): array
{
$merged = $array1;
foreach ($array2 as $key => $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMergeRecursive($merged[$key], $value);
} elseif (is_numeric($key)) {
if (!in_array($value, $merged)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
}
}