ew_base/Classes/Configuration/AdditionalConfiguration.php

157 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is part of TYPO3 CMS-based extension "ew-base" by evoWeb.
*
* 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.
*/
namespace Evoweb\EwBase\Configuration;
use TYPO3\CMS\Core\Core\Environment;
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' => 0,
],
];
/**
* This is needed to override all mail settings instead of merging them.
*
* @var array<string, string>
*/
protected array $mailConfig = [
'transport' => 'smtp',
'transport_smtp_server' => 'mailpit:1025'
];
public function __construct()
{
if (PHP_VERSION_ID >= 80400) {
$this->developConfig['SYS']['errorHandlerErrors'] = E_ALL & ~(
\E_NOTICE | \E_WARNING | \E_COMPILE_WARNING | \E_COMPILE_ERROR
| \E_CORE_WARNING | \E_CORE_ERROR | \E_PARSE | \E_ERROR
);
} else {
$this->developConfig['SYS']['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
);
}
}
public function initialize(array $configuration = []): void
{
$this->addContextToSitename();
$this->addContextConfiguration($configuration);
if (Environment::getContext() == 'Development') {
$this->addDebugConfiguration();
}
$this->addFurtherConfigurationFiles();
}
protected function addContextToSitename(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] .= ' - ' . Environment::getContext();
}
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;
}
}