Enh/aliases (#7333)

* Used Aliases ENV Vars

* Autocommit PHP CS Fixer

* Improved ENV loading

* Fixed missing return handling

* Update CHANGELOG.md
This commit is contained in:
Lucas Bartholemy 2024-12-10 04:36:35 +01:00 committed by GitHub
parent 482773ffe3
commit e23d9f15d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 13 deletions

View File

@ -12,6 +12,7 @@ HumHub Changelog
- Fix #7316: Fix formatter default time zone
- Enh #7317: Space browser: Make the whole space card header and body clickable
- Enh: Add missing IDs in the modal login forms
- Enh #7333: Improved Yii alias handling and added ENV support
1.17.0-beta.2 (November 12, 2024)
---------------------------------

View File

@ -9,6 +9,7 @@
namespace humhub\components;
use humhub\helpers\DatabaseHelper;
use humhub\helpers\EnvHelper;
use humhub\interfaces\MailerInterface;
use humhub\libs\DynamicConfig;
use humhub\libs\SelfTest;
@ -45,6 +46,8 @@ trait ApplicationTrait
*/
public function __construct($config = [])
{
$config = EnvHelper::resolveConfigAliases($config);
$this->loadedAppConfig = $config;
$config = $this->removeLegacyConfigSettings($config);

View File

@ -8,11 +8,7 @@
use humhub\components\i18n\PhpMessageSource;
Yii::setAlias('@webroot', realpath(__DIR__ . '/../../../'));
Yii::setAlias('@app', '@webroot/protected');
Yii::setAlias('@humhub', '@app/humhub');
Yii::setAlias('@config', '@app/config');
Yii::setAlias('@themes', '@webroot/themes');
Yii::setAlias('@humhub', $_ENV['HUMHUB_ALIASES__HUMHUB'] ?? realpath(__DIR__ . '/../'));
// Workaround: PHP 7.3 compatible ZF2 ArrayObject class
Yii::$classMap['Zend\Stdlib\ArrayObject'] = '@humhub/compat/ArrayObject.php';
@ -30,12 +26,21 @@ $config = [
'minRecommendedPhpVersion' => '8.1',
'minSupportedPhpVersion' => '8.1',
'basePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'bootstrap' => ['log', 'humhub\components\bootstrap\ModuleAutoLoader', 'queue', 'humhub\modules\ui\view\bootstrap\ThemeLoader'],
'bootstrap' => [
'log',
'humhub\components\bootstrap\ModuleAutoLoader',
'queue',
'humhub\modules\ui\view\bootstrap\ThemeLoader',
],
'runtimePath' => '@app/runtime',
'sourceLanguage' => 'en',
'aliases' => [
'@webroot' => realpath(__DIR__ . '/../../../'),
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
'@filestore' => '@webroot/uploads/file',
'@config' => '@app/config',
'@themes' => '@webroot/themes',
],
'components' => [
'moduleManager' => [
@ -60,9 +65,13 @@ $config = [
'class' => \yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:400', 'yii\web\HttpException:401', 'yii\web\HttpException:403',
'yii\web\HttpException:404', 'yii\web\HttpException:405',
'yii\web\User::getIdentityAndDurationFromCookie', 'yii\web\User::renewAuthStatus',
'yii\web\HttpException:400',
'yii\web\HttpException:401',
'yii\web\HttpException:403',
'yii\web\HttpException:404',
'yii\web\HttpException:405',
'yii\web\User::getIdentityAndDurationFromCookie',
'yii\web\User::renewAuthStatus',
],
'logVars' => ['_GET', '_SERVER'],
],
@ -70,9 +79,13 @@ $config = [
'class' => \yii\log\DbTarget::class,
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:400', 'yii\web\HttpException:401', 'yii\web\HttpException:403',
'yii\web\HttpException:404', 'yii\web\HttpException:405',
'yii\web\User::getIdentityAndDurationFromCookie', 'yii\web\User::renewAuthStatus',
'yii\web\HttpException:400',
'yii\web\HttpException:401',
'yii\web\HttpException:403',
'yii\web\HttpException:404',
'yii\web\HttpException:405',
'yii\web\User::getIdentityAndDurationFromCookie',
'yii\web\User::renewAuthStatus',
],
'logVars' => ['_GET', '_SERVER'],
],

View File

@ -14,6 +14,8 @@ class EnvHelper
private const FIXED_SETTINGS_PATH = ['params', 'fixed-settings'];
private const DEPTH_SEPARATOR = '__';
private const ALIASES_PREFIX = 'HUMHUB_ALIASES';
public static function toConfig(?array $env = []): array
{
$config = [];
@ -76,4 +78,29 @@ class EnvHelper
},
);
}
/**
* Writes variables defined in ENV with the syntax `HUMHUB_ALIASES__ALIASNAME` to the config alias map.
*
* @param array $config
* @return array
*/
public static function resolveConfigAliases(array $config): array
{
if (!is_array($_ENV)) {
return $config;
}
if (!is_array($config['aliases'])) {
$config['aliases'] = [];
}
foreach ($_ENV as $key => $value) {
if (StringHelper::startsWith($key, self::ALIASES_PREFIX . self::DEPTH_SEPARATOR)) {
$aliasName = str_replace(self::ALIASES_PREFIX . self::DEPTH_SEPARATOR, '', $key);
$config['aliases']['@' . strtolower($aliasName)] = $value;
}
}
return $config;
}
}

View File

@ -9,7 +9,7 @@ class AjaxLinkPagerAsset extends AssetBundle
/**
* @inheritdoc
*/
public $sourcePath = '@app/humhub/widgets/resources';
public $sourcePath = '@humhub/widgets/resources';
/**
* @inheritdoc