akaunting/app/Console/Commands/ModuleInstall.php

96 lines
2.3 KiB
PHP
Raw Normal View History

2017-11-01 19:43:42 +03:00
<?php
namespace App\Console\Commands;
2017-11-02 12:07:15 +03:00
use App\Events\ModuleInstalled;
2017-11-01 19:43:42 +03:00
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
2018-05-21 18:10:43 +03:00
use Symfony\Component\Console\Input\InputArgument;
2017-11-01 19:43:42 +03:00
class ModuleInstall extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2017-11-01 22:15:25 +03:00
protected $signature = 'module:install {alias} {company_id}';
2017-11-01 19:43:42 +03:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the specified module.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2018-12-20 17:35:28 +03:00
$alias = $this->argument('alias');
$company_id = $this->argument('company_id');
2018-12-25 16:49:37 +03:00
$old_company_id = session('company_id');
2018-12-20 17:35:28 +03:00
// Set company id
session(['company_id' => $company_id]);
2017-11-01 19:43:42 +03:00
$request = [
2018-12-20 17:35:28 +03:00
'company_id' => $company_id,
'alias' => strtolower($alias),
2017-11-01 19:43:42 +03:00
'status' => '1',
];
$model = Module::create($request);
2018-12-20 17:35:28 +03:00
$module = $this->laravel['modules']->findByAlias($alias);
2017-11-02 12:07:15 +03:00
// Add history
2017-11-01 19:43:42 +03:00
$data = [
'company_id' => $company_id,
2017-11-01 19:43:42 +03:00
'module_id' => $model->id,
'category' => $module->get('category'),
'version' => $module->get('version'),
2018-05-21 18:10:43 +03:00
'description' => trans('modules.installed', ['module' => $module->get('name')]),
2017-11-01 19:43:42 +03:00
];
ModuleHistory::create($data);
2018-11-15 11:04:30 +03:00
// Clear cache
$this->call('cache:clear');
2017-11-02 12:07:15 +03:00
// Update database
$this->call('migrate', ['--force' => true]);
// Trigger event
2018-12-20 17:35:28 +03:00
event(new ModuleInstalled($alias, $company_id));
// Unset company id
session()->forget('company_id');
2017-11-02 12:07:15 +03:00
2018-12-25 16:49:37 +03:00
// Set company id
if (!empty($old_company_id)) {
session(['company_id' => $old_company_id]);
}
2017-11-01 19:43:42 +03:00
$this->info('Module installed!');
}
2018-05-21 18:10:43 +03:00
/**
2018-05-21 18:17:54 +03:00
* Get the console command arguments.
*
* @return array
*/
2018-05-21 18:10:43 +03:00
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
}
2017-11-01 19:43:42 +03:00
}