FEATURE #2520: add commands for creating new migration and running migrations in modules
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
class $MigrationName$
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$TableName$;
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\Modules;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CreateMigration extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'module:make-migration {module?} {name?} {--t|table= : Table name}';
|
||||
/**
|
||||
* The console command description.
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Make new migration for given module';
|
||||
protected $migrationName;
|
||||
protected $tableName;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$module = $this->argument('module');
|
||||
$this->migrationName = $name = $this->argument('name');
|
||||
$this->tableName = $this->option('table');
|
||||
|
||||
$modules = Module::where('enabled', true)
|
||||
->get()
|
||||
->pluck('static_name', 'id')
|
||||
->toArray();
|
||||
|
||||
while (is_null($module) || !in_array($module, $modules)) {
|
||||
$module = $this->anticipate('Input valid module name', $modules);
|
||||
}
|
||||
|
||||
$selectedModule = Module::where('static_name', $module)
|
||||
->first();
|
||||
|
||||
$basePath = $selectedModule->is_template ? config('modules.path.template') : config('modules.paths.modules');
|
||||
$path = $basePath . '/' . $module . '/Database/Migrations/';
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0777, true);
|
||||
}
|
||||
|
||||
while (is_null($name)) {
|
||||
$name = $this->ask('Input migration name');
|
||||
}
|
||||
|
||||
$content = file_get_contents(config('modules.paths.stubs') . '/Migrations/new.stub');
|
||||
$content = $this->replacePlaceholders($content);
|
||||
|
||||
$migrationName = $path . $this->getMigrationName($name);
|
||||
|
||||
file_put_contents($migrationName, $content);
|
||||
|
||||
$this->line('Migration successfully created.', 'info');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and replace all placeholders in given content
|
||||
*
|
||||
* @param $content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function replacePlaceholders($content)
|
||||
{
|
||||
$replace = [
|
||||
'$MigrationName$' => studly_case($this->migrationName),
|
||||
'$TableName$' => $this->tableName
|
||||
];
|
||||
|
||||
foreach ($replace as $what => $for) {
|
||||
$content = str_replace($what, $for, $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get prefix for created migration
|
||||
* @return false|string
|
||||
*/
|
||||
protected function getDatePrefix()
|
||||
{
|
||||
return date('Y_m_d_His');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getMigrationName($name)
|
||||
{
|
||||
return $this->getDatePrefix() . '_' . snake_case($name) . '.php';
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ class CreateNew extends Command
|
||||
mkdir($path);
|
||||
|
||||
// copy stub files
|
||||
\File::copyDirectory(config('modules.paths.stubs'), $path);
|
||||
\File::copyDirectory(config('modules.paths.stubs').'/NewModule', $path);
|
||||
|
||||
$files = \File::allFiles($path);
|
||||
foreach ($files as $file) {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\Modules;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class RunMigrations extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'module:migrate {module?} {--rollback : Rollback one migration}';
|
||||
/**
|
||||
* The console command description.
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run migrations for all modules or for one module';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$modules = Module::where('enabled', true);
|
||||
if (!is_null($module = $this->argument('module'))) {
|
||||
$modules = $modules->where('static_name', $module);
|
||||
}
|
||||
$modules = $modules->get();
|
||||
|
||||
if ($modules->isEmpty()) {
|
||||
$this->error('No valid modules.');
|
||||
return;
|
||||
}
|
||||
|
||||
$command = $this->option('rollback') ? 'migrate:rollback' : 'migrate';
|
||||
foreach ($modules as $module) {
|
||||
$this->info('Migrating '.$module->static_name);
|
||||
|
||||
$basePath = $module->is_template ? config('modules.path.template') : config('modules.paths.modules');
|
||||
$path = $basePath . '/' . $module->static_name . '/Database/Migrations/';
|
||||
|
||||
$this->call($command, ['--path' => $path]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Console;
|
||||
|
||||
use App\Console\Commands\InstallCMS;
|
||||
use App\Console\Commands\Modules\CreateMigration;
|
||||
use App\Console\Commands\Modules\RunMigrations;
|
||||
use App\Console\Commands\Modules\CreateNew;
|
||||
use App\Console\Commands\Modules\DisableModule;
|
||||
use App\Console\Commands\Modules\EnableModule;
|
||||
@@ -34,7 +36,9 @@ class Kernel extends ConsoleKernel
|
||||
Commands\AddLanguage::class,
|
||||
PublishTemplateResources::class,
|
||||
TemplateInfo::class,
|
||||
InstallCMS::class
|
||||
InstallCMS::class,
|
||||
CreateMigration::class,
|
||||
RunMigrations::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@ return [
|
||||
'namespace' => 'Modules',
|
||||
'paths' => [
|
||||
'modules' => base_path('modules'),
|
||||
'templates' => base_path('templates'),
|
||||
'assets' => public_path('modules'),
|
||||
'stubs' => base_path('app/CMS/Modules/Stubs'),
|
||||
'stubs' => base_path('app/CMS/Modules/Stubs'),
|
||||
]
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user