FEATURE: add commands for installing modules

This commit is contained in:
Michal Kváček
2016-03-14 13:15:43 +01:00
parent 6d13576185
commit e7c5a69f81
3 changed files with 163 additions and 2 deletions
@@ -0,0 +1,53 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
class InstallAllModules extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'install-all-modules';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Crawl /modules directory and install all modules';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$modules = File::directories(config('modules.paths.modules'));
foreach ($modules as $module) {
$moduleName = basename($module);
try {
$this->call('install-module', ['name' => $moduleName]);
} catch (\Exception $e) {
$this->line('Module ' . $moduleName . ' already installed.');
}
}
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace App\Console\Commands;
use App\Models\Module;
use Illuminate\Console\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
class InstallModule extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'install-module {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Custom installation of CMS module. Name given as directory name in /modules.';
protected $moduleName;
protected $modulesPath;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->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;
}
}
+5 -2
View File
@@ -14,13 +14,16 @@ class Kernel extends ConsoleKernel
*/ */
protected $commands = [ protected $commands = [
Commands\NewUser::class, Commands\NewUser::class,
Commands\ChangePassword::class Commands\ChangePassword::class,
Commands\InstallModule::class,
Commands\InstallAllModules::class,
]; ];
/** /**
* Define the application's command schedule. * Define the application's command schedule.
* *
* @param \Illuminate\Console\Scheduling\Schedule $schedule * @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void * @return void
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)