MigrateMakeCommand.php 3.8 KB

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