Improve debug mode handling v2 (#7167)

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Autocommit PHP CS Fixer

* Improve Debug Mode Handling

* Autocommit PHP CS Fixer

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Improve Debug Mode Handling

* Autocommit PHP CS Fixer

* Updated Composer.locK

* Improve Debug Mode Handling

* Improve Debug Mode Handling

---------

Co-authored-by: gevorgmansuryan <gevorgmansuryan@users.noreply.github.com>
Co-authored-by: Lucas Bartholemy <luke-@users.noreply.github.com>
Co-authored-by: Lucas Bartholemy <lucas@bartholemy.com>
This commit is contained in:
Gevorg Mansuryan 2024-09-05 17:15:16 +04:00 committed by GitHub
parent ee6eb65425
commit 1f8a53f6bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 866 additions and 592 deletions

5
.env.example Normal file
View File

@ -0,0 +1,5 @@
# Warning: This file may contain sensitive information. Make sure that this file is not
# distributed via the web server and does not have file permissions that are too open.
HUMHUB_DEBUG=1
HUMHUB_CONFIG.PARAMS.MODULE_AUTOLOAD_PATHS=

3
.gitignore vendored
View File

@ -2,6 +2,7 @@ assets/*
!assets/.gitignore !assets/.gitignore
protected/runtime/* protected/runtime/*
.env
!protected/runtime/.gitignore !protected/runtime/.gitignore
node_modules node_modules
@ -42,4 +43,4 @@ favicon.ico
**/tests/codeception/_output/** **/tests/codeception/_output/**
/.php_cs.cache /.php_cs.cache
.DS_Store .DS_Store

View File

@ -25,6 +25,7 @@ HumHub Changelog
- Fix #7176: Fix of broken tests related to `Create Private Spaces` and `Create Public Spaces` Groups permissions - Fix #7176: Fix of broken tests related to `Create Private Spaces` and `Create Public Spaces` Groups permissions
- Enh #7198: Increase default Uplaod Max File Size to 64mb - Enh #7198: Increase default Uplaod Max File Size to 64mb
- Fix #7197: Increased version of `PHPOffice/PHPSpreadsheet` to v2.2+ - Fix #7197: Increased version of `PHPOffice/PHPSpreadsheet` to v2.2+
- Enh #7167: Disable DEBUG mode automatically after successful humhub installation. Add `.env` support
- Enh #7202: Increased minimum PHP version to 8.1 - Enh #7202: Increased minimum PHP version to 8.1
1.16.2 (Unreleased) 1.16.2 (Unreleased)

View File

@ -70,6 +70,7 @@
"symfony/sendgrid-mailer": "^5.4", "symfony/sendgrid-mailer": "^5.4",
"symfony/sendinblue-mailer": "^5.4", "symfony/sendinblue-mailer": "^5.4",
"twig/twig": "^3.4.1", "twig/twig": "^3.4.1",
"vlucas/phpdotenv": "^5.6",
"web-token/jwt-checker": ">=1.0 <3.0", "web-token/jwt-checker": ">=1.0 <3.0",
"web-token/jwt-signature": ">=1.0 <3.0", "web-token/jwt-signature": ">=1.0 <3.0",
"web-token/jwt-signature-algorithm-ecdsa": ">=1.0 <3.0", "web-token/jwt-signature-algorithm-ecdsa": ">=1.0 <3.0",

1247
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -27,10 +27,14 @@ defined('YII_ENV_TEST') or define('YII_ENV_TEST', true);
require(__DIR__ . '/protected/vendor/autoload.php'); require(__DIR__ . '/protected/vendor/autoload.php');
require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php'); require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '.env');
$dotenv->safeLoad();
$config = yii\helpers\ArrayHelper::merge( $config = yii\helpers\ArrayHelper::merge(
// add more configurations here // add more configurations here
(is_readable(__DIR__ . '/protected/humhub/tests/codeception/config/dynamic.php')) ? require(__DIR__ . '/protected/humhub/tests/codeception/config/dynamic.php') : [], (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') require(__DIR__ . '/protected/humhub/tests/codeception/config/acceptance.php'),
humhub\helpers\EnvHelper::toConfig($_ENV),
); );
require_once './protected/vendor/codeception/codeception/autoload.php'; require_once './protected/vendor/codeception/codeception/autoload.php';

View File

@ -6,28 +6,33 @@
* @license https://www.humhub.com/licences * @license https://www.humhub.com/licences
*/ */
// comment out the following two lines when deployed to production
use humhub\helpers\DatabaseHelper;
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/protected/vendor/autoload.php'); require(__DIR__ . '/protected/vendor/autoload.php');
require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '.env');
$dotenv->safeLoad();
$dynamicConfig = (is_readable(__DIR__ . '/protected/config/dynamic.php')) ? require(__DIR__ . '/protected/config/dynamic.php') : [];
$debug = !!($_ENV['HUMHUB_DEBUG'] ?? false) || !$dynamicConfig['params']['installed'];
defined('YII_DEBUG') or define('YII_DEBUG', $debug);
defined('YII_ENV') or define('YII_ENV', $debug ? 'dev' : 'prod');
require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge( $config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/protected/humhub/config/common.php'), require(__DIR__ . '/protected/humhub/config/common.php'),
require(__DIR__ . '/protected/humhub/config/web.php'), require(__DIR__ . '/protected/humhub/config/web.php'),
(is_readable(__DIR__ . '/protected/config/dynamic.php')) ? require(__DIR__ . '/protected/config/dynamic.php') : [],
require(__DIR__ . '/protected/config/common.php'), require(__DIR__ . '/protected/config/common.php'),
require(__DIR__ . '/protected/config/web.php') require(__DIR__ . '/protected/config/web.php'),
$dynamicConfig,
humhub\helpers\EnvHelper::toConfig($_ENV),
); );
try { try {
(new humhub\components\Application($config))->run(); (new humhub\components\Application($config))->run();
} catch (\Throwable $ex) { } catch (Throwable $e) {
if (null === DatabaseHelper::handleConnectionErrors($ex)) { if (null === humhub\helpers\DatabaseHelper::handleConnectionErrors($e)) {
throw $ex; throw $e;
} }
} }

