MigrateMakeCommand.php 4.3 KB

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