1: <?php
2:
3: namespace Aurora\System\Console\Commands\Migrations;
4:
5: use Aurora\System\Console\Commands\BaseCommand;
6: use Illuminate\Database\Console\Migrations\TableGuesser;
7: use Illuminate\Database\Migrations\MigrationCreator;
8: use Illuminate\Support\Composer;
9: use Illuminate\Support\Str;
10: use Symfony\Component\Console\Command\Command;
11: use Symfony\Component\Console\Input\InputArgument;
12: use Symfony\Component\Console\Input\InputInterface;
13: use Symfony\Component\Console\Input\InputOption;
14: use Symfony\Component\Console\Output\OutputInterface;
15:
16: class MigrateMakeCommand extends BaseCommand
17: {
18: /**
19: * The migration creator instance.
20: *
21: * @var \Illuminate\Database\Migrations\MigrationCreator
22: */
23: protected $creator;
24:
25: /**
26: * The Composer instance.
27: *
28: * @var \Illuminate\Support\Composer
29: */
30: protected $composer;
31:
32: /**
33: * Create a new migration install command instance.
34: *
35: * @param \Illuminate\Database\Migrations\MigrationCreator $creator
36: * @param \Illuminate\Support\Composer $composer
37: * @return void
38: */
39: public function __construct(MigrationCreator $creator, Composer $composer)
40: {
41: parent::__construct();
42:
43: $this->creator = $creator;
44: $this->composer = $composer;
45: }
46:
47: protected function configure(): void
48: {
49: $this->setName('make:migration')
50: ->setDescription('Create the migration repository')
51: ->addArgument('name', InputArgument::REQUIRED, 'The name of the migration')
52: ->addArgument('module', InputArgument::REQUIRED, 'The module for the migration')
53: ->addOption('create', null, InputOption::VALUE_OPTIONAL, 'The table to be created')
54: ->addOption('table', null, InputOption::VALUE_OPTIONAL, 'The table to migrate')
55: ->addOption('path', null, InputOption::VALUE_OPTIONAL, 'The location where the migration file should be created')
56: ->addOption('realpath', null, InputOption::VALUE_OPTIONAL, 'Indicate any provided migration file paths are pre-resolved absolute paths')
57: ->addOption('fullpath', null, InputOption::VALUE_OPTIONAL, 'Output the full path of the migration');
58: }
59:
60: /**
61: * Execute the console command.
62: *
63: * @return void
64: */
65: protected function execute(InputInterface $input, OutputInterface $output): int
66: {
67: // It's possible for the developer to specify the tables to modify in this
68: // schema operation. The developer may also specify if this table needs
69: // to be freshly created so we can create the appropriate migrations.
70: $name = Str::snake(trim($input->getArgument('name')));
71:
72: $table = $input->getOption('table');
73:
74: $create = $input->getOption('create') ?: false;
75:
76: // If no table was given as an option but a create option is given then we
77: // will use the "create" option as the table name. This allows the devs
78: // to pass a table name into this option as a short-cut for creating.
79: if (!$table && is_string($create)) {
80: $table = $create;
81:
82: $create = true;
83: }
84:
85: // Next, we will attempt to guess the table name if this the migration has
86: // "create" in the name. This will allow us to provide a convenient way
87: // of creating migrations that create new tables for the application.
88: if (!$table) {
89: [$table, $create] = TableGuesser::guess($name);
90: }
91:
92: // Now we are ready to write the migration out to disk. Once we've written
93: // the migration out, we will dump-autoload for the entire framework to
94: // make sure that the migrations are registered by the class loaders.
95: $this->writeMigration($name, $table, $create, $input, $output);
96:
97: $this->composer->dumpAutoloads();
98:
99: return Command::SUCCESS;
100: }
101:
102: /**
103: * Write the migration file to disk.
104: *
105: * @param string $name
106: * @param string $table
107: * @param bool $create
108: * @return void
109: */
110: protected function writeMigration($name, $table, $create, $input, $output)
111: {
112: $file = $this->creator->create(
113: $name,
114: $this->getMigrationPath($input->getArgument('module')),
115: $table,
116: $create
117: );
118:
119: if (!$input->getOption('fullpath')) {
120: $file = pathinfo($file, PATHINFO_FILENAME);
121: }
122:
123: $output->writeln("<info>Created Migration:</info> {$file}");
124: }
125: }
126: