Akaunting/app/Abstracts/Commands/Module.php

87 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2020-06-11 20:32:13 +00:00
<?php
namespace App\Abstracts\Commands;
use App\Models\Module\Module as Model;
2020-06-12 08:51:37 +00:00
use App\Models\Module\ModuleHistory as ModelHistory;
2020-06-11 20:32:13 +00:00
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
abstract class Module extends Command
{
2023-11-03 14:05:40 +00:00
public string $alias;
public int $company_id;
public string $locale;
public object|null $model;
public int|null $old_company_id;
2020-06-11 20:32:13 +00:00
protected function prepare()
{
$this->alias = Str::kebab($this->argument('alias'));
2023-11-03 14:05:40 +00:00
$this->company_id = (int) $this->argument('company');
2020-06-11 20:32:13 +00:00
$this->locale = $this->argument('locale');
}
protected function changeRuntime()
{
2021-04-15 21:59:43 +00:00
$this->old_company_id = company_id();
2020-06-11 20:32:13 +00:00
2021-04-15 21:59:43 +00:00
company($this->company_id)->makeCurrent();
2020-06-11 20:32:13 +00:00
app()->setLocale($this->locale);
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
}
protected function revertRuntime()
{
2023-11-03 14:05:40 +00:00
if (empty($this->old_company_id)) {
return;
2020-06-11 20:32:13 +00:00
}
2023-11-03 14:05:40 +00:00
company($this->old_company_id)->makeCurrent();
2020-06-11 20:32:13 +00:00
}
protected function getModel()
{
$this->model = Model::companyId($this->company_id)->alias($this->alias)->first();
return $this->model;
}
protected function createHistory($action)
{
if (empty($this->model)) {
return;
}
2020-06-12 08:51:37 +00:00
ModelHistory::create([
2020-06-11 20:32:13 +00:00
'company_id' => $this->company_id,
'module_id' => $this->model->id,
2023-11-03 14:05:40 +00:00
'version' => module($this->alias)->get('version'),
2020-06-11 20:32:13 +00:00
'description' => trans('modules.' . $action, ['module' => $this->alias]),
2021-09-07 07:33:34 +00:00
'created_from' => source_name(),
'created_by' => user_id(),
2020-06-11 20:32:13 +00:00
]);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['alias', InputArgument::REQUIRED, 'Module alias.'],
['company', InputArgument::REQUIRED, 'Company ID.'],
];
}
}