| 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: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | protected $creator; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | protected $composer; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 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: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | protected function execute(InputInterface $input, OutputInterface $output): int |
| 66: | { |
| 67: | |
| 68: | |
| 69: | |
| 70: | $name = Str::snake(trim($input->getArgument('name'))); |
| 71: | |
| 72: | $table = $input->getOption('table'); |
| 73: | |
| 74: | $create = $input->getOption('create') ?: false; |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | if (!$table && is_string($create)) { |
| 80: | $table = $create; |
| 81: | |
| 82: | $create = true; |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | if (!$table) { |
| 89: | [$table, $create] = TableGuesser::guess($name); |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | $this->writeMigration($name, $table, $create, $input, $output); |
| 96: | |
| 97: | $this->composer->dumpAutoloads(); |
| 98: | |
| 99: | return Command::SUCCESS; |
| 100: | } |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 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: | |