1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caffeinated\Modules\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Caffeinated\Modules\Modules; |
6
|
|
|
use Caffeinated\Modules\Traits\MigrationTrait; |
7
|
|
|
use Illuminate\Console\ConfirmableTrait; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
|
12
|
|
|
class ModuleMigrateRollbackCommand extends Command |
13
|
|
|
{ |
14
|
|
|
use MigrationTrait, ConfirmableTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command name. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name = 'module:migrate:rollback'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Rollback the last database migrations for a specific or all modules'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Modules |
32
|
|
|
*/ |
33
|
|
|
protected $module; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new command instance. |
37
|
|
|
* |
38
|
|
|
* @param Modules $module |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Modules $module) |
41
|
|
|
{ |
42
|
|
|
parent::__construct(); |
43
|
|
|
|
44
|
|
|
$this->module = $module; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function fire() |
53
|
|
|
{ |
54
|
|
|
if (!$this->confirmToProceed()) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$slug = $this->argument('slug'); |
59
|
|
|
|
60
|
|
|
if ($slug) { |
61
|
|
|
return $this->rollback($slug); |
62
|
|
|
} else { |
63
|
|
|
foreach ($this->module->all() as $module) { |
64
|
|
|
$this->rollback($module['slug']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Run the migration rollback for the specified module. |
71
|
|
|
* |
72
|
|
|
* @param string $slug |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
protected function rollback($slug) |
77
|
|
|
{ |
78
|
|
|
$this->requireMigrations($slug); |
79
|
|
|
|
80
|
|
|
$this->call('migrate:rollback', [ |
81
|
|
|
'--database' => $this->option('database'), |
82
|
|
|
'--force' => $this->option('force'), |
83
|
|
|
'--pretend' => $this->option('pretend'), |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the console command arguments. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function getArguments() |
93
|
|
|
{ |
94
|
|
|
return [['slug', InputArgument::OPTIONAL, 'Module slug.']]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the console command options. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], |
106
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'], |
107
|
|
|
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.