env application type (#7353)

* ENV Vars - Web Only #481

* ENV Vars - Web Only #481

* ENV Vars - Web Only #481

* ENV Vars - Web Only #481

* ENV Vars - Web Only #481
This commit is contained in:
Gevorg Mansuryan 2024-12-18 18:33:54 +04:00 committed by GitHub
parent 39dcfa545e
commit 1738937102
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 13 deletions

View File

@ -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)
---------------------------------

View File

@ -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';

View File

@ -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 {

View File

@ -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,
);
}
}
}

View File

@ -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));
}
}

View File

@ -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 {