From e23d9f15d6cad4f0c8bc2ac473db9d48c7ef7804 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Tue, 10 Dec 2024 04:36:35 +0100 Subject: [PATCH] Enh/aliases (#7333) * Used Aliases ENV Vars * Autocommit PHP CS Fixer * Improved ENV loading * Fixed missing return handling * Update CHANGELOG.md --- CHANGELOG.md | 1 + .../humhub/components/ApplicationTrait.php | 3 ++ protected/humhub/config/common.php | 37 +++++++++++++------ protected/humhub/helpers/EnvHelper.php | 27 ++++++++++++++ .../widgets/assets/AjaxLinkPagerAsset.php | 2 +- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ea142057..b33fccd400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) --------------------------------- diff --git a/protected/humhub/components/ApplicationTrait.php b/protected/humhub/components/ApplicationTrait.php index f8a2567ade..083b0948d2 100644 --- a/protected/humhub/components/ApplicationTrait.php +++ b/protected/humhub/components/ApplicationTrait.php @@ -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); diff --git a/protected/humhub/config/common.php b/protected/humhub/config/common.php index cf409e9e13..16f11324bb 100644 --- a/protected/humhub/config/common.php +++ b/protected/humhub/config/common.php @@ -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'], ], diff --git a/protected/humhub/helpers/EnvHelper.php b/protected/humhub/helpers/EnvHelper.php index ba5375686f..acd3c3c7ea 100644 --- a/protected/humhub/helpers/EnvHelper.php +++ b/protected/humhub/helpers/EnvHelper.php @@ -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; + } } diff --git a/protected/humhub/widgets/assets/AjaxLinkPagerAsset.php b/protected/humhub/widgets/assets/AjaxLinkPagerAsset.php index 5b28e46d0c..20950f6b24 100644 --- a/protected/humhub/widgets/assets/AjaxLinkPagerAsset.php +++ b/protected/humhub/widgets/assets/AjaxLinkPagerAsset.php @@ -9,7 +9,7 @@ class AjaxLinkPagerAsset extends AssetBundle /** * @inheritdoc */ - public $sourcePath = '@app/humhub/widgets/resources'; + public $sourcePath = '@humhub/widgets/resources'; /** * @inheritdoc