diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc958db6c..66c822631b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ HumHub Changelog - Enh #7344: Disable editing Base URL when setting is fixed - Fix #7345: Fix debug mode setting in .env - Enh #7349: Add body classes about the current device and methods to the `DeviceDetectorHelper` class +- Enh #7353: Enable dot env to read variables based on application type 1.17.0-beta.2 (November 12, 2024) --------------------------------- diff --git a/index-test.php b/index-test.php index da9ade4678..05a2a09b67 100644 --- a/index-test.php +++ b/index-test.php @@ -34,7 +34,7 @@ $config = yii\helpers\ArrayHelper::merge( // add more configurations here (is_readable(__DIR__ . '/protected/humhub/tests/codeception/config/dynamic.php')) ? require(__DIR__ . '/protected/humhub/tests/codeception/config/dynamic.php') : [], require(__DIR__ . '/protected/humhub/tests/codeception/config/acceptance.php'), - humhub\helpers\EnvHelper::toConfig($_ENV), + humhub\helpers\EnvHelper::toConfig($_ENV, humhub\components\console\Application::class), ); require_once './protected/vendor/codeception/codeception/autoload.php'; diff --git a/index.php b/index.php index 01d5f59212..cbf73d1b7b 100644 --- a/index.php +++ b/index.php @@ -20,13 +20,14 @@ defined('YII_ENV') or define('YII_ENV', $debug ? 'dev' : 'prod'); require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php'); + $config = yii\helpers\ArrayHelper::merge( require(__DIR__ . '/protected/humhub/config/common.php'), require(__DIR__ . '/protected/humhub/config/web.php'), require(__DIR__ . '/protected/config/common.php'), require(__DIR__ . '/protected/config/web.php'), $dynamicConfig, - humhub\helpers\EnvHelper::toConfig($_ENV), + humhub\helpers\EnvHelper::toConfig($_ENV, humhub\components\console\Application::class), ); try { diff --git a/protected/humhub/helpers/EnvHelper.php b/protected/humhub/helpers/EnvHelper.php index acd3c3c7ea..66e2b25c17 100644 --- a/protected/humhub/helpers/EnvHelper.php +++ b/protected/humhub/helpers/EnvHelper.php @@ -3,6 +3,7 @@ namespace humhub\helpers; use yii\base\InvalidArgumentException; +use yii\helpers\ArrayHelper; use yii\helpers\Inflector; use yii\helpers\Json; use yii\helpers\StringHelper; @@ -11,12 +12,13 @@ class EnvHelper { private const FIXED_SETTING_PREFIX = 'HUMHUB_FIXED_SETTINGS'; private const MAIN_PREFIX = 'HUMHUB_CONFIG'; + private const WEB_PREFIX = 'HUMHUB_WEB_CONFIG'; + private const CLI_PREFIX = 'HUMHUB_CLI_CONFIG'; 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 + public static function toConfig(?array $env = [], ?string $applicationType = null): array { $config = []; @@ -38,14 +40,22 @@ class EnvHelper $value, ); } - if (StringHelper::startsWith($key, self::MAIN_PREFIX . self::DEPTH_SEPARATOR)) { - ArrayHelper::setValue( - $config, - [ - ...self::keyToPath(str_replace(self::MAIN_PREFIX . self::DEPTH_SEPARATOR, '', $key)), - ], - $value, - ); + + foreach ( + ArrayHelper::getValue([ + \humhub\components\Application::class => [self::MAIN_PREFIX, self::WEB_PREFIX], + \humhub\components\console\Application::class => [self::MAIN_PREFIX, self::CLI_PREFIX], + ], $applicationType, [self::MAIN_PREFIX]) as $prefix + ) { + if (StringHelper::startsWith($key, $prefix . self::DEPTH_SEPARATOR)) { + ArrayHelper::setValue( + $config, + [ + ...self::keyToPath(str_replace($prefix . self::DEPTH_SEPARATOR, '', $key)), + ], + $value, + ); + } } } diff --git a/protected/humhub/modules/web/tests/codeception/unit/env/ConfigTest.php b/protected/humhub/modules/web/tests/codeception/unit/env/ConfigTest.php index 081b97521b..88e1afe164 100644 --- a/protected/humhub/modules/web/tests/codeception/unit/env/ConfigTest.php +++ b/protected/humhub/modules/web/tests/codeception/unit/env/ConfigTest.php @@ -112,4 +112,42 @@ class ConfigTest extends HumHubDbTestCase $this->assertEquals($config, EnvHelper::toConfig($ENV)); } + + public function testWebApplicationConfig() + { + $ENV = [ + 'HUMHUB_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'false', + 'HUMHUB_WEB_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'true', + 'HUMHUB_CLI_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'false', + ]; + + $config = [ + 'components' => [ + 'urlManager' => [ + 'showScriptName' => true, + ], + ], + ]; + + $this->assertEquals($config, EnvHelper::toConfig($ENV, \humhub\components\Application::class)); + } + + public function testConsoleApplicationConfig() + { + $ENV = [ + 'HUMHUB_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'false', + 'HUMHUB_CLI_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'true', + 'HUMHUB_WEB_CONFIG__COMPONENTS__URL_MANAGER__SHOW_SCRIPT_NAME' => 'false', + ]; + + $config = [ + 'components' => [ + 'urlManager' => [ + 'showScriptName' => true, + ], + ], + ]; + + $this->assertEquals($config, EnvHelper::toConfig($ENV, \humhub\components\console\Application::class)); + } } diff --git a/protected/yii b/protected/yii index a862d1a3b1..1e120c4e6f 100755 --- a/protected/yii +++ b/protected/yii @@ -31,7 +31,7 @@ $config = yii\helpers\ArrayHelper::merge( $dynamicConfig, require(__DIR__ . '/config/common.php'), require(__DIR__ . '/config/console.php'), - humhub\helpers\EnvHelper::toConfig($_ENV), + humhub\helpers\EnvHelper::toConfig($_ENV, \humhub\components\console\Application::class), ); try {