Subversion-Projekte lars-tiefland.laravel_shop

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\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Database\Events\DatabaseRefreshed;
9
use Symfony\Component\Console\Input\InputOption;
10
 
11
class RefreshCommand extends Command
12
{
13
    use ConfirmableTrait;
14
 
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'migrate:refresh';
21
 
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Reset and re-run all migrations';
28
 
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return int
33
     */
34
    public function handle()
35
    {
36
        if (! $this->confirmToProceed()) {
37
            return 1;
38
        }
39
 
40
        // Next we'll gather some of the options so that we can have the right options
41
        // to pass to the commands. This includes options such as which database to
42
        // use and the path to use for the migration. Then we'll run the command.
43
        $database = $this->input->getOption('database');
44
 
45
        $path = $this->input->getOption('path');
46
 
47
        // If the "step" option is specified it means we only want to rollback a small
48
        // number of migrations before migrating again. For example, the user might
49
        // only rollback and remigrate the latest four migrations instead of all.
50
        $step = $this->input->getOption('step') ?: 0;
51
 
52
        if ($step > 0) {
53
            $this->runRollback($database, $path, $step);
54
        } else {
55
            $this->runReset($database, $path);
56
        }
57
 
58
        // The refresh command is essentially just a brief aggregate of a few other of
59
        // the migration commands and just provides a convenient wrapper to execute
60
        // them in succession. We'll also see if we need to re-seed the database.
61
        $this->call('migrate', array_filter([
62
            '--database' => $database,
63
            '--path' => $path,
64
            '--realpath' => $this->input->getOption('realpath'),
65
            '--force' => true,
66
        ]));
67
 
68
        if ($this->laravel->bound(Dispatcher::class)) {
69
            $this->laravel[Dispatcher::class]->dispatch(
70
                new DatabaseRefreshed
71
            );
72
        }
73
 
74
        if ($this->needsSeeding()) {
75
            $this->runSeeder($database);
76
        }
77
 
78
        return 0;
79
    }
80
 
81
    /**
82
     * Run the rollback command.
83
     *
84
     * @param  string  $database
85
     * @param  string  $path
86
     * @param  int  $step
87
     * @return void
88
     */
89
    protected function runRollback($database, $path, $step)
90
    {
91
        $this->call('migrate:rollback', array_filter([
92
            '--database' => $database,
93
            '--path' => $path,
94
            '--realpath' => $this->input->getOption('realpath'),
95
            '--step' => $step,
96
            '--force' => true,
97
        ]));
98
    }
99
 
100
    /**
101
     * Run the reset command.
102
     *
103
     * @param  string  $database
104
     * @param  string  $path
105
     * @return void
106
     */
107
    protected function runReset($database, $path)
108
    {
109
        $this->call('migrate:reset', array_filter([
110
            '--database' => $database,
111
            '--path' => $path,
112
            '--realpath' => $this->input->getOption('realpath'),
113
            '--force' => true,
114
        ]));
115
    }
116
 
117
    /**
118
     * Determine if the developer has requested database seeding.
119
     *
120
     * @return bool
121
     */
122
    protected function needsSeeding()
123
    {
124
        return $this->option('seed') || $this->option('seeder');
125
    }
126
 
127
    /**
128
     * Run the database seeder command.
129
     *
130
     * @param  string  $database
131
     * @return void
132
     */
133
    protected function runSeeder($database)
134
    {
135
        $this->call('db:seed', array_filter([
136
            '--database' => $database,
137
            '--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
138
            '--force' => true,
139
        ]));
140
    }
141
 
142
    /**
143
     * Get the console command options.
144
     *
145
     * @return array
146
     */
147
    protected function getOptions()
148
    {
149
        return [
150
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
151
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
152
            ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
153
            ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
154
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
155
            ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
156
            ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted & re-run'],
157
        ];
158
    }
159
}