You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrateMakeCommand.php 4.1KB

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