Refactoring

This commit is contained in:
Lucas Bartholemy 2025-01-08 13:05:29 +01:00
parent dba964da3f
commit 6dae731330
14 changed files with 58 additions and 48 deletions

View File

@ -8,6 +8,7 @@
namespace humhub\commands;
use humhub\components\InstallationState;
use Yii;
use yii\helpers\BaseConsole;
use yii\helpers\Console;
@ -43,9 +44,9 @@ class TestController extends \yii\console\Controller
public function actionDbConnection()
{
$this->stdout(PHP_EOL . 'DB Connection: ');
if (empty(Yii::$app->db->dsn) || empty(Yii::$app->db->username)) {
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
$this->stdout('Not Configured!', BaseConsole::FG_RED, BaseConsole::BOLD);
} elseif (Yii::$app->installationState->isDatabaseInstalled()) {
} elseif (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$this->stdout('OK!', BaseConsole::FG_GREEN, BaseConsole::BOLD);
} else {
$this->stdout('Failed!', BaseConsole::FG_RED, BaseConsole::BOLD);

View File

@ -64,7 +64,7 @@ trait ApplicationTrait
private function initLocales(): void
{
if ($this->installationState->isDatabaseInstalled()) {
if ($this->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
if ($this->settings instanceof SettingsManager) {
$this->timeZone = $this->settings->get('serverTimeZone', $this->timeZone);
if ($this->formatter instanceof Formatter) {
@ -121,12 +121,12 @@ trait ApplicationTrait
* Sets application in installed state (disables installer)
*
* @deprecated since 1.18
* @see InstallationState::setState()
* @see InstallationState::setInstalled()
* @since 1.16
*/
public function setInstalled()
{
$this->installationState->setState(InstallationState::STATE_INSTALLED);
$this->installationState->setInstalled();
}
@ -139,11 +139,7 @@ trait ApplicationTrait
*/
public function isDatabaseInstalled(bool $checkConnection = false): bool
{
if ($checkConnection) {
return $this->installationState->isDatabaseInstalled();
}
return $this->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED);
return $this->installationState->hasState(InstallationState::STATE_DATABASE_CREATED);
}

View File

@ -3,53 +3,74 @@
namespace humhub\components;
use humhub\helpers\DatabaseHelper;
use humhub\libs\DynamicConfig;
use Yii;
use yii\base\BaseObject;
use yii\base\StaticInstanceInterface;
use yii\base\StaticInstanceTrait;
class InstallationState extends BaseObject implements StaticInstanceInterface
final class InstallationState extends BaseObject implements StaticInstanceInterface
{
use StaticInstanceTrait;
/**
* The application is not installed.
* This state indicates that the installation process has not been started or completed.
* Condition: No database configuration is present.
*/
public const STATE_NOT_INSTALLED = 0;
/**
* The database is configured.
* This state indicates that the database configuration is complete and is valid, but the application may not be fully installed.
* Condition: A database configuration is present.
*/
public const STATE_DATABASE_CONFIGURED = 1 << 1;
public const STATE_DATABASE_CONFIGURED = 1;
/**
* The application is fully installed.
* This state indicates that the installation process is complete and the application is ready to use.
* The database is created.
* Condition: The database has been migrated (e.g. `settings` table exists)
*/
public const STATE_INSTALLED = self::STATE_DATABASE_CONFIGURED;
public const STATE_DATABASE_CREATED = 2;
/**
* The database is initialized.
* Condition: The admin user is created and the installation is complete.
*/
public const STATE_INSTALLED = 3;
private int $state;
public function init()
{
if (!YII_ENV_TEST && !DynamicConfig::exist()) {
try {
$this->state = (int)Yii::$app->settings->get(self::class, null);
} catch (\Exception $e) {
// Database seems not working
}
if ($this->state === self::STATE_INSTALLED) {
;
} elseif (empty(Yii::$app->db->dsn) || empty(Yii::$app->db->username)) {
$this->state = self::STATE_NOT_INSTALLED;
} elseif ($this->isDatabaseInstalled()) {
$this->state = self::STATE_DATABASE_CREATED;
} else {
$this->state = Yii::$app->settings->get(self::class, self::STATE_NOT_INSTALLED);
$this->state = self::STATE_DATABASE_CONFIGURED;
}
}
public function setState(int $state): void
public function setInstalled(): void
{
$this->setState(self::STATE_INSTALLED);
}
private function setState(int $state): void
{
$this->state = $state;
Yii::$app->settings->set(self::class, $this->state);
}
private function getState(): string
private function getState(): int
{
if ($this->state === self::STATE_NOT_INSTALLED) {
$this->init();
@ -60,23 +81,15 @@ class InstallationState extends BaseObject implements StaticInstanceInterface
public function hasState(int $state): bool
{
return ($this->getState() & $state) === $state;
return ($this->state >= $state);
}
public function isDatabaseInstalled(): bool
private function isDatabaseInstalled(): bool
{
$configExist = $this->hasState(self::STATE_DATABASE_CONFIGURED);
if (!$configExist) {
return false;
}
try {
Yii::$app->db->open();
} catch (\Exception $e) {
if ($configExist) {
DatabaseHelper::handleConnectionErrors($e);
}
DatabaseHelper::handleConnectionErrors($e);
return false;
}

View File

@ -118,14 +118,14 @@ class ModuleManager extends Component
parent::init();
// Either database installed and not in installed state
if (!Yii::$app->installationState->hasState(InstallationState::STATE_INSTALLED) && !Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (!Yii::$app->installationState->hasState(InstallationState::STATE_INSTALLED) && !Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
return;
}
if (Yii::$app->installationState->hasState(InstallationState::STATE_INSTALLED)) {
return;
}
if (!Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (!Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$this->enabledModules = [];
} else {
$this->enabledModules = ModuleEnabled::getEnabledIds();

View File

@ -41,7 +41,7 @@ class Application extends \yii\console\Application implements ApplicationInterfa
));
}
if ($this->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if ($this->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$baseUrl = $this->settings->get('baseUrl');
if (!empty($baseUrl)) {
if (Yii::getAlias('@web', false) === false) {

View File

@ -37,7 +37,7 @@ class UrlManager extends \humhub\components\UrlManager
private function getConfiguredBaseUrl()
{
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$baseUrl = Yii::$app->settings->get('baseUrl');
if (!empty($baseUrl)) {
return $baseUrl;

View File

@ -311,6 +311,6 @@ abstract class BaseSettingsManager extends Component
*/
public static function isDatabaseInstalled(): bool
{
return Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED);
return Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED);
}
}

View File

@ -44,7 +44,7 @@ class DynamicConfig extends BaseObject
return [];
}
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$validConfig = [
'components' => [
'db' => ArrayHelper::getValue($config, 'components.db', []),

View File

@ -91,7 +91,7 @@ class InstallController extends Controller
MigrationService::create()->migrateUp();
$this->stdout(" * Finishing\n", Console::FG_YELLOW);
Yii::$app->installationState->setState(Yii::$app->installationState::STATE_INSTALLED);
Yii::$app->installationState->setInstalled();
return ExitCode::OK;
}
@ -137,7 +137,7 @@ class InstallController extends Controller
Yii::$app->settings->set('mailer.systemEmailName', $site_email);
Yii::$app->settings->set('secret', UUID::v4());
Yii::$app->installationState->setState(Yii::$app->installationState::STATE_INSTALLED);
Yii::$app->installationState->setInstalled();
return ExitCode::OK;
}

View File

@ -564,7 +564,7 @@ class ConfigController extends Controller
Yii::$app->settings->set('defaultTimeZone', Yii::$app->timeZone);
// Set to installed
Yii::$app->installationState->setState(Yii::$app->installationState::STATE_INSTALLED);
Yii::$app->installationState->setInstalled();
try {
Yii::$app->user->logout();

View File

@ -35,7 +35,7 @@ class ThemeLoader implements BootstrapInterface
return;
}
if ($app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if ($app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$themePath = $app->settings->get('theme');
if (!empty($themePath) && is_dir($themePath)) {
$theme = ThemeHelper::getThemeByPath($themePath);

View File

@ -229,7 +229,7 @@ class Theme extends BaseTheme
return $this->parents;
}
if ($this->isActive() && Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if ($this->isActive() && Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
$this->parents = static::getActiveParents();
}
@ -244,7 +244,7 @@ class Theme extends BaseTheme
$parentPaths[] = $theme->getBasePath();
}
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
Yii::$app->settings->setSerialized('themeParents', $parentPaths);
}
}

View File

@ -59,7 +59,7 @@ class ThemeVariables extends Component
*/
public function get($key, $default = null)
{
if (!Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (!Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
return null;
}

View File

@ -1140,7 +1140,7 @@ class ModuleManagerTest extends HumHubDbTestCase
],
]);
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CONFIGURED)) {
if (Yii::$app->installationState->hasState(InstallationState::STATE_DATABASE_CREATED)) {
static::$moduleEnabledList ??= array_column(
static::dbSelect('module_enabled', 'module_id'),
'module_id',