1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caffeinated\Modules\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Console\ConfirmableTrait; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class ModuleMigrateRefreshCommand extends Command |
11
|
|
|
{ |
12
|
|
|
use ConfirmableTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The console command name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name = 'module:migrate:refresh'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Reset and re-run all migrations for a specific or all modules'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Execute the console command. |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function fire() |
34
|
|
|
{ |
35
|
|
|
if (!$this->confirmToProceed()) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$slug = $this->argument('slug'); |
40
|
|
|
|
41
|
|
|
$this->call('module:migrate:reset', [ |
42
|
|
|
'slug' => $slug, |
43
|
|
|
'--database' => $this->option('database'), |
44
|
|
|
'--force' => $this->option('force'), |
45
|
|
|
'--pretend' => $this->option('pretend'), |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$this->call('module:migrate', [ |
49
|
|
|
'slug' => $slug, |
50
|
|
|
'--database' => $this->option('database'), |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
if ($this->needsSeeding()) { |
54
|
|
|
$this->runSeeder($slug, $this->option('database')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (isset($slug)) { |
58
|
|
|
$this->info('Module has been refreshed.'); |
59
|
|
|
} else { |
60
|
|
|
$this->info('All modules have been refreshed.'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Determine if the developer has requested database seeding. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
protected function needsSeeding() |
70
|
|
|
{ |
71
|
|
|
return $this->option('seed'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Run the module seeder command. |
76
|
|
|
* |
77
|
|
|
* @param string $database |
78
|
|
|
*/ |
79
|
|
|
protected function runSeeder($slug = null, $database = null) |
80
|
|
|
{ |
81
|
|
|
$this->call('module:seed', [ |
82
|
|
|
'slug' => $slug, |
83
|
|
|
'--database' => $database, |
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
|
|
|
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.