1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Processing; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Constants\Status; |
6
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
7
|
|
|
use Helldar\LaravelLangPublisher\Facades\File; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Path; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
final class PublishJson extends BaseProcess |
12
|
|
|
{ |
13
|
|
|
public function run(): array |
14
|
|
|
{ |
15
|
|
|
$this->checkExists($this->sourcePath()); |
16
|
|
|
$this->publish(); |
17
|
|
|
|
18
|
|
|
return $this->result(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function publish(): void |
22
|
|
|
{ |
23
|
|
|
foreach (File::files($this->sourcePath()) as $file) { |
24
|
|
|
if ($file->isDir() || $file->getExtension() !== 'php') { |
25
|
|
|
continue; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$filename = $file->getFilename(); |
29
|
|
|
$src_file = $file->getRealPath(); |
30
|
|
|
$dst_file = $this->targetPath($filename); |
31
|
|
|
|
32
|
|
|
if ($this->force || ! File::exists($dst_file)) { |
33
|
|
|
$this->copy($src_file, $dst_file, $filename); |
34
|
|
|
$this->push($filename, Status::COPIED); |
35
|
|
|
|
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->push($filename, Status::SKIPPED); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function sourcePath(): string |
44
|
|
|
{ |
45
|
|
|
return Path::source($this->locale, null, true); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function targetPath(string $filename): string |
49
|
|
|
{ |
50
|
|
|
return Path::target($this->locale, $filename); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function copy(string $source, string $target, string $filename): void |
54
|
|
|
{ |
55
|
|
|
$key = File::name($filename); |
56
|
|
|
|
57
|
|
|
$this->isValidation($filename) |
58
|
|
|
? $this->copyValidations($source, $target, $key) |
59
|
|
|
: $this->copyOthers($source, $target, $key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function copyValidations(string $src, string $dst, string $filename): void |
63
|
|
|
{ |
64
|
|
|
$source = File::load($src); |
65
|
|
|
$target = File::load($dst, true); |
66
|
|
|
|
67
|
|
|
$source_custom = Arr::get($source, 'custom', []); |
68
|
|
|
$source_attributes = Arr::get($source, 'attributes', []); |
69
|
|
|
|
70
|
|
|
$target_custom = Arr::get($target, 'custom', []); |
71
|
|
|
$target_attributes = Arr::get($target, 'attributes', []); |
72
|
|
|
|
73
|
|
|
$excluded_target = $this->excluded($target, $filename); |
74
|
|
|
$excluded_custom = $this->excluded($target_custom, $filename); |
75
|
|
|
$excluded_attributes = $this->excluded($target_attributes, $filename); |
76
|
|
|
|
77
|
|
|
$custom = array_merge($source_custom, $target_custom, $excluded_custom); |
78
|
|
|
$attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes); |
79
|
|
|
|
80
|
|
|
$result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes')); |
81
|
|
|
|
82
|
|
|
File::save($dst, $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function copyOthers(string $src, string $dst, string $filename): void |
86
|
|
|
{ |
87
|
|
|
$source = File::load($src); |
88
|
|
|
$target = File::load($dst, true); |
89
|
|
|
|
90
|
|
|
$excluded = $this->excluded($target, $filename); |
91
|
|
|
|
92
|
|
|
$result = array_merge($target, $source, $excluded); |
93
|
|
|
|
94
|
|
|
File::save($dst, $result); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function isValidation(string $filename): bool |
98
|
|
|
{ |
99
|
|
|
return $filename === 'validation.php'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function excluded(array $array, string $key): array |
103
|
|
|
{ |
104
|
|
|
$keys = Config::getExclude($key, []); |
105
|
|
|
|
106
|
|
|
return Arr::only($array, $keys); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|