Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Console\Migrations;
4
 
5
use Illuminate\Database\Migrations\Migrator;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Console\Input\InputOption;
8
 
9
class StatusCommand extends BaseCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'migrate:status';
17
 
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Show the status of each migration';
24
 
25
    /**
26
     * The migrator instance.
27
     *
28
     * @var \Illuminate\Database\Migrations\Migrator
29
     */
30
    protected $migrator;
31
 
32
    /**
33
     * Create a new migration rollback command instance.
34
     *
35
     * @param  \Illuminate\Database\Migrations\Migrator  $migrator
36
     * @return void
37
     */
38
    public function __construct(Migrator $migrator)
39
    {
40
        parent::__construct();
41
 
42
        $this->migrator = $migrator;
43
    }
44
 
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return int|null
49
     */
50
    public function handle()
51
    {
52
        return $this->migrator->usingConnection($this->option('database'), function () {
53
            if (! $this->migrator->repositoryExists()) {
54
                $this->components->error('Migration table not found.');
55
 
56
                return 1;
57
            }
58
 
59
            $ran = $this->migrator->getRepository()->getRan();
60
 
61
            $batches = $this->migrator->getRepository()->getMigrationBatches();
62
 
63
            if (count($migrations = $this->getStatusFor($ran, $batches)) > 0) {
64
                $this->newLine();
65
 
66
                $this->components->twoColumnDetail('<fg=gray>Migration name</>', '<fg=gray>Batch / Status</>');
67
 
68
                $migrations->each(
69
                    fn ($migration) => $this->components->twoColumnDetail($migration[0], $migration[1])
70
                );
71
 
72
                $this->newLine();
73
            } else {
74
                $this->components->info('No migrations found');
75
            }
76
        });
77
    }
78
 
79
    /**
80
     * Get the status for the given run migrations.
81
     *
82
     * @param  array  $ran
83
     * @param  array  $batches
84
     * @return \Illuminate\Support\Collection
85
     */
86
    protected function getStatusFor(array $ran, array $batches)
87
    {
88
        return Collection::make($this->getAllMigrationFiles())
89
                    ->map(function ($migration) use ($ran, $batches) {
90
                        $migrationName = $this->migrator->getMigrationName($migration);
91
 
92
                        $status = in_array($migrationName, $ran)
93
                            ? '<fg=green;options=bold>Ran</>'
94
                            : '<fg=yellow;options=bold>Pending</>';
95
 
96
                        if (in_array($migrationName, $ran)) {
97
                            $status = '['.$batches[$migrationName].'] '.$status;
98
                        }
99
 
100
                        return [$migrationName, $status];
101
                    });
102
    }
103
 
104
    /**
105
     * Get an array of all of the migration files.
106
     *
107
     * @return array
108
     */
109
    protected function getAllMigrationFiles()
110
    {
111
        return $this->migrator->getMigrationFiles($this->getMigrationPaths());
112
    }
113
 
114
    /**
115
     * Get the console command options.
116
     *
117
     * @return array
118
     */
119
    protected function getOptions()
120
    {
121
        return [
122
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
123
 
124
            ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to use'],
125
 
126
            ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
127
        ];
128
    }
129
}