refactored module commands

This commit is contained in:
Denis Duliçi 2020-06-11 23:32:13 +03:00
parent 72cfdf14b2
commit b54c9b1ecc
16 changed files with 395 additions and 353 deletions

View File

@ -0,0 +1,77 @@
<?php
namespace App\Abstracts\Commands;
use App\Models\Module\Module as Model;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
abstract class Module extends Command
{
protected function prepare()
{
$this->alias = Str::kebab($this->argument('alias'));
$this->company_id = $this->argument('company');
$this->locale = $this->argument('locale');
$this->module = module($this->alias);
}
protected function changeRuntime()
{
$this->old_company_id = session('company_id');
session(['company_id' => $this->company_id]);
app()->setLocale($this->locale);
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
}
protected function revertRuntime()
{
session()->forget('company_id');
if (!empty($this->old_company_id)) {
session(['company_id' => $this->old_company_id]);
}
}
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;
}
ModuleHistory::create([
'company_id' => $this->company_id,
'module_id' => $this->model->id,
'category' => $this->module->get('category'),
'version' => $this->module->get('version'),
'description' => trans('modules.' . $action, ['module' => $this->alias]),
]);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['alias', InputArgument::REQUIRED, 'Module alias.'],
['company', InputArgument::REQUIRED, 'Company ID.'],
];
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Console\Commands;
use App\Abstracts\Commands\Module;
class UninstallModule extends Module
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:uninstall {alias} {company} {locale=en-GB}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Uninstall the specified module.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->prepare();
if (!$this->getModel()) {
$this->info("Module [{$this->alias}] not found.");
return;
}
$this->changeRuntime();
// Delete db
$this->model->delete();
$this->createHistory('uninstalled');
event(new \App\Events\Module\Uninstalled($this->alias, $this->company_id));
// Delete files
$this->module->delete();
$this->revertRuntime();
$this->info("Module [{$this->alias}] uninstalled.");
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Events\Install;
use Illuminate\Queue\SerializesModels;
class UpdateCacheCleared
{
use SerializesModels;
public $company_id;
/**
* Create a new event instance.
*
* @param $company_id
*/
public function __construct($company_id)
{
$this->company_id = $company_id;
}
}

View File

