fixed module installation

This commit is contained in:
Denis Duliçi 2023-11-03 17:05:40 +03:00
parent 1d886b8810
commit 811cb36a03
6 changed files with 49 additions and 33 deletions

View File

@ -10,13 +10,21 @@ use Symfony\Component\Console\Input\InputArgument;
abstract class Module extends Command abstract class Module extends Command
{ {
public string $alias;
public int $company_id;
public string $locale;
public object|null $model;
public int|null $old_company_id;
protected function prepare() protected function prepare()
{ {
$this->alias = Str::kebab($this->argument('alias')); $this->alias = Str::kebab($this->argument('alias'));
$this->company_id = $this->argument('company'); $this->company_id = (int) $this->argument('company');
$this->locale = $this->argument('locale'); $this->locale = $this->argument('locale');
$this->module = module($this->alias);
} }
protected function changeRuntime() protected function changeRuntime()
@ -33,11 +41,11 @@ abstract class Module extends Command
protected function revertRuntime() protected function revertRuntime()
{ {
session()->forget('company_id'); if (empty($this->old_company_id)) {
return;
if (!empty($this->old_company_id)) {
company($this->old_company_id)->makeCurrent();
} }
company($this->old_company_id)->makeCurrent();
} }
protected function getModel() protected function getModel()
@ -56,7 +64,7 @@ abstract class Module extends Command
ModelHistory::create([ ModelHistory::create([
'company_id' => $this->company_id, 'company_id' => $this->company_id,
'module_id' => $this->model->id, 'module_id' => $this->model->id,
'version' => $this->module->get('version'), 'version' => module($this->alias)->get('version'),
'description' => trans('modules.' . $action, ['module' => $this->alias]), 'description' => trans('modules.' . $action, ['module' => $this->alias]),
'created_from' => source_name(), 'created_from' => source_name(),
'created_by' => user_id(), 'created_by' => user_id(),

View File

@ -45,7 +45,7 @@ class UninstallModule extends Command
event(new Uninstalled($this->alias, $this->company_id)); event(new Uninstalled($this->alias, $this->company_id));
// Delete files // Delete files
$this->module->delete(); module($this->alias)->delete();
$this->revertRuntime(); $this->revertRuntime();

View File

@ -36,26 +36,11 @@ class ModuleActivator implements ActivatorInterface
return true; return true;
} }
$alias = $module->getAlias(); if (! isset($this->statuses[$module->getAlias()])) {
return $active;
if (! isset($this->statuses[$alias])) {
if (empty($this->company_id)) {
$company_id = $this->getCompanyId();
if (empty($company_id)) {
return false;
}
$this->company_id = $company_id;
}
$model = Model::companyId($this->company_id)->alias($alias)->first();
$status = $model ? $model->enabled : $active;
$this->setActive($module, $status);
} }
return $this->statuses[$alias] === $active; return $this->statuses[$module->getAlias()] === $active;
} }
public function enable(Module $module): void public function enable(Module $module): void
@ -72,15 +57,33 @@ class ModuleActivator implements ActivatorInterface
{ {
$this->statuses[$module->getAlias()] = $active; $this->statuses[$module->getAlias()] = $active;
Model::updateOrCreate([ $this->flushCache();
if (empty($this->company_id)) {
$company_id = $this->getCompanyId();
if (empty($company_id)) {
return;
}
$this->company_id = $company_id;
}
$model = Model::companyId($this->company_id)->alias($module->getAlias())->first();
if (! empty($model)) {
$model->enabled = $active;
$model->save();
return;
}
Model::create([
'company_id' => $this->company_id, 'company_id' => $this->company_id,
'alias' => $module->getAlias(), 'alias' => $module->getAlias(),
], [
'enabled' => $active, 'enabled' => $active,
'created_from' => 'core::activator', 'created_from' => 'core::activator',
]); ]);
$this->flushCache();
} }
public function delete(Module $module): void public function delete(Module $module): void

View File

@ -30,13 +30,15 @@ class DisableCommand extends Command
{ {
$this->prepare(); $this->prepare();
if (!$this->getModel()) { if (! $this->getModel()) {
$this->info("Module [{$this->alias}] not found."); $this->info("Module [{$this->alias}] not found.");
return; return;
} }
if (!$this->model->enabled) { if (!$this->model->enabled) {
$this->comment("Module [{$this->alias}] is already disabled."); $this->comment("Module [{$this->alias}] is already disabled.");
return; return;
} }

View File

@ -30,13 +30,15 @@ class EnableCommand extends Command
{ {
$this->prepare(); $this->prepare();
if (!$this->getModel()) { if (! $this->getModel()) {
$this->info("Module [{$this->alias}] not found."); $this->info("Module [{$this->alias}] not found.");
return; return;
} }
if ($this->model->enabled) { if ($this->model->enabled) {
$this->comment("Module [{$this->alias}] is already enabled."); $this->comment("Module [{$this->alias}] is already enabled.");
return; return;
} }

View File

@ -33,6 +33,7 @@ class InstallCommand extends Command
if ($this->getModel()) { if ($this->getModel()) {
$this->comment("Module [{$this->alias}] is already installed."); $this->comment("Module [{$this->alias}] is already installed.");
return; return;
} }