-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72cfdf1
commit b54c9b1
Showing
16 changed files
with
386 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
use Illuminate\Queue\SerializesModels; | ||
|
||
class Deleted | ||
class Copied | ||
{ | ||
use SerializesModels; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} |
Oops, something went wrong.