diff --git a/app/Console/Commands/InstallAllModules.php b/app/Console/Commands/InstallAllModules.php new file mode 100644 index 00000000..1cb87c4b --- /dev/null +++ b/app/Console/Commands/InstallAllModules.php @@ -0,0 +1,53 @@ +call('install-module', ['name' => $moduleName]); + } catch (\Exception $e) { + $this->line('Module ' . $moduleName . ' already installed.'); + } + } + } +} diff --git a/app/Console/Commands/InstallModule.php b/app/Console/Commands/InstallModule.php new file mode 100644 index 00000000..f45a5620 --- /dev/null +++ b/app/Console/Commands/InstallModule.php @@ -0,0 +1,105 @@ +modulesPath = config('modules.paths.modules'); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $name = $this->argument('name'); + $this->moduleName = $name; + + if (!is_dir($this->modulesPath . DIRECTORY_SEPARATOR . $name)) { + throw new InvalidArgumentException('Invalid module ' . $name . '. No directory under ' . $this->modulesPath . DIRECTORY_SEPARATOR . $name . ' found!'); + } + + $config = $this->loadConfig(); + + $this->checkMandatoryFields($config); + + // all mandatory fields are present, we can write it into database and create modules.json + $this->installModule($config); + + $this->line('Module ' . $name . ' successfuly installed'); + } + + private function loadConfig() + { + // we cannot rely on config() helper, because this module does not have to be published + $config = @include($this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'config.php'); + + return $config; + } + + private function checkMandatoryFields($config) + { + $fields = ['providers', 'handler', 'name', 'files', 'alias']; + foreach ($fields as $field) { + if (!isset($config[$field])) { + throw new MissingMandatoryParametersException('Missing *' . $field . '* in config file! Cannot install ' . $this->moduleName); + } + } + } + + private function installModule($config) + { + $name = isset($config['translatedName']) ? $config['translatedName'] : $config['name']; + $description = isset($config['translatedDescription']) ? $config['translatedDescription'] : null; + + // check if module is not installed yet + $exists = Module::where('handler', $config['handler']) + ->exists(); + + if ($exists) { + throw new \Exception('Module ' . $config['name'] . ' already exists in database.'); + } + + $settings = [ + 'staticName' => $this->moduleName, + 'providers' => $config['providers'], + 'aliases' => isset($config['aliases']) ? $config['aliases'] : [], + 'order' => isset($config['order']) ? $config['order'] : 0, + 'files' => $config['files'] + + ]; + $module = Module::install($name, $config['alias'], $description, $config['handler'], $settings); + + return $module; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 53a0ead5..f85bfe13 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,13 +14,16 @@ class Kernel extends ConsoleKernel */ protected $commands = [ Commands\NewUser::class, - Commands\ChangePassword::class + Commands\ChangePassword::class, + Commands\InstallModule::class, + Commands\InstallAllModules::class, ]; /** * Define the application's command schedule. * - * @param \Illuminate\Console\Scheduling\Schedule $schedule + * @param \Illuminate\Console\Scheduling\Schedule $schedule + * * @return void */ protected function schedule(Schedule $schedule)