1: <?php
2:
3: namespace Baril\Sqlout\Migrations;
4:
5: use Baril\Sqlout\Migrations\MigrationCreator;
6: use Illuminate\Database\Console\Migrations\MigrateMakeCommand as BaseCommand;
7: use Illuminate\Support\Composer;
8: use Illuminate\Support\Str;
9:
10: class MigrateMakeCommand extends BaseCommand
11: {
12: protected $signature = 'sqlout:make-migration {connection? : Name of the connection}
13: {--name= : The name of the migration.}
14: {--path= : The location where the migration file should be created.}
15: {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths.}
16: {--migrate : Migrate the database after the migration file has been created.}';
17: protected $description = 'Create the migration file for Sqlout, and optionally run the migration';
18:
19: public function __construct(MigrationCreator $creator, Composer $composer)
20: {
21: parent::__construct($creator, $composer);
22: }
23:
24: public function handle()
25: {
26: $connection = $this->input->getArgument('connection') ?? config('database.default');
27:
28: $this->writeSqloutMigration($connection);
29: $this->composer->dumpAutoloads();
30:
31: if ($this->input->hasOption('migrate') && $this->option('migrate')) {
32: $this->call('migrate');
33: }
34: }
35:
36: protected function writeSqloutMigration($connection)
37: {
38: // Get the name for the migration file:
39: $name = $this->input->getOption('name') ?: 'create_sqlout_index_for_' . $connection;
40: $name = Str::snake(trim($name));
41: $className = Str::studly($name);
42: $tableName = config('scout.sqlout.table_name');
43:
44: // Generate the content of the migration file:
45: $contents = $this->getMigrationContents($className, $connection, $tableName);
46:
47: // Generate the file:
48: $file = $this->creator->create(
49: $name,
50: $this->getMigrationPath(),
51: $tableName,
52: true
53: );
54: file_put_contents($file, $contents);
55:
56: // Output information:
57: $file = pathinfo($file, PATHINFO_FILENAME);
58: $this->line("<info>Created Migration:</info> {$file}");
59: }
60:
61: protected function getMigrationContents($className, $connection, $tableName)
62: {
63: $contents = file_get_contents(__DIR__ . '/stubs/migration.stub');
64: $contents = str_replace([
65: 'class CreateSqloutIndex',
66: '::connection()',
67: "config('scout.sqlout.table_name')",
68: ], [
69: 'class ' . $className,
70: "::connection('$connection')",
71: "'$tableName'"
72: ], $contents);
73: $contents = preg_replace('/\;[\s]*\/\/.*\n/U', ";\n", $contents);
74: return $contents;
75: }
76: }
77: