FEATURE #2520: add commands for creating new migration and running migrations in modules

This commit is contained in:
Michal Kváček
2017-09-01 23:25:26 +02:00
parent c92ba41def
commit a5b6f6fa0d
22 changed files with 173 additions and 3 deletions
@@ -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';
}
}