1
|
|
|
<?php |
2
|
|
|
namespace Triadev\EsMigration\Console\Commands; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use Triadev\EsMigration\Business\Mapper\MigrationTypes; |
6
|
|
|
use Triadev\EsMigration\Contract\ElasticsearchMigrationContract; |
7
|
|
|
|
8
|
|
|
class ImportFileMigrations extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'triadev:es:import_file_migrations {migration}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Import file migrations.'; |
23
|
|
|
|
24
|
|
|
/** @var ElasticsearchMigrationContract */ |
25
|
|
|
private $elasticsearchMigrationService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ImportFileMigrations constructor. |
29
|
|
|
* @param ElasticsearchMigrationContract $elasticsearchMigrationService |
30
|
|
|
*/ |
31
|
63 |
|
public function __construct(ElasticsearchMigrationContract $elasticsearchMigrationService) |
32
|
|
|
{ |
33
|
63 |
|
parent::__construct(); |
34
|
|
|
|
35
|
63 |
|
$this->elasticsearchMigrationService = $elasticsearchMigrationService; |
36
|
63 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
3 |
|
public function handle() |
44
|
|
|
{ |
45
|
3 |
|
$migration = (string)$this->argument('migration'); |
46
|
|
|
|
47
|
3 |
|
$filePath = config('triadev-elasticsearch-migration.filePath'); |
48
|
3 |
|
if (!$filePath) { |
49
|
1 |
|
throw new \Exception("No migration file path was defined."); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$migrationSteps = $this->getMigrationSteps($migration, $filePath); |
53
|
1 |
|
if (!empty($migrationSteps)) { |
54
|
1 |
|
$this->elasticsearchMigrationService->createMigration($migration); |
55
|
|
|
|
56
|
1 |
|
$this->importMigrationSteps( |
57
|
1 |
|
$migration, |
58
|
1 |
|
$migrationSteps |
59
|
|
|
); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $migration |
65
|
|
|
* @param string $filePath |
66
|
|
|
* @return array |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
2 |
|
private function getMigrationSteps(string $migration, string $filePath) : array |
70
|
|
|
{ |
71
|
2 |
|
$filePath = $filePath . DIRECTORY_SEPARATOR . $migration; |
72
|
2 |
|
if (!is_dir($filePath)) { |
73
|
1 |
|
throw new \Exception("The migration directory does not exist."); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$files = []; |
77
|
|
|
|
78
|
1 |
|
foreach (scandir($filePath) as $key => $value) |
79
|
|
|
{ |
80
|
1 |
|
if (!in_array($value, ['.', '..'])) { |
81
|
1 |
|
$files[] = $filePath . DIRECTORY_SEPARATOR . $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $files; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $migration |
90
|
|
|
* @param array $migrationSteps |
91
|
|
|
*/ |
92
|
1 |
|
private function importMigrationSteps(string $migration, array $migrationSteps) |
93
|
|
|
{ |
94
|
1 |
|
$migrationTypes = new MigrationTypes(); |
95
|
|
|
|
96
|
1 |
|
foreach ($migrationSteps as $migrationStep) { |
97
|
1 |
|
$step = require $migrationStep; |
98
|
|
|
|
99
|
1 |
|
if (array_has($step, ['type', 'params']) && |
100
|
1 |
|
$migrationTypes->isMigrationTypeValid(array_get($step, 'type'))) { |
|
|
|
|
101
|
1 |
|
$this->elasticsearchMigrationService->addMigrationStep( |
102
|
1 |
|
$migration, |
103
|
1 |
|
array_get($step, 'type'), |
|
|
|
|
104
|
1 |
|
array_get($step, 'params'), |
|
|
|
|
105
|
1 |
|
array_get($step, 'priority', 1), |
106
|
1 |
|
array_get($step, 'stopOnFailure', true) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
1 |
|
} |
111
|
|
|
} |
112
|
|
|
|