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