1
|
|
|
<?php |
2
|
|
|
namespace Triadev\EsMigration\Console\Commands; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Triadev\EsMigration\Business\Mapper\MigrationTypes; |
8
|
|
|
use Triadev\EsMigration\Contract\ElasticsearchMigrationContract; |
9
|
|
|
|
10
|
|
|
class ImportFileMigrations extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'triadev:es-migration:import-file-migrations {migration} {filePath}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Import file migrations.'; |
25
|
|
|
|
26
|
|
|
/** @var ElasticsearchMigrationContract */ |
27
|
|
|
private $elasticsearchMigrationService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ImportFileMigrations constructor. |
31
|
|
|
* @param ElasticsearchMigrationContract $elasticsearchMigrationService |
32
|
|
|
*/ |
33
|
63 |
|
public function __construct(ElasticsearchMigrationContract $elasticsearchMigrationService) |
34
|
|
|
{ |
35
|
63 |
|
parent::__construct(); |
36
|
|
|
|
37
|
63 |
|
$this->elasticsearchMigrationService = $elasticsearchMigrationService; |
38
|
63 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the console command. |
42
|
|
|
* |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
3 |
|
public function handle() |
46
|
|
|
{ |
47
|
3 |
|
$migration = (string)$this->argument('migration'); |
48
|
3 |
|
$filePathSelector = (string)$this->argument('filePath'); |
49
|
|
|
|
50
|
3 |
|
$filePath = config('triadev-elasticsearch-migration.filePath'); |
51
|
3 |
|
if (!array_has($filePath, $filePathSelector)) { |
52
|
1 |
|
throw new \Exception("No migration file path was defined."); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$migrationSteps = $this->getMigrationSteps($migration, $filePath[$filePathSelector]); |
56
|
1 |
|
if (!empty($migrationSteps)) { |
57
|
1 |
|
$this->elasticsearchMigrationService->createMigration($migration); |
58
|
|
|
|
59
|
1 |
|
$this->importMigrationSteps( |
60
|
1 |
|
$migration, |
61
|
1 |
|
$migrationSteps |
62
|
|
|
); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $migration |
68
|
|
|
* @param string $filePath |
69
|
|
|
* @return array |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
2 |
|
private function getMigrationSteps(string $migration, string $filePath) : array |
73
|
|
|
{ |
74
|
2 |
|
$filePath = $filePath . DIRECTORY_SEPARATOR . $migration; |
75
|
2 |
|
if (!is_dir($filePath)) { |
76
|
1 |
|
throw new \Exception("The migration directory does not exist."); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$files = []; |
80
|
|
|
|
81
|
1 |
|
foreach (scandir($filePath) as $key => $value) { |
82
|
1 |
|
if (!in_array($value, ['.', '..'])) { |
83
|
1 |
|
$files[] = $filePath . DIRECTORY_SEPARATOR . $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $files; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $migration |
92
|
|
|
* @param array $migrationSteps |
93
|
|
|
*/ |
94
|
1 |
|
private function importMigrationSteps(string $migration, array $migrationSteps) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
1 |
|
foreach ($this->getValidMigrationSteps($migrationSteps) as $validMigrationStep) { |
98
|
1 |
|
$this->elasticsearchMigrationService->addMigrationStep( |
99
|
1 |
|
$migration, |
100
|
1 |
|
/** @scrutinizer ignore-type */ array_get($validMigrationStep, 'type'), |
101
|
1 |
|
/** @scrutinizer ignore-type */ array_get($validMigrationStep, 'params'), |
102
|
1 |
|
array_get($validMigrationStep, 'priority', 1), |
103
|
1 |
|
array_get($validMigrationStep, 'stopOnFailure', true) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
Log::error("The migration steps could not be imported."); |
108
|
|
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $migrationSteps |
113
|
|
|
* @return array |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
1 |
|
private function getValidMigrationSteps(array $migrationSteps) : array |
117
|
|
|
{ |
118
|
1 |
|
$migrationTypes = new MigrationTypes(); |
119
|
|
|
|
120
|
1 |
|
$validMigrationSteps = []; |
121
|
|
|
|
122
|
1 |
|
foreach ($migrationSteps as $migrationStep) { |
123
|
1 |
|
$step = require $migrationStep; |
124
|
|
|
|
125
|
1 |
|
if (Validator::make($step, [ |
126
|
1 |
|
'type' => 'required|string', |
127
|
|
|
'params' => 'required|array', |
128
|
|
|
'priority' => 'integer', |
129
|
|
|
'stopOnFailure' => 'boolean' |
130
|
1 |
|
])->fails()) { |
131
|
|
|
throw new \Exception("The migration step is invalid."); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if (!$migrationTypes->isMigrationTypeValid( |
135
|
1 |
|
/** @scrutinizer ignore-type */ array_get($step, 'type') |
136
|
|
|
)) { |
137
|
|
|
throw new \Exception("The migration step type is invalid."); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
$validMigrationSteps[] = $step; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $validMigrationSteps; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|