From 53d532da823a4b9c03642e8ed81051835918f5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Mon, 15 Aug 2016 20:47:34 +0200 Subject: [PATCH] FEATURE: make artisan command for installing work with submodules --- app/Console/Commands/InstallModule.php | 70 ++++++++++++++++++++------ app/Models/Module.php | 8 +-- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/app/Console/Commands/InstallModule.php b/app/Console/Commands/InstallModule.php index 04d0e205..477ffb3c 100644 --- a/app/Console/Commands/InstallModule.php +++ b/app/Console/Commands/InstallModule.php @@ -4,6 +4,8 @@ namespace App\Console\Commands; use App\Models\Module; use Illuminate\Console\Command; +use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Support\Facades\File; use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; @@ -67,11 +69,25 @@ class InstallModule extends Command * yet. * * @return array + * @throws FileNotFoundException */ private function loadConfig() { + $basePath = $this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName . DIRECTORY_SEPARATOR; + $path = $basePath . 'Config' . DIRECTORY_SEPARATOR . 'config.php'; + + // check if module has Config/config.php file + if (!file_exists($path)) { + $path = $basePath . 'info.php'; + + // does module has info.php? If not, throw error, because we have no info about this module + if (!file_exists($path)) { + throw new FileNotFoundException('Missing config file. Create /Config/config.php or /info.php (for module with submodules) in ' . $basePath); + } + } + // 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'); + $config = @include($path); return $config; } @@ -83,7 +99,7 @@ class InstallModule extends Command */ private function checkMandatoryFields($config) { - $fields = ['providers', 'handler', 'name', 'files', 'alias']; + $fields = ['name']; foreach ($fields as $field) { if (!isset($config[$field])) { throw new MissingMandatoryParametersException('Missing *' . $field . '* in config file! Cannot install ' . $this->moduleName); @@ -107,25 +123,51 @@ class InstallModule extends Command { $name = isset($config['translatedName']) ? $config['translatedName'] : $config['name']; $description = isset($config['translatedDescription']) ? $config['translatedDescription'] : null; + $alias = isset($config['alias']) ? $config['alias'] : null; + $handler = isset($config['handler']) ? $config['handler'] : null; + $providers = isset($config['providers']) ? $config['providers'] : []; // check if module is not installed yet - $exists = Module::where('handler', $config['handler']) + $exists = Module::where('handler', $handler) + ->where('name', $config['name']) ->exists(); - if ($exists) { - throw new \Exception('Module ' . $config['name'] . ' already exists in database.'); + if (!isset($config['hasSubmodules']) || !$config['hasSubmodules']) { + if ($exists) { + throw new \Exception('Module ' . $config['name'] . ' already exists in database.'); + } + + // insert module into database + $settings = [ + 'staticName' => $this->moduleName, + 'providers' => $providers, + 'aliases' => isset($config['aliases']) ? $config['aliases'] : [], + 'order' => isset($config['order']) ? $config['order'] : 0, + 'files' => isset($config['files']) ? $config['files'] : [] + ]; + + $module = Module::install($name, $alias, $description, $handler, $settings); } - $settings = [ - 'staticName' => $this->moduleName, - 'providers' => $config['providers'], - 'aliases' => isset($config['aliases']) ? $config['aliases'] : [], - 'order' => isset($config['order']) ? $config['order'] : 0, - 'files' => $config['files'] + // try to install submodules + if (isset($config['hasSubmodules']) && $config['hasSubmodules']) { + $this->installSubmodules($this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName); + } + } - ]; - $module = Module::install($name, $config['alias'], $description, $config['handler'], $settings); + /** + * Crawl all subdirectories in module and try to install submodules + * + * @param $path string path to module base directory + */ + private function installSubmodules($path) + { + $submodules = File::directories($path); - return $module; + foreach ($submodules as $submodulePath) { + $submodule = str_replace($this->modulesPath . DIRECTORY_SEPARATOR, '', $submodulePath); + + $this->call('install-module', ['name' => $submodule]); + } } } diff --git a/app/Models/Module.php b/app/Models/Module.php index bde4288b..0a8300a8 100644 --- a/app/Models/Module.php +++ b/app/Models/Module.php @@ -2,10 +2,8 @@ namespace App\Models; -use App\Handlers\Contracts\AppHandler; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\App; -use Modules\Blog\Article\Handlers\Article; /** * Class Module @@ -44,7 +42,7 @@ class Module extends Model throw new \ErrorException('Modules can be installed only via Artisan!'); } - $managesContent = in_array('App\Handlers\Contracts\AppHandler', class_implements($handler)); + $managesContent = $handler ? in_array('App\Handlers\Contracts\AppHandler', class_implements($handler)) : false; $supportCategories = $managesContent ? call_user_func([$handler, 'supportCategories']) : false; // insert module into database @@ -60,7 +58,9 @@ class Module extends Model ]); // install something module-specific - call_user_func([$handler, 'install']); + if ($handler) { + call_user_func([$handler, 'install']); + } return $module; }