StatusCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Database\Migrations\Migrator;
  5. use Symfony\Component\Console\Input\InputOption;
  6. class StatusCommand extends BaseCommand
  7. {
  8. /**
  9. * The console command name.
  10. *
  11. * @var string
  12. */
  13. protected $name = 'migrate:status';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Show the status of each migration';
  20. /**
  21. * The migrator instance.
  22. *
  23. * @var \Illuminate\Database\Migrations\Migrator
  24. */
  25. protected $migrator;
  26. /**
  27. * Create a new migration rollback command instance.
  28. *
  29. * @param \Illuminate\Database\Migrations\Migrator $migrator
  30. * @return void
  31. */
  32. public function __construct(Migrator $migrator)
  33. {
  34. parent::__construct();
  35. $this->migrator = $migrator;
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $this->migrator->setConnection($this->option('database'));
  45. if (! $this->migrator->repositoryExists()) {
  46. return $this->error('No migrations found.');
  47. }
  48. $ran = $this->migrator->getRepository()->getRan();
  49. if (count($migrations = $this->getStatusFor($ran)) > 0) {
  50. $this->table(['Ran?', 'Migration'], $migrations);
  51. } else {
  52. $this->error('No migrations found');
  53. }
  54. }
  55. /**
  56. * Get the status for the given ran migrations.
  57. *
  58. * @param array $ran
  59. * @return \Illuminate\Support\Collection
  60. */
  61. protected function getStatusFor(array $ran)
  62. {
  63. return Collection::make($this->getAllMigrationFiles())
  64. ->map(function ($migration) use ($ran) {
  65. $migrationName = $this->migrator->getMigrationName($migration);
  66. return in_array($migrationName, $ran)
  67. ? ['<info>Y</info>', $migrationName]
  68. : ['<fg=red>N</fg=red>', $migrationName];
  69. });
  70. }
  71. /**
  72. * Get an array of all of the migration files.
  73. *
  74. * @return array
  75. */
  76. protected function getAllMigrationFiles()
  77. {
  78. return $this->migrator->getMigrationFiles($this->getMigrationPaths());
  79. }
  80. /**
  81. * Get the console command options.
  82. *
  83. * @return array
  84. */
  85. protected function getOptions()
  86. {
  87. return [
  88. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
  89. ['path', null, InputOption::VALUE_OPTIONAL, 'The path of migrations files to use.'],
  90. ];
  91. }
  92. }