Skip to content

Commit

Permalink
refactored module commands
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jun 11, 2020
1 parent 72cfdf1 commit b54c9b1
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 344 deletions.
77 changes: 77 additions & 0 deletions app/Abstracts/Commands/Module.php
@@ -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.'],
];
}
}
53 changes: 53 additions & 0 deletions app/Console/Commands/UninstallModule.php
@@ -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.");
}
}
22 changes: 22 additions & 0 deletions app/Events/Install/UpdateCacheCleared.php
@@ -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;
}
}
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Queue\SerializesModels;

class Deleted
class Copied
{
use SerializesModels;

Expand Down
26 changes: 26 additions & 0 deletions app/Events/Module/Uninstalled.php
@@ -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;
}
}
101 changes: 19 additions & 82 deletions app/Http/Controllers/Modules/Item.php
Expand Up @@ -38,7 +38,7 @@ public function show($alias)
$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())) {
Expand Down Expand Up @@ -169,102 +169,39 @@ public function uninstall($alias)
{
$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();

$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']]);
if ($json['success']) {
$message = trans('modules.uninstalled', ['module' => $json['data']['name']]);

flash($message)->success();
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);

$message = trans('modules.enabled', ['module' => $json['data']['name']]);
if ($json['success']) {
$message = trans('modules.enabled', ['module' => $json['data']['name']]);

flash($message)->success();
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);

$message = trans('modules.disabled', ['module' => $json['data']['name']]);
if ($json['success']) {
$message = trans('modules.disabled', ['module' => $json['data']['name']]);

flash($message)->success();
flash($message)->success();
}

return redirect('apps/' . $alias)->send();
return redirect()->route('apps.app.show', $alias)->send();
}

public function reviews($alias, Request $request)
Expand Down Expand Up @@ -292,12 +229,12 @@ public function documentation($alias)
{
$documentation = $this->getDocumentation($alias);

$back = route('apps.app.show', $alias);

if (empty($documentation)) {
return redirect('apps/' . $alias)->send();
return redirect()->route($back)->send();
}

$back = 'apps/' . $alias;

return view('modules.item.documentation', compact('documentation', 'back'));
}
}
45 changes: 45 additions & 0 deletions app/Listeners/Module/ClearCache.php
@@ -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');
}
}
}

0 comments on commit b54c9b1

Please sign in to comment.