View File

@ -8,14 +8,14 @@
namespace humhub\components; namespace humhub\components;
use humhub\libs\BaseSettingsManager;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerController; use humhub\modules\content\components\ContentContainerController;
use humhub\modules\content\components\ContentContainerSettingsManager;
use humhub\modules\space\models\Space; use humhub\modules\space\models\Space;
use humhub\modules\user\models\User; use humhub\modules\user\models\User;
use Throwable; use Throwable;
use Yii; use Yii;
use humhub\libs\BaseSettingsManager;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerSettingsManager;
/** /**
* SettingsManager application component * SettingsManager application component

View File

@ -0,0 +1,59 @@
<?php
namespace humhub\helpers;
use yii\base\InvalidArgumentException;
use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\helpers\StringHelper;
class EnvHelper
{
private const FIXED_SETTING_PREFIX = 'HUMHUB_FIXED_SETTINGS.';
private const MAIN_PREFIX = 'HUMHUB_CONFIG.';
private const FIXED_SETTINGS_PATH = ['params', 'fixed-settings'];
public static function toConfig(?array $env = []): array
{
$config = [];
foreach ($env as $key => $value) {
try {
$value = Json::decode($value);
} catch (InvalidArgumentException) {
}
if (StringHelper::startsWith($key, self::FIXED_SETTING_PREFIX)) {
ArrayHelper::setValue(
$config,
[
...self::FIXED_SETTINGS_PATH,
...self::keyToPath(str_replace(self::FIXED_SETTING_PREFIX, '', $key)),
],
$value,
);
}
if (StringHelper::startsWith($key, self::MAIN_PREFIX)) {
ArrayHelper::setValue(
$config,
[
...self::keyToPath(str_replace(self::MAIN_PREFIX, '', $key)),
],
$value,
);
}
}
return $config;
}
private static function keyToPath(string $key): array
{
return ArrayHelper::getColumn(
explode('.', $key),
function ($path) {
return Inflector::variablize(strtolower($path));
},
);
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace tests\codeception\unit\modules\web\env;
use humhub\helpers\EnvHelper;
use tests\codeception\_support\HumHubDbTestCase;
class ConfigTest extends HumHubDbTestCase
{
public function testDebugIgnored()
{
$ENV = [
'HUMHUB_DEBUG' => 1,
];
$this->assertEmpty(EnvHelper::toConfig($ENV));
}
public function testFixedSettings()
{
$ENV = [
'HUMHUB_FIXED_SETTINGS.BASE.MAILER.DSN' => 'smtp://...',
'HUMHUB_FIXED_SETTINGS.BASE.MAILER.TRANSPORT_TYPE' => 'php',
'HUMHUB_FIXED_SETTINGS.BASE.MAILER.SYSTEM_EMAIL_ADDRESS' => 'noreply@humhub.com',
];
$config = [
'params' => [
'fixed-settings' => [
'base' => [
'mailer' => [
'dsn' => 'smtp://...',
'transportType' => 'php',
'systemEmailAddress' => 'noreply@humhub.com',
],
],
],
],
];
$this->assertEquals($config, EnvHelper::toConfig($ENV));
}
public function testArrayConfig()
{
$ENV = [
'HUMHUB_CONFIG.PARAMS.MODULE_AUTOLOAD_PATHS' => ["/app/modules/humhub","/app/modules/humhub-contrib"],
];
$config = [
'params' => [
'moduleAutoloadPaths' => [
'/app/modules/humhub',
'/app/modules/humhub-contrib',
],
],
];
$this->assertEquals($config, EnvHelper::toConfig($ENV));
}
public function testJsonConfig()
{
$ENV = [
'HUMHUB_CONFIG.COMPONENTS.DB' => '{"on afterOpen":["humhub\\\libs\\\Helpers","SqlMode"]}',
];
$config = [
'components' => [
'db' => [
'on afterOpen' => ['humhub\libs\Helpers', 'SqlMode'],
],
],
];
$this->assertEquals($config, EnvHelper::toConfig($ENV));
}
}

View File

@ -8,30 +8,37 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
use humhub\helpers\DatabaseHelper; require(__DIR__ . '/vendor/autoload.php');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../', '.env');
$dotenv->safeLoad();
defined('YII_DEBUG') or define('YII_DEBUG', true); $dynamicConfig = (is_readable(__DIR__ . '/config/dynamic.php')) ? require(__DIR__ . '/config/dynamic.php') : [];
$debug = !!($_ENV['HUMHUB_DEBUG'] ?? false) || !$dynamicConfig['params']['installed'];
defined('YII_DEBUG') or define('YII_DEBUG', $debug);
// fcgi doesn't have STDIN and STDOUT defined by default // fcgi doesn't have STDIN and STDOUT defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w')); defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php'); require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge( $config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/humhub/config/common.php'), require(__DIR__ . '/humhub/config/common.php'),
require(__DIR__ . '/humhub/config/console.php'), require(__DIR__ . '/humhub/config/console.php'),
(is_readable(__DIR__ . '/config/dynamic.php')) ? require(__DIR__ . '/config/dynamic.php') : [], $dynamicConfig,
require(__DIR__ . '/config/common.php'), require(__DIR__ . '/config/common.php'),
require(__DIR__ . '/config/console.php') require(__DIR__ . '/config/console.php'),
humhub\helpers\EnvHelper::toConfig($_ENV),
); );
try { try {
$exitCode = (new humhub\components\console\Application($config))->run(); $exitCode = (new humhub\components\console\Application($config))->run();
exit($exitCode); exit($exitCode);
} catch (\Throwable $ex) { } catch (\Throwable $e) {
if (null === DatabaseHelper::handleConnectionErrors($ex)) { if (null === humhub\helpers\DatabaseHelper::handleConnectionErrors($e)) {
throw $ex; throw $e;
} }
} }