Services\ModuleInstaller::installModule PHP Method

installModule() public method

Install the module
public installModule ( $file ) : array
$file
return array $input
    public function installModule($file)
    {
        @ini_set('max_execution_time', 0);
        // Temporarily increase maximum execution time
        if (is_file($file)) {
            $filename = $this->uploadModule($file);
            $canonical = str_replace('.zip', '', $filename);
            $unzipSuccess = $this->Unzip("{$this->temp_path}{$filename}", "{$this->temp_path}{$canonical}");
            if (!$unzipSuccess) {
                throw new Exception("The module file {$filename} couldn\\'t be extracted.");
            }
            $temp_module_dir = "{$this->temp_path}{$canonical}/";
            $replace_existing = (bool) Input::get('replace_existing');
        } else {
            $temp_module_dir = $file;
            $replace_existing = true;
        }
        $config_file = $temp_module_dir . 'module.json';
        if (!File::exists($config_file)) {
            throw new Exception('module.json doesn\'t exist in the module');
        }
        $this->config = json_decode(file_get_contents($config_file), true);
        if (Module::where('alias', '=', $this->config['info']['alias'])->first() && !$replace_existing) {
            throw new Exception('Another module with the same name already exists');
        }
        $this->checkIfRequiredModulesExist();
        // Copy modules from temporary folder to modules folder
        $this->copyModule($temp_module_dir);
        File::delete($file);
        $this->manageTables();
        $form_ids = $this->addToBuiltForms();
        if ($form_ids) {
            $this->addToBuiltModules($form_ids);
        }
        $input = $this->fixInput();
        return $input;
    }

Usage Example

Example #1
0
 /**
  * Install all modules bundled with the theme
  */
 protected function installModules()
 {
     if (!isset($this->config['modules'])) {
         return false;
     }
     $modules = $this->config['modules'];
     $module_installer = new ModuleInstaller();
     foreach ($modules as $module) {
         $module_dir = "{$this->full_path}modules/{$module}/";
         if (file_exists($module_dir) && file_exists($module_dir . 'module.json')) {
             $module_data = $module_installer->installModule($module_dir);
             if ($module = Module::where('alias', '=', $module_data['alias'])->first()) {
                 $module->update($module_data);
             } else {
                 $module = Module::create($module_data);
             }
         }
     }
 }