FIX: make artisan install-module really work with submodules

This commit is contained in:
Michal Kváček
2016-08-15 21:39:18 +02:00
parent bd06797c24
commit aa462c2454
2 changed files with 33 additions and 13 deletions
+23 -12
View File
@@ -16,7 +16,7 @@ class InstallModule extends Command
*
* @var string
*/
protected $signature = 'install-module {name}';
protected $signature = 'install-module {name} {parentModuleId?}';
/**
* The console command description.
*
@@ -24,6 +24,7 @@ class InstallModule extends Command
*/
protected $description = 'Custom installation of CMS module. Name given as directory name in /modules.';
protected $moduleName;
protected $parentModuleId;
protected $modulesPath;
/**
@@ -45,6 +46,7 @@ class InstallModule extends Command
public function handle()
{
$this->moduleName = $name = $this->argument('name');
$this->parentModuleId = $this->argument('parentModuleId');
// invalid module name provided
if (!is_dir($this->modulesPath . DIRECTORY_SEPARATOR . $name)) {
@@ -128,15 +130,19 @@ class InstallModule extends Command
$providers = isset($config['providers']) ? $config['providers'] : [];
// check if module is not installed yet
$exists = Module::where('handler', $handler)
->where('name', $config['name'])
->exists();
$module = Module::where('name', $name);
if (!isset($config['hasSubmodules']) || !$config['hasSubmodules']) {
if ($exists) {
throw new \Exception('Module ' . $config['name'] . ' already exists in database.');
}
if ($handler) {
$module = $module->where('handler', $handler);
}
$module = $module->first();
if ($module && (!isset($config['hasSubmodules']) || !$config['hasSubmodules'])) {
throw new \Exception('Module ' . $config['name'] . ' already exists in database.');
}
if (!$module) {
// insert module into database
$settings = [
'staticName' => $this->moduleName,
@@ -146,12 +152,12 @@ class InstallModule extends Command
'files' => isset($config['files']) ? $config['files'] : []
];
$module = Module::install($name, $alias, $description, $handler, $settings);
$module = Module::install($name, $alias, $description, $handler, $this->parentModuleId, $settings);
}
// try to install submodules
if (isset($config['hasSubmodules']) && $config['hasSubmodules']) {
$this->installSubmodules($this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName);
$this->installSubmodules($this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName, $module);
}
}
@@ -159,15 +165,20 @@ class InstallModule extends Command
* Crawl all subdirectories in module and try to install submodules
*
* @param $path string path to module base directory
* @param $parentModule
*/
private function installSubmodules($path)
private function installSubmodules($path, Module $parentModule)
{
$submodules = File::directories($path);
foreach ($submodules as $submodulePath) {
$submodule = str_replace($this->modulesPath . DIRECTORY_SEPARATOR, '', $submodulePath);
$this->call('install-module', ['name' => $submodule]);
try {
$this->call('install-module', ['name' => $submodule, 'parentModuleId' => $parentModule->id]);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}
}