reduce dynamic.php (#7328)

* Reduce dynamic.php content - remove `mailer` from dynamic.php

* Autocommit PHP CS Fixer

* Reduce dynamic.php content - remove `mailer` from dynamic.php

* Reduce dynamic.php content - remove `mailer` from dynamic.php

* Reduce dynamic.php content - remove `cache` from dynamic.php

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Autocommit PHP CS Fixer

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Reduce dynamic.php content

* Autocommit PHP CS Fixer

* Reduce dynamic.php content

* Reduce dynamic.php content

* Autocommit PHP CS Fixer

* Reduce dynamic.php content

* Update CHANGELOG.md

---------

Co-authored-by: gevorgmansuryan <gevorgmansuryan@users.noreply.github.com>
Co-authored-by: Lucas Bartholemy <luke-@users.noreply.github.com>
This commit is contained in:
Gevorg Mansuryan 2024-12-11 21:53:48 +04:00 committed by GitHub
parent d8beaf2540
commit 1633770af3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 145 additions and 69 deletions

View File

@ -1,6 +1,9 @@
HumHub Changelog
================
1.18 (TBA)
------------------------------
- Enh #7328: `Mailer`, `User` and `Cache` configs removed from `dynamic.php`
1.17.0-beta.3 (Unreleased)
---------------------------------

View File

@ -0,0 +1,140 @@
<?php
namespace humhub\components\bootstrap;
use humhub\components\mail\Mailer;
use humhub\modules\admin\models\forms\MailingSettingsForm;
use yii\base\BootstrapInterface;
use yii\helpers\ArrayHelper;
use yii\log\Logger;
class SettingsLoader implements BootstrapInterface
{
public function bootstrap($app)
{
if (!$app) {
return;
}
$this->setMailerConfig($app);
$this->setUserConfig($app);
$this->setCacheConfig($app);
}
private function updateComponentDefinition($app, $component, $definition)
{
$app->set(
$component,
ArrayHelper::merge($this->getComponentDefinition($app, $component), $definition),
);
}
private function getComponentDefinition($app, $component, $property = null)
{
if (!is_null($property)) {
return ArrayHelper::getValue($app->components, [$component, $property]);
} else {
return ArrayHelper::getValue($app->components, $component, []);
}
}
private function setMailerConfig($app): void
{
if ($app->has('mailer', true)) {
$app->log->logger->log('`mailer` component should not be instantiated before settings are loaded.', Logger::LEVEL_WARNING);
}
$transportType = $app->settings->get('mailer.transportType', MailingSettingsForm::TRANSPORT_PHP);
//Check if Test environment
if ($this->getComponentDefinition($app, 'mailer', 'class') !== Mailer::class) {
$app->mailer->useFileTransport = true;
return;
}
if ($transportType === MailingSettingsForm::TRANSPORT_FILE) {
$definition = [
'transport' => ['dsn' => 'native://default'],
'useFileTransport' => true,
];
$this->updateComponentDefinition($app, 'mailer', $definition);
} elseif ($transportType === MailingSettingsForm::TRANSPORT_CONFIG) {
$app->set('mailer', false);
} else {
$definition = [
'useFileTransport' => true,
];
if ($transportType === MailingSettingsForm::TRANSPORT_SMTP) {
if ($app->settings->get('mailer.hostname')) {
$definition['transport']['host'] = $app->settings->get('mailer.hostname');
}
if ($app->settings->get('mailer.port')) {
$definition['transport']['port'] = (int)$app->settings->get('mailer.port');
} else {
$definition['transport']['port'] = 25;
}
if ($app->settings->get('mailer.username')) {
$definition['transport']['username'] = $app->settings->get('mailer.username');
}
if ($app->settings->get('mailer.password')) {
$definition['transport']['password'] = $app->settings->get('mailer.password');
}
$definition['transport']['scheme'] = (empty($app->settings->get('mailer.useSmtps'))) ? 'smtp' : 'smtps';
} elseif ($transportType === MailingSettingsForm::TRANSPORT_PHP) {
$definition['transport']['dsn'] = 'native://default';
} elseif ($transportType === MailingSettingsForm::TRANSPORT_DSN) {
$definition['transport']['dsn'] = $app->settings->get('mailer.dsn');
}
$this->updateComponentDefinition($app, 'mailer', $definition);
}
}
private function setUserConfig($app): void
{
if ($defaultUserIdleTimeoutSec = $app->getModule('user')->settings->get('auth.defaultUserIdleTimeoutSec')) {
if ($app->has('user', true)) {
$app->log->logger->log('`user` component should not be instantiated before settings are loaded.', Logger::LEVEL_WARNING);
} else {
$this->updateComponentDefinition($app, 'user', [
'authTimeout' => $defaultUserIdleTimeoutSec,
]);
}
}
}
private function setCacheConfig($app): void
{
if ($app->has('cache', true)) {
$app->log->logger->log('`cache` component should not be instantiated before settings are loaded.', Logger::LEVEL_WARNING);
}
$cacheClass = $app->settings->get('cache.class');
$cacheComponent = [];
if (in_array($cacheClass, [\yii\caching\DummyCache::class, \yii\caching\FileCache::class])) {
$cacheComponent = [
'class' => $cacheClass,
];
} elseif ($cacheClass == \yii\caching\ApcCache::class && (function_exists('apcu_add') || function_exists('apc_add'))) {
$cacheComponent = [
'class' => $cacheClass,
'useApcu' => (function_exists('apcu_add')),
];
} elseif ($cacheClass === \yii\redis\Cache::class) {
$cacheComponent = [
'class' => \yii\redis\Cache::class,
];
}
if (!empty($cacheComponent)) {
$this->updateComponentDefinition($app, 'cache', ArrayHelper::merge($cacheComponent, [
'keyPrefix' => $app->id,
]));
}
}
}

View File

@ -29,6 +29,7 @@ $config = [
'bootstrap' => [
'log',
'humhub\components\bootstrap\ModuleAutoLoader',
'humhub\components\bootstrap\SettingsLoader',
'queue',
'humhub\modules\ui\view\bootstrap\ThemeLoader',
],

View File

@ -8,7 +8,6 @@
namespace humhub\libs;
use humhub\modules\admin\models\forms\MailingSettingsForm;
use Yii;
use yii\base\BaseObject;
use yii\helpers\ArrayHelper;
@ -91,37 +90,10 @@ class DynamicConfig extends BaseObject
$config['name'] = Yii::$app->settings->get('name');
// Add Caching
$cacheClass = Yii::$app->settings->get('cache.class');
if (in_array($cacheClass, ['yii\caching\DummyCache', 'yii\caching\FileCache'])) {
$config['components']['cache'] = [
'class' => $cacheClass,
'keyPrefix' => Yii::$app->id,
];
} elseif ($cacheClass == 'yii\caching\ApcCache' && (function_exists('apcu_add') || function_exists('apc_add'))) {
$config['components']['cache'] = [
'class' => $cacheClass,
'keyPrefix' => Yii::$app->id,
'useApcu' => (function_exists('apcu_add')),
];
} elseif ($cacheClass === \yii\redis\Cache::class) {
$config['components']['cache'] = [
'class' => \yii\redis\Cache::class,
'keyPrefix' => Yii::$app->id,
];
}
// Add User settings
$config['components']['user'] = [];
if (Yii::$app->getModule('user')->settings->get('auth.defaultUserIdleTimeoutSec')) {
$config['components']['user']['authTimeout'] = Yii::$app->getModule('user')->settings->get('auth.defaultUserIdleTimeoutSec');
}
// Install Mail Component
$config['components']['mailer'] = self::getMailerConfig();
// Remove old theme/view stuff
unset($config['components']['view']);
unset($config['components']['mailer']['view']);
// Cleanups
unset($config['components']['db']['charset']);
@ -141,44 +113,6 @@ class DynamicConfig extends BaseObject
self::save($config);
}
private static function getMailerConfig()
{
$mail = [];
$mail['transport'] = [];
$transportType = Yii::$app->settings->get('mailer.transportType', MailingSettingsForm::TRANSPORT_PHP);
if ($transportType === MailingSettingsForm::TRANSPORT_SMTP) {
if (Yii::$app->settings->get('mailer.hostname')) {
$mail['transport']['host'] = Yii::$app->settings->get('mailer.hostname');
}
if (Yii::$app->settings->get('mailer.port')) {
$mail['transport']['port'] = (int)Yii::$app->settings->get('mailer.port');
} else {
$mail['transport']['port'] = 25;
}
if (Yii::$app->settings->get('mailer.username')) {
$mail['transport']['username'] = Yii::$app->settings->get('mailer.username');
}
if (Yii::$app->settings->get('mailer.password')) {
$mail['transport']['password'] = Yii::$app->settings->get('mailer.password');
}
$mail['transport']['scheme'] = (empty(Yii::$app->settings->get('mailer.useSmtps'))) ? 'smtp' : 'smtps';
} elseif ($transportType === MailingSettingsForm::TRANSPORT_CONFIG) {
return [];
} elseif ($transportType === MailingSettingsForm::TRANSPORT_PHP) {
$mail['transport']['dsn'] = 'native://default';
} elseif ($transportType === MailingSettingsForm::TRANSPORT_DSN) {
$mail['transport']['dsn'] = Yii::$app->settings->get('mailer.dsn');
} elseif ($transportType === MailingSettingsForm::TRANSPORT_FILE) {
unset($mail['transport']);
$mail['useFileTransport'] = true;
}
return $mail;
}
/**
* Checks whether the config should be rewritten based on changed setting name
*
@ -189,9 +123,7 @@ class DynamicConfig extends BaseObject
public static function needRewrite($moduleId, $name)
{
return (in_array($name, [
'name', 'defaultLanguage', 'timeZone', 'cache.class', 'mailer.transportType',
'mailer.hostname', 'mailer.username', 'mailer.password', 'mailer.encryption',
'mailer.port', 'horImageScrollOnMobile']));
'name', 'defaultLanguage', 'timeZone', 'horImageScrollOnMobile']));
}
public static function getConfigFilePath()