1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Triadev\Leopard\Business\Mapping; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Illuminate\Filesystem\Filesystem; |
5
|
|
|
use Triadev\Leopard\Contract\Repository\MappingLogRepositoryContract; |
6
|
|
|
|
7
|
|
|
class Mapper |
|
|
|
|
8
|
|
|
{ |
|
|
|
|
9
|
|
|
/** @var Filesystem */ |
|
|
|
|
10
|
|
|
private $filesystem; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** @var MappingLogRepositoryContract */ |
|
|
|
|
13
|
|
|
private $mappingLogRepository; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** @var array */ |
|
|
|
|
16
|
|
|
private $mappingLogs; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Mapper constructor. |
20
|
|
|
* @param Filesystem $filesystem |
|
|
|
|
21
|
|
|
* @param MappingLogRepositoryContract $mappingLogRepository |
|
|
|
|
22
|
|
|
*/ |
23
|
8 |
|
public function __construct(Filesystem $filesystem, MappingLogRepositoryContract $mappingLogRepository) |
|
|
|
|
24
|
|
|
{ |
25
|
8 |
|
$this->filesystem = $filesystem; |
|
|
|
|
26
|
8 |
|
$this->mappingLogRepository = $mappingLogRepository; |
27
|
|
|
|
28
|
|
|
$this->mappingLogs = array_map(function ($log) { |
|
|
|
|
29
|
|
|
return $log['mapping']; |
30
|
8 |
|
}, $this->mappingLogRepository->all()); |
|
|
|
|
31
|
8 |
|
} |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Run mappings |
35
|
|
|
* |
36
|
|
|
* @param string $path |
|
|
|
|
37
|
|
|
* @param string|null $index |
|
|
|
|
38
|
|
|
* @param string|null $type |
|
|
|
|
39
|
|
|
* |
40
|
|
|
* @throws \Throwable |
|
|
|
|
41
|
|
|
*/ |
|
|
|
|
42
|
8 |
|
public function run(string $path, ?string $index = null, ?string $type = null) |
|
|
|
|
43
|
|
|
{ |
44
|
8 |
|
$mappingFiles = $this->getMappingFiles($path); |
45
|
|
|
|
46
|
8 |
|
sort($mappingFiles); |
47
|
|
|
|
48
|
8 |
|
foreach ($mappingFiles as $mappingFile) { |
49
|
8 |
|
if ($this->isMappingAlreadyRun($mappingFile)) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
$this->filesystem->requireOnce($path . DIRECTORY_SEPARATOR . $mappingFile . '.php'); |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** @var Mapping $mapping */ |
|
|
|
|
56
|
8 |
|
$mapping = new $mappingFile(); |
57
|
|
|
|
58
|
8 |
|
if ($index) { |
59
|
1 |
|
$mapping->setDocumentIndex($index); |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
if ($type) { |
63
|
1 |
|
$mapping->setDocumentType($type); |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
$mapping->map(); |
67
|
|
|
|
68
|
8 |
|
$this->mappingLogRepository->add($mappingFile); |
69
|
|
|
} |
|
|
|
|
70
|
8 |
|
} |
|
|
|
|
71
|
|
|
|
72
|
8 |
|
private function getMappingFiles(string $path): array |
|
|
|
|
73
|
|
|
{ |
74
|
8 |
|
$files = $this->filesystem->glob($path . DIRECTORY_SEPARATOR . '*.php'); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return array_map(function (string $file) { |
|
|
|
|
77
|
8 |
|
return str_replace('.php', '', basename($file)); |
78
|
8 |
|
}, $files); |
|
|
|
|
79
|
|
|
} |
|
|
|
|
80
|
|
|
|
81
|
8 |
|
private function isMappingAlreadyRun(string $mapping) : bool |
|
|
|
|
82
|
|
|
{ |
83
|
8 |
|
if (in_array($mapping, $this->mappingLogs)) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
return false; |
88
|
|
|
} |
|
|
|
|
89
|
|
|
} |
|
|
|
|
90
|
|
|
|