MigrateMakeCommand.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if (preg_match('/^create_(\w+)_table$/', $name, $matches)) {
  74. $table = $matches[1];
  75. $create = true;
  76. }
  77. }
  78. // Now we are ready to write the migration out to disk. Once we've written
  79. // the migration out, we will dump-autoload for the entire framework to
  80. // make sure that the migrations are registered by the class loaders.
  81. $this->writeMigration($name, $table, $create);
  82. $this->composer->dumpAutoloads();
  83. }
  84. /**
  85. * Write the migration file to disk.
  86. *
  87. * @param string $name
  88. * @param string $table
  89. * @param bool $create
  90. * @return string
  91. */
  92. protected function writeMigration($name, $table, $create)
  93. {
  94. $file = pathinfo($this->creator->create(
  95. $name, $this->getMigrationPath(), $table, $create
  96. ), PATHINFO_FILENAME);
  97. $this->line("<info>Created Migration:</info> {$file}");
  98. }
  99. /**
  100. * Get migration path (either specified by '--path' option or default location).
  101. *
  102. * @return string
  103. */
  104. protected function getMigrationPath()
  105. {
  106. if (! is_null($targetPath = $this->input->getOption('path'))) {
  107. return ! $this->usingRealPath()
  108. ? $this->laravel->basePath().'/'.$targetPath
  109. : $targetPath;
  110. }
  111. return parent::getMigrationPath();
  112. }
  113. /**
  114. * Determine if the given path(s) are pre-resolved "real" paths.
  115. *
  116. * @return bool
  117. */
  118. protected function usingRealPath()
  119. {
  120. return $this->input->hasOption('realpath') && $this->option('realpath');
  121. }
  122. }