diff --git a/app/Console/Stubs/Modules/json.stub b/app/Console/Stubs/Modules/json.stub index 4f9b5b754..dae1e040a 100644 --- a/app/Console/Stubs/Modules/json.stub +++ b/app/Console/Stubs/Modules/json.stub @@ -12,5 +12,6 @@ "requires": [], "reports": [], "widgets": [], - "settings": [] + "settings": [], + "extra-modules": {} } diff --git a/app/Events/Module/Installed.php b/app/Events/Module/Installed.php index cce16aa9c..9f9baa597 100644 --- a/app/Events/Module/Installed.php +++ b/app/Events/Module/Installed.php @@ -10,15 +10,19 @@ class Installed extends Event public $company_id; + public $locale; + /** * Create a new event instance. * * @param $alias * @param $company_id + * @param $locale */ - public function __construct($alias, $company_id) + public function __construct($alias, $company_id, $locale) { $this->alias = $alias; $this->company_id = $company_id; + $this->locale = $locale; } } diff --git a/app/Jobs/Install/DownloadModule.php b/app/Jobs/Install/DownloadModule.php new file mode 100644 index 000000000..996c4b2dd --- /dev/null +++ b/app/Jobs/Install/DownloadModule.php @@ -0,0 +1,41 @@ +alias = $alias; + $this->company_id = $company_id ?: session('company_id'); + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + $command = "module:download {$this->alias} {$this->company_id}"; + + $result = Console::run($command); + + if ($result !== true) { + throw new \Exception($result); + } + } +} diff --git a/app/Listeners/Module/InstallExtraModules.php b/app/Listeners/Module/InstallExtraModules.php new file mode 100644 index 000000000..0ed953f12 --- /dev/null +++ b/app/Listeners/Module/InstallExtraModules.php @@ -0,0 +1,48 @@ +alias); + + $extra_modules = $module->get('extra-modules'); + + if (empty($extra_modules)) { + return; + } + + foreach ($extra_modules as $alias => $level) { + // Don't install if the module is "suggested" + if ($level != 'required') { + continue; + } + + try { + $this->dispatch(new DownloadModule($alias, $event->company_id)); + + $this->dispatch(new InstallModule($alias, $event->company_id, $event->locale)); + } catch (\Exception $e) { + logger($e->getMessage()); + + // Stop the propagation of event if the required module failed to install + return false; + } + } + } +} diff --git a/app/Providers/Event.php b/app/Providers/Event.php index b7b216781..7981a1051 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -69,6 +69,7 @@ class Event extends Provider 'App\Listeners\Menu\AddPortalItems', ], 'App\Events\Module\Installed' => [ + 'App\Listeners\Module\InstallExtraModules', 'App\Listeners\Module\FinishInstallation', ], ]; diff --git a/modules/BC21/module.json b/modules/BC21/module.json index 2dfb292ae..f68c21684 100644 --- a/modules/BC21/module.json +++ b/modules/BC21/module.json @@ -11,5 +11,6 @@ "requires": [], "reports": [], "widgets": [], - "settings": [] + "settings": [], + "extra-modules": {} } diff --git a/modules/OfflinePayments/module.json b/modules/OfflinePayments/module.json index 391fec329..7bec9c773 100644 --- a/modules/OfflinePayments/module.json +++ b/modules/OfflinePayments/module.json @@ -12,5 +12,6 @@ "requires": [], "reports": [], "widgets": [], - "settings": [] + "settings": [], + "extra-modules": {} } diff --git a/modules/PaypalStandard/module.json b/modules/PaypalStandard/module.json index ec78a84fc..2d2e776aa 100644 --- a/modules/PaypalStandard/module.json +++ b/modules/PaypalStandard/module.json @@ -97,5 +97,6 @@ "attributes": {}, "rules": "required|integer" } - ] + ], + "extra-modules": {} } diff --git a/overrides/akaunting/module/Commands/InstallCommand.php b/overrides/akaunting/module/Commands/InstallCommand.php index a49d4cb5f..60e3c0320 100644 --- a/overrides/akaunting/module/Commands/InstallCommand.php +++ b/overrides/akaunting/module/Commands/InstallCommand.php @@ -47,7 +47,7 @@ class InstallCommand extends Command $this->createHistory('installed'); - event(new Installed($this->alias, $this->company_id)); + event(new Installed($this->alias, $this->company_id, $this->locale)); $this->revertRuntime();