MigrateMakeCommand.php 4.5 KB

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