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() |
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->move( $this->progressBar, [ |
54
|
|
|
'inLaravel' => $_SERVER["PWD"] . '/database/migrations/', |
55
|
|
|
'stubs' => __DIR__ . '/../database/migrations/stubs/' |
56
|
|
|
], $this->listFileMigrations); |
57
|
|
|
|
58
|
|
|
$this->move( $this->progressBar, [ |
59
|
|
|
'inLaravel' => $_SERVER["PWD"] . '/database/seeds/', |
60
|
|
|
'stubs' => __DIR__ . '/../database/seeds/stubs/' |
61
|
|
|
], $this->listFilesSeeder ); |
62
|
|
|
|
63
|
|
|
$this->moveConfig( $this->progressBar ); |
64
|
|
|
|
65
|
|
|
$this->progressBar->finish(); |
66
|
|
|
$output->writeln(['']); |
67
|
|
|
$output->writeln(['<info>All successfully copied!</info>']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function move(ProgressBar $progress, array $pathList, $fileData) |
71
|
|
|
{ |
72
|
|
|
$this->createDir($pathList['inLaravel']); |
73
|
|
|
|
74
|
|
|
foreach ($fileData as $name => $migrate) |
75
|
|
|
{ |
76
|
|
|
$fileName = $pathList['inLaravel'] . $this->getDateNormalize() . $migrate . '.php'; |
77
|
|
|
file_put_contents($fileName, $this->getContent($pathList['stubs'] . $name)); |
78
|
|
|
$progress->advance(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
protected function moveConfig(ProgressBar $progress) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$pathToConfig = __DIR__ . '/../config.php'; |
86
|
|
|
$pathToConfigsLaravel = $_SERVER["PWD"] . '/config/'; |
87
|
|
|
$this->createDir($pathToConfigsLaravel); |
88
|
|
|
|
89
|
|
|
copy($pathToConfig,$pathToConfigsLaravel . 'geography.php' ); |
90
|
|
|
$progress->advance(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Just create directory |
95
|
|
|
* @param $dir |
96
|
|
|
*/ |
97
|
|
|
protected function createDir($dir) |
98
|
|
|
{ |
99
|
|
|
if (is_dir($dir) === false) |
100
|
|
|
{ |
101
|
|
|
mkdir($dir, 0777, true); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Data from stubs file |
107
|
|
|
* @param $nameFile |
108
|
|
|
* @return bool|string |
109
|
|
|
*/ |
110
|
|
|
protected function getContent($nameFile) |
111
|
|
|
{ |
112
|
|
|
return file_get_contents($nameFile); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get date normalize mow |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
protected function getDateNormalize() |
120
|
|
|
{ |
121
|
|
|
$date = Carbon::now(); |
122
|
|
|
$date = preg_replace('/-|\s/','_', $date); |
123
|
|
|
$data = preg_replace('/:/','', $date); |
124
|
|
|
|
125
|
|
|
return $data; |
126
|
|
|
} |
127
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: