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; } }