FEATURE #1753: remove pingpong/modules and use custom solution

This commit is contained in:
Michal Kváček
2017-07-31 15:32:33 +02:00
parent f876a08429
commit 72c64dc6d1
48 changed files with 1230 additions and 1156 deletions
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace App\Console\Commands\Modules;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class CreateNew extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'module:new
{name : module name}
{parentModule? : name of parent module}
{--entity= : entity}';
/**
* The console command description.
* @var string
*/
protected $description = 'Create new CMS module.';
protected $moduleName;
protected $moduleNamespace;
protected $entity;
protected $entityNamespace;
/**
* Execute the console command.
*/
public function handle()
{
$defaultEntityNamespace = $this->loadArguments();
$path = preg_replace('|/{2,}|i', '/', config('modules.paths.modules') . '/' . $this->moduleNamespace);
if (is_dir($path)) {
$this->error('Module already exists in ' . $path);
return;
}
// copy stub files
\File::copyDirectory(config('modules.paths.stubs'), $path);
$files = \File::allFiles($path);
foreach ($files as $file) {
$newName = str_replace('stub', 'php', $this->replacePlaceholders($file->getPathname()));
// ignore new entity when using existing entity
if ($this->entityNamespace != $defaultEntityNamespace && Str::contains('Entities', $file)) {
\File::delete($file);
continue;
}
// rename file
\File::move($file, $newName);
// change its content
$content = file_get_contents($newName);
$content = $this->replacePlaceholders($content);
file_put_contents($newName, $content);
}
$this->info('Module successfully created.');
}
/**
* Find and replace all placeholders in given content
*
* @param $content
*
* @return mixed
*/
private function replacePlaceholders($content)
{
$replace = [
'$Module$' => $this->moduleName,
'$ModuleNamespace$' => str_replace('/', '\\', $this->moduleNamespace),
'$module$' => strtolower($this->moduleName),
'$EntityNamespace$' => $this->entityNamespace,
'$Entity$' => $this->entity
];
foreach ($replace as $what => $for) {
$content = str_replace($what, $for, $content);
}
return $content;
}
/**
* Load command arguments
* @return string
*/
private function loadArguments()
{
$parent = $this->argument('parentModule');
$this->moduleName = $moduleName = $this->argument('name');
$this->moduleNamespace = trim($parent . '/' . $moduleName, '/');
$entity = $this->option('entity');
$defaultEntityNamespace = '\\Modules\\' . str_replace('/', '\\', $this->moduleNamespace) . '\Entities\\';
if (!empty($entity)) {
$parts = explode('\\', $entity);
$this->entity = $parts[count($parts) - 1];
unset($parts[count($parts) - 1]);
$this->entityNamespace = implode('\\', $parts);
} else {
$this->entityNamespace = $defaultEntityNamespace;
$this->entity = $this->moduleName;
}
return $defaultEntityNamespace;
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Console\Commands\Modules;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class InstallAllModules extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:install-all';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Crawl /modules directory and install all modules';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// crawl all subdirectories in modules path
$modules = File::directories(config('modules.paths.modules'));
foreach ($modules as $module) {
$moduleName = basename($module);
// try to install each module
try {
$this->call('module:install', ['name' => $moduleName]);
} catch (\Exception $e) {
$this->line('Module ' . $moduleName . ' already installed.');
}
}
}
}
@@ -0,0 +1,193 @@
<?php
namespace App\Console\Commands\Modules;
use App\Models\Module;
use App\ModuleManager\Installer;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
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 = 'module:install {name} {parentModuleId?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Custom installation of CMS module. Name given as directory name in /modules.';
protected $moduleName;
protected $parentModuleId;
protected $modulesPath;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->modulesPath = config('modules.paths.modules');
}
/**
* Execute the console 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)) {
throw new InvalidArgumentException('Invalid module ' . $name . '. No directory under ' . $this->modulesPath . DIRECTORY_SEPARATOR . $name . ' found!');
}
// get config
$config = $this->loadConfig();
if (empty($config))
return;
// check if it is valid
$this->checkMandatoryFields($config);
// all mandatory fields are present, we can write it into database and create modules.json
$this->installModule($config);
// delete cache
Cache::forget('enabled-modules');
Cache::forget('module-handler-to-id-enumeration');
// inform user
$this->line('Module ' . $name . ' successfuly installed');
}
/**
* Load config from Config/config.php in module files. We cannot use config() because the file have not published
* yet.
*
* @return array
* @throws FileNotFoundException
*/
private function loadConfig()
{
$basePath = $this->modulesPath . DIRECTORY_SEPARATOR . $this->moduleName . DIRECTORY_SEPARATOR;
$path = $basePath . 'Config' . DIRECTORY_SEPARATOR . 'config.php';
if (basename($basePath) == 'Database')
return [];
// 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($path);
return $config;
}
/**
* Checks if config contains all mandatory entries.
*
* @param $config
*/
private function checkMandatoryFields($config)
{
$fields = ['name'];
foreach ($fields as $field) {
if (!isset($config[$field])) {
throw new MissingMandatoryParametersException('Missing *' . $field . '* in config file! Cannot install ' . $this->moduleName);
}
}
}
/**
* Installs module into database.
*
* This method calls Module::install, which handles specific installation of module. In this way we should be able
* to install any module even with some other database structures.
*
* @param $config
*
* @return Module
* @throws \ErrorException
* @throws \Exception
*/
private function installModule($config)
{
$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
$module = Module::where('name', $name);
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 (is_null($module)) {
// insert module into database
$settings = [
'staticName' => $this->moduleName,
'providers' => $providers,
'order' => isset($config['order']) ? $config['order'] : 0,
'files' => isset($config['files']) ? $config['files'] : []
];
$module = Installer::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, $module);
}
}
/**
* Crawl all subdirectories in module and try to install submodules
*
* @param $path string path to module base directory
* @param $parentModule
*/
private function installSubmodules($path, Module $parentModule)
{
$submodules = File::directories($path);
foreach ($submodules as $submodulePath) {
$submodule = str_replace($this->modulesPath . DIRECTORY_SEPARATOR, '', $submodulePath);
try {
$this->call('module:install', ['name' => $submodule, 'parentModuleId' => $parentModule->id]);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}
}