FEATURE: make artisan command for installing work with submodules
This commit is contained in:
@@ -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 (!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' => $config['providers'],
|
||||
'providers' => $providers,
|
||||
'aliases' => isset($config['aliases']) ? $config['aliases'] : [],
|
||||
'order' => isset($config['order']) ? $config['order'] : 0,
|
||||
'files' => $config['files']
|
||||
|
||||
'files' => isset($config['files']) ? $config['files'] : []
|
||||
];
|
||||
$module = Module::install($name, $config['alias'], $description, $config['handler'], $settings);
|
||||
|
||||
return $module;
|
||||
$module = Module::install($name, $alias, $description, $handler, $settings);
|
||||
}
|
||||
|
||||
// try to install submodules
|
||||
if (isset($config['hasSubmodules']) && $config['hasSubmodules']) {
|
||||
$this->installSubmodules($this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
foreach ($submodules as $submodulePath) {
|
||||
$submodule = str_replace($this->modulesPath . DIRECTORY_SEPARATOR, '', $submodulePath);
|
||||
|
||||
$this->call('install-module', ['name' => $submodule]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
if ($handler) {
|
||||
call_user_func([$handler, 'install']);
|
||||
}
|
||||
|
||||
return $module;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user