FEATURE #1753: remove pingpong/modules and use custom solution
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+3
-13
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
namespace App\Console\Commands\Modules;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\File;
|
||||
@@ -12,7 +12,7 @@ class InstallAllModules extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'install-all-modules';
|
||||
protected $signature = 'module:install-all';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
@@ -20,16 +20,6 @@ class InstallAllModules extends Command
|
||||
*/
|
||||
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.
|
||||
*
|
||||
@@ -45,7 +35,7 @@ class InstallAllModules extends Command
|
||||
|
||||
// try to install each module
|
||||
try {
|
||||
$this->call('install-module', ['name' => $moduleName]);
|
||||
$this->call('module:install', ['name' => $moduleName]);
|
||||
} catch (\Exception $e) {
|
||||
$this->line('Module ' . $moduleName . ' already installed.');
|
||||
}
|
||||
+5
-5
@@ -1,8 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
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;
|
||||
@@ -17,7 +18,7 @@ class InstallModule extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'install-module {name} {parentModuleId?}';
|
||||
protected $signature = 'module:install {name} {parentModuleId?}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
@@ -156,12 +157,11 @@ class InstallModule extends Command
|
||||
$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, $this->parentModuleId, $settings);
|
||||
$module = Installer::install($name, $alias, $description, $handler, $this->parentModuleId, $settings);
|
||||
}
|
||||
|
||||
// try to install submodules
|
||||
@@ -184,7 +184,7 @@ class InstallModule extends Command
|
||||
$submodule = str_replace($this->modulesPath . DIRECTORY_SEPARATOR, '', $submodulePath);
|
||||
|
||||
try {
|
||||
$this->call('install-module', ['name' => $submodule, 'parentModuleId' => $parentModule->id]);
|
||||
$this->call('module:install', ['name' => $submodule, 'parentModuleId' => $parentModule->id]);
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Module;
|
||||
use App\ModuleManager\Installer;
|
||||
use App\ModuleManager\Uninstaller;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -43,14 +45,13 @@ class SetTemplate extends Command
|
||||
$config['staticName'] = $template;
|
||||
|
||||
if ($current) {
|
||||
$info = json_decode($current->module_settings);
|
||||
|
||||
$this->line('Template is already installed. Currently using template ' . trans($current->name) . ' located in /templates/' . $info->staticName . '.');
|
||||
$this->line('Template is already installed. Currently using template ' . trans($current->name) . ' located in /templates/' . $current->module_settings['staticName'] . '.');
|
||||
$confirm = $this->confirm('Uninstall it and install *' . trans($config['name']) . '*? All data associated with installed template will be lost!');
|
||||
|
||||
if ($confirm) {
|
||||
$this->line('Uninstalling ' . $current->name . '.');
|
||||
$current->uninstall();
|
||||
|
||||
Uninstaller::uninstall($current);
|
||||
} else {
|
||||
$this->line('Aborting installation.');
|
||||
return;
|
||||
@@ -58,7 +59,7 @@ class SetTemplate extends Command
|
||||
}
|
||||
|
||||
try {
|
||||
Module::install($config['name'], $config['alias'], $config['description'], $config['handler'], false, $config);
|
||||
Installer::install($config['name'], $config['alias'], $config['description'], $config['handler'], false, $config);
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Cannot install template ' . $template . '. Message: ' . $e->getMessage());
|
||||
return;
|
||||
@@ -72,6 +73,8 @@ class SetTemplate extends Command
|
||||
|
||||
// run again artisan for publishing resources
|
||||
// it cannot be done using $this->call, because we need to load different template into service providers
|
||||
exec('php artisan vendor:publish');
|
||||
if ($publish) {
|
||||
exec('php artisan vendor:publish');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Console\Commands\Modules\CreateNew;
|
||||
use App\Console\Commands\PublishTemplateResources;
|
||||
use App\Console\Commands\TemplateInfo;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
@@ -17,8 +18,9 @@ class Kernel extends ConsoleKernel
|
||||
protected $commands = [
|
||||
Commands\NewUser::class,
|
||||
Commands\ChangePassword::class,
|
||||
Commands\InstallModule::class,
|
||||
Commands\InstallAllModules::class,
|
||||
CreateNew::class,
|
||||
Commands\Modules\InstallModule::class,
|
||||
Commands\Modules\InstallAllModules::class,
|
||||
Commands\TranslateDirectory::class,
|
||||
Commands\TranslateAll::class,
|
||||
Commands\InitDatabaseSchema::class,
|
||||
@@ -37,7 +39,5 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
// $schedule->command('inspire')
|
||||
// ->hourly();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user