|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProAI\SuperMigrations\Console\Migrations; |
| 4 | + |
| 5 | +use Illuminate\Support\Composer; |
| 6 | +use Illuminate\Database\Migrations\MigrationCreator; |
| 7 | + |
| 8 | +class MigrateMakeCommand extends BaseCommand |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The console command signature. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'make:migration:super {name : The name of the migration.}'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Create a new migration file'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The migration creator instance. |
| 26 | + * |
| 27 | + * @var \Illuminate\Database\Migrations\MigrationCreator |
| 28 | + */ |
| 29 | + protected $creator; |
| 30 | + |
| 31 | + /** |
| 32 | + * The Composer instance. |
| 33 | + * |
| 34 | + * @var \Illuminate\Support\Composer |
| 35 | + */ |
| 36 | + protected $composer; |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a new migration install command instance. |
| 40 | + * |
| 41 | + * @param \Illuminate\Database\Migrations\MigrationCreator $creator |
| 42 | + * @param \Illuminate\Support\Composer $composer |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function __construct(MigrationCreator $creator, Composer $composer) |
| 46 | + { |
| 47 | + parent::__construct(); |
| 48 | + |
| 49 | + $this->creator = $creator; |
| 50 | + $this->composer = $composer; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Execute the console command. |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function fire() |
| 59 | + { |
| 60 | + // It's possible for the developer to specify the tables to modify in this |
| 61 | + // schema operation. The developer may also specify if this table needs |
| 62 | + // to be freshly created so we can create the appropriate migrations. |
| 63 | + $name = trim($this->input->getArgument('name')); |
| 64 | + |
| 65 | + $table = $this->input->getOption('table'); |
| 66 | + |
| 67 | + // Now we are ready to write the migration out to disk. Once we've written |
| 68 | + // the migration out, we will dump-autoload for the entire framework to |
| 69 | + // make sure that the migrations are registered by the class loaders. |
| 70 | + $this->writeMigration($name, $table); |
| 71 | + |
| 72 | + $this->composer->dumpAutoloads(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Write the migration file to disk. |
| 77 | + * |
| 78 | + * @param string $name |
| 79 | + * @param string $table |
| 80 | + * @return string |
| 81 | + */ |
| 82 | + protected function writeMigration($name, $table) |
| 83 | + { |
| 84 | + $path = $this->getMigrationPath(); |
| 85 | + |
| 86 | + $file = pathinfo($this->creator->create($name, $path, $table), PATHINFO_FILENAME); |
| 87 | + |
| 88 | + $this->line("<info>Created Migration:</info> {$file}"); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get migration path (either specified by '--path' option or default location). |
| 93 | + * |
| 94 | + * @return string |
| 95 | + */ |
| 96 | + protected function getMigrationPath() |
| 97 | + { |
| 98 | + if (! is_null($targetPath = $this->input->getOption('path'))) { |
| 99 | + return $this->laravel->basePath().'/'.$targetPath; |
| 100 | + } |
| 101 | + |
| 102 | + return parent::getMigrationPath(); |
| 103 | + } |
| 104 | +} |
0 commit comments