1
|
|
|
<?php |
2
|
|
|
namespace agoalofalife\Commands; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class MigrateLaravelCommand extends Command |
13
|
|
|
{ |
14
|
|
|
protected $listFileMigrations = [ |
15
|
|
|
'country' => '_create_table_country', |
16
|
|
|
'regions' => '_create_table_regions', |
17
|
|
|
'cities' => '_create_table_cities' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
protected $listFilesSeeder = [ |
21
|
|
|
'CountryTableSeeder' => 'CountryTableSeeder.php', |
22
|
|
|
'RegionsTableSeeder' => 'RegionsTableSeeder.php', |
23
|
|
|
'CitiesTableSeeder' => 'CitiesTableSeeder.php' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
protected $formatterStyle; |
27
|
|
|
protected $progressBar; |
28
|
|
|
|
29
|
2 |
|
public function __construct(OutputFormatterStyle $formatterStyle, ProgressBar $bar) |
30
|
|
|
{ |
31
|
2 |
|
parent::__construct(); |
32
|
2 |
|
$this->formatterStyle = $formatterStyle; |
33
|
2 |
|
$this->progressBar = $bar; |
34
|
2 |
|
$this->progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%'); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
2 |
|
protected function configure() : void |
38
|
|
|
{ |
39
|
2 |
|
$this->setName('migrate:laravel')->setHelp('to migrate files to Laravel'); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$output->getFormatter()->setStyle('fire', $this->formatterStyle); |
45
|
|
|
|
46
|
|
|
$output->writeln([ |
47
|
|
|
'<fire>There is a migration in the project Laravel</fire>', |
48
|
|
|
'' |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->progressBar->start(); |
52
|
|
|
|
53
|
|
|
$this->moveMigrate( $this->progressBar ); |
54
|
|
|
$this->moveSeeders( $this->progressBar ); |
55
|
|
|
$this->moveConfig( $this->progressBar ); |
56
|
|
|
|
57
|
|
|
$this->progressBar->finish(); |
58
|
|
|
$output->writeln(['']); |
59
|
|
|
$output->writeln(['<info>All successfully copied!</info>']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
protected function moveMigrate(ProgressBar $progress) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$pathToMigrationsLaravel = $_SERVER["PWD"] . '/database/migrations/'; |
65
|
|
|
$pathToStubs = __DIR__ . '/../database/migrations/stubs/'; |
66
|
|
|
|
67
|
|
|
$this->createDir($pathToMigrationsLaravel); |
68
|
|
|
|
69
|
|
|
foreach ($this->listFileMigrations as $name => $migrate) |
70
|
|
|
{ |
71
|
|
|
$fileName = $pathToMigrationsLaravel . $this->getDateNormalize() . $migrate . '.php'; |
72
|
|
|
file_put_contents($fileName, $this->getContent($pathToStubs . $name)); |
73
|
|
|
$progress->advance(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
protected function moveSeeders(ProgressBar $progress) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$pathToSeedersLaravel = $_SERVER["PWD"] . '/database/seeds/'; |
80
|
|
|
$pathToSeed = __DIR__ . '/../database/seeds/stubs/'; |
81
|
|
|
|
82
|
|
|
$this->createDir($pathToSeedersLaravel); |
83
|
|
|
|
84
|
|
|
foreach ($this->listFilesSeeder as $name => $seed) |
85
|
|
|
{ |
86
|
|
|
$fileName = $pathToSeedersLaravel . $seed; |
87
|
|
|
file_put_contents($fileName, $this->getContent($pathToSeed . $name)); |
88
|
|
|
$progress->advance(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function moveConfig(ProgressBar $progress) : void |
93
|
|
|
{ |
94
|
|
|
$pathToConfig = __DIR__ . '/../config.php'; |
95
|
|
|
$pathToConfigsLaravel = $_SERVER["PWD"] . '/config/'; |
96
|
|
|
$this->createDir($pathToConfigsLaravel); |
97
|
|
|
|
98
|
|
|
copy($pathToConfig,$pathToConfigsLaravel . 'geography.php' ); |
99
|
|
|
$progress->advance(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Just create directory |
104
|
|
|
* @param $dir |
105
|
|
|
*/ |
106
|
|
|
protected function createDir(string $dir) : void |
107
|
|
|
{ |
108
|
|
|
if (is_dir($dir) === false) |
109
|
|
|
{ |
110
|
|
|
mkdir($dir, 0777, true); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Data from stubs file |
116
|
|
|
* @param $nameFile |
117
|
|
|
* @return bool|string |
118
|
|
|
*/ |
119
|
|
|
protected function getContent(string $nameFile) : string |
120
|
|
|
{ |
121
|
|
|
return file_get_contents($nameFile); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get date normalize mow |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
protected function getDateNormalize() : string |
129
|
|
|
{ |
130
|
|
|
$date = Carbon::now(); |
131
|
|
|
$date = preg_replace('/-|\s/','_', $date); |
132
|
|
|
$data = preg_replace('/:/','', $date); |
133
|
|
|
|
134
|
|
|
return $data; |
135
|
|
|
} |
136
|
|
|
} |
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.