@ -4,7 +4,7 @@ namespace App\Events\Module;
use Illuminate\Queue\SerializesModels;
class Deleted
class Copied
{
use SerializesModels;

View File

@ -0,0 +1,26 @@
<?php
namespace App\Events\Module;
use Illuminate\Queue\SerializesModels;
class Uninstalled
{
use SerializesModels;
public $alias;
public $company_id;
/**
* Create a new event instance.
*
* @param $alias
* @param $company_id
*/
public function __construct($alias, $company_id)
{
$this->alias = $alias;
$this->company_id = $company_id;
}
}

View File

@ -38,7 +38,7 @@ class Item extends Controller
$module = $this->getModule($alias);
if (empty($module)) {
return redirect('apps/home')->send();
return redirect()->route('apps.home.index')->send();
}
if ($this->moduleExists($alias) && ($model = Module::alias($alias)->first())) {
@ -169,102 +169,39 @@ class Item extends Controller
{
$json = $this->uninstallModule($alias);
$module = Module::alias($alias)->first();
$data = [
'company_id' => session('company_id'),
'module_id' => $module->id,
'category' => $json['data']['category'],
'version' => $json['data']['version'],
'description' => trans('modules.uninstalled', ['module' => $json['data']['name']]),
];
ModuleHistory::create($data);
$module->delete();
if ($json['success']) {
$message = trans('modules.uninstalled', ['module' => $json['data']['name']]);
flash($message)->success();
return redirect('apps/' . $alias)->send();
}
public function update($alias)
{
$json = $this->updateModule($alias);
$module = Module::alias($alias)->first();
$data = [
'company_id' => session('company_id'),
'module_id' => $module->id,
'category' => $json['data']['category'],
'version' => $json['data']['version'],
'description' => trans_choice('modules.updated', $json['data']['name']),
];
ModuleHistory::create($data);
$message = trans('modules.updated', ['module' => $json['data']['name']]);
flash($message)->success();
return redirect('apps/' . $alias)->send();
return redirect()->route('apps.app.show', $alias)->send();
}
public function enable($alias)
{
$json = $this->enableModule($alias);
$module = Module::alias($alias)->first();
$data = [
'company_id' => session('company_id'),
'module_id' => $module->id,
'category' => $json['data']['category'],
'version' => $json['data']['version'],
'description' => trans('modules.enabled', ['module' => $json['data']['name']]),
];
$module->enabled = 1;
$module->save();
ModuleHistory::create($data);
if ($json['success']) {
$message = trans('modules.enabled', ['module' => $json['data']['name']]);
flash($message)->success();
}
return redirect('apps/' . $alias)->send();
return redirect()->route('apps.app.show', $alias)->send();
}
public function disable($alias)
{
$json = $this->disableModule($alias);
$module = Module::alias($alias)->first();
$data = [
'company_id' => session('company_id'),
'module_id' => $module->id,
'category' => $json['data']['category'],
'version' => $json['data']['version'],
'description' => trans('modules.disabled', ['module' => $json['data']['name']]),
];
$module->enabled = 0;
$module->save();
ModuleHistory::create($data);
if ($json['success']) {
$message = trans('modules.disabled', ['module' => $json['data']['name']]);
flash($message)->success();
}
return redirect('apps/' . $alias)->send();
return redirect()->route('apps.app.show', $alias)->send();
}
public function reviews($alias, Request $request)
@ -292,11 +229,11 @@ class Item extends Controller
{
$documentation = $this->getDocumentation($alias);
if (empty($documentation)) {
return redirect('apps/' . $alias)->send();
}
$back = route('apps.app.show', $alias);
$back = 'apps/' . $alias;
if (empty($documentation)) {
return redirect()->route($back)->send();
}
return view('modules.item.documentation', compact('documentation', 'back'));
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Listeners\Module;
use Illuminate\Support\Facades\Cache;
class ClearCache
{
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle($event)
{
if (config('module.cache.enabled')) {
Cache::forget(config('module.cache.key'));
}
Cache::forget('apps.notifications');
Cache::forget('apps.suggestions');
Cache::forget('apps.installed.' . $event->company_id);
}
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $dispatcher
*/
public function subscribe($dispatcher)
{
$events = [
'App\Events\Install\UpdateCacheCleared',
'App\Events\Module\Copied',
'App\Events\Module\Enabled',
'App\Events\Module\Disabled',
'App\Events\Module\Uninstalled',
];
foreach ($events as $event) {
$dispatcher->listen($event, 'App\Listeners\Module\ClearCache@handle');
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Listeners\Module;
use App\Events\Module\Installed as Event;
use App\Traits\Permissions;
use Artisan;
class FinishInstallation
{
use Permissions;
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
$module = module($event->alias);
Artisan::call('migrate', ['--force' => true]);
$this->attachDefaultModulePermissions($module);
}
}

View File

@ -73,6 +73,9 @@ class Event extends Provider
'App\Events\Menu\PortalCreated' => [
'App\Listeners\Menu\AddPortalItems',
],
'App\Events\Module\Installed' => [
'App\Listeners\Module\FinishInstallation',
],
];
/**
@ -81,6 +84,7 @@ class Event extends Provider
* @var array
*/
protected $subscribe = [
'App\Listeners\Module\ClearCache',
'App\Listeners\Report\AddDate',
'App\Listeners\Report\AddAccounts',
'App\Listeners\Report\AddCustomers',

View File

@ -425,17 +425,11 @@ trait Modules
File::copyDirectory($temp_path, $module_path);
File::deleteDirectory($temp_path);
$data = [
'path' => $path,
'name' => Str::studly($module->alias),
'alias' => $module->alias,
];
event(new \App\Events\Module\Copied($module->alias, session('company_id')));
$company_id = session('company_id');
$locale = app()->getLocale();
$this->clearModulesCache();
$command = "module:install {$module->alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
@ -454,29 +448,44 @@ trait Modules
'redirect' => route('apps.app.show', $module->alias),
'error' => false,
'message' => null,
'data' => $data,
'data' => [
'path' => $path,
'name' => module($module->alias)->getName(),
'alias' => $module->alias,
],
];
}
public function uninstallModule($alias)
{
$module = module($alias);
$name = $module->getName();
$category = $module->get('category');
$version = $module->get('version');
$data = [
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:uninstall {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
$module->delete();
$this->clearModulesCache();
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => $data,
'data' => [
'name' => $name,
'category' => $category,
'version' => $version,
],
];
}
@ -484,21 +493,29 @@ trait Modules
{
$module = module($alias);
$data = [
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:enable {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
$module->enable();
$this->clearModulesCache();
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => $data,
'data' => [
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
],
];
}
@ -506,21 +523,29 @@ trait Modules
{
$module = module($alias);
$data = [
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:disable {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
$module->disable();
$this->clearModulesCache($alias);
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => $data,
'data' => [
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
],
];
}
@ -638,15 +663,4 @@ trait Modules
return $data['query']['page'];
}
public function clearModulesCache()
{
if (config('module.cache.enabled')) {
Cache::forget(config('module.cache.key'));
}
Cache::forget('apps.notifications');
Cache::forget('apps.suggestions');
Cache::forget('apps.installed.' . session('company_id'));
}
}

View File

@ -2,6 +2,7 @@
namespace App\Utilities;
use App\Events\Install\UpdateCacheCleared;
use App\Events\Install\UpdateCopied;
use App\Events\Install\UpdateDownloaded;
use App\Events\Install\UpdateUnzipped;
@ -22,9 +23,8 @@ class Updater
{
Cache::forget('updates');
Cache::forget('versions');
Cache::forget('apps.notifications');
Cache::forget('apps.suggestions');
Cache::forget('apps.installed.' . session('company_id'));
event(new UpdateCacheCleared(session('company_id')));
return true;
}

View File

@ -2,21 +2,16 @@
namespace Akaunting\Module\Commands;
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use App\Console\Commands\UninstallModule;
class DeleteCommand extends Command
class DeleteCommand extends UninstallModule
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:delete {alias} {company_id}';
protected $signature = 'module:delete {alias} {company} {locale=en-GB}';
/**
* The console command description.
@ -24,61 +19,4 @@ class DeleteCommand extends Command
* @var string
*/
protected $description = 'Delete the specified module.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$alias = Str::kebab($this->argument('alias'));
$company_id = $this->argument('company_id');
$model = Module::alias($alias)->companyId($company_id)->first();
if (!$model) {
$this->info("Module [{$alias}] not found.");
return;
}
$module = module($alias);
$module->delete();
$model->enabled = 0;
$model->save();
// Add history
$data = [
'company_id' => $company_id,
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
'version' => $module->get('version'),
'description' => trans('modules.deleted', ['module' => $module->get('alias')]),
];
ModuleHistory::create($data);
// Trigger event
event(new \App\Events\Module\Deleted($alias, $company_id));
if (config('module.cache.enabled')) {
Cache::forget(config('module.cache.key'));
}
$this->info("Module [{$alias}] deleted.");
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
}
}

View File

@ -2,20 +2,16 @@
namespace Akaunting\Module\Commands;
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use App\Abstracts\Commands\Module;
class DisableCommand extends Command
class DisableCommand extends Module
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:disable {alias} {company_id}';
protected $signature = 'module:disable {alias} {company} {locale=en-GB}';
/**
* The console command description.
@ -31,52 +27,30 @@ class DisableCommand extends Command
*/
public function handle()
{
$alias = Str::kebab($this->argument('alias'));
$company_id = $this->argument('company_id');
$this->prepare();
$model = Module::alias($alias)->companyId($company_id)->first();
if (!$model) {
$this->info("Module [{$alias}] not found.");
if (!$this->getModel()) {
$this->info("Module [{$this->alias}] not found.");
return;
}
if ($model->enabled == 1) {
$model->enabled = 0;
$model->save();
$module = module($alias);
// Add history
$data = [
'company_id' => $company_id,
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
'version' => $module->get('version'),
'description' => trans('modules.disabled', ['module' => $module->get('alias')]),
];
ModuleHistory::create($data);
// Trigger event
event(new \App\Events\Module\Disabled($alias, $company_id));
$this->info("Module [{$alias}] disabled.");
} else {
$this->comment("Module [{$alias}] is already disabled.");
}
if ($this->model->enabled == 0) {
$this->comment("Module [{$this->alias}] is already disabled.");
return;
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
$this->changeRuntime();
// Update db
$this->model->enabled = 0;
$this->model->save();
$this->createHistory('disabled');
event(new \App\Events\Module\Disabled($this->alias, $this->company_id));
$this->revertRuntime();
$this->info("Module [{$this->alias}] disabled.");
}
}

View File

@ -2,20 +2,16 @@
namespace Akaunting\Module\Commands;
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use App\Abstracts\Commands\Module;
class EnableCommand extends Command
class EnableCommand extends Module
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:enable {alias} {company_id}';
protected $signature = 'module:enable {alias} {company} {locale=en-GB}';
/**
* The console command description.
@ -31,52 +27,30 @@ class EnableCommand extends Command
*/
public function handle()
{
$alias = Str::kebab($this->argument('alias'));
$company_id = $this->argument('company_id');
$this->prepare();
$model = Module::alias($alias)->companyId($company_id)->first();
if (!$model) {
$this->info("Module [{$alias}] not found.");
if (!$this->getModel()) {
$this->info("Module [{$this->alias}] not found.");
return;
}
if ($model->enabled == 0) {
$model->enabled = 1;
$model->save();
$module = module($alias);
// Add history
$data = [
'company_id' => $company_id,
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
'version' => $module->get('version'),
'description' => trans('modules.enabled', ['module' => $module->get('alias')]),
];
ModuleHistory::create($data);
// Trigger event
event(new \App\Events\Module\Enabled($alias, $company_id));
$this->info("Module [{$alias}] enabled.");
} else {
$this->comment("Module [{$alias}] is already enabled.");
}
if ($this->model->enabled == 1) {
$this->comment("Module [{$this->alias}] is already enabled.");
return;
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
$this->changeRuntime();
// Update db
$this->model->enabled = 1;
$this->model->save();
$this->createHistory('enabled');
event(new \App\Events\Module\Enabled($this->alias, $this->company_id));
$this->revertRuntime();
$this->info("Module [{$this->alias}] enabled.");
}
}

View File

@ -2,18 +2,11 @@
namespace Akaunting\Module\Commands;
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use App\Traits\Permissions;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use App\Abstracts\Commands\Module;
use App\Models\Module\Module as Model;
class InstallCommand extends Command
class InstallCommand extends Module
{
use Permissions;
/**
* The name and signature of the console command.
*
@ -35,65 +28,23 @@ class InstallCommand extends Command
*/
public function handle()
{
$alias = Str::kebab($this->argument('alias'));
$company_id = $this->argument('company');
$locale = $this->argument('locale');
$this->prepare();
$old_company_id = session('company_id');
$this->changeRuntime();
session(['company_id' => $company_id]);
app()->setLocale($locale);
$module = module($alias);
$model = Module::create([
'company_id' => $company_id,
'alias' => $alias,
// Create db
$this->model = Model::create([
'company_id' => $this->company_id,
'alias' => $this->alias,
'enabled' => '1',
]);
ModuleHistory::create([
'company_id' => $company_id,
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
'version' => $module->get('version'),
'description' => trans('modules.installed', ['module' => $alias]),
]);
$this->createHistory('installed');
if (config('module.cache.enabled')) {
Cache::forget(config('module.cache.key'));
}
event(new \App\Events\Module\Installed($this->alias, $this->company_id));
// Disable model cache during installation
config(['laravel-model-caching.enabled' => false]);
// Update database
$this->call('migrate', ['--force' => true]);
event(new \App\Events\Module\Installed($alias, $company_id));
$this->attachDefaultModulePermissions($module);
session()->forget('company_id');
if (!empty($old_company_id)) {
session(['company_id' => $old_company_id]);
}
$this->revertRuntime();
$this->info('Module installed!');
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
}
}