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
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use SplFileInfo; |
12
|
|
|
|
13
|
|
|
final class PublishJson extends BaseProcess |
14
|
|
|
{ |
15
|
|
|
protected $extension = 'json'; |
16
|
|
|
|
17
|
30 |
|
public function run(): array |
18
|
|
|
{ |
19
|
30 |
|
$this->checkExists($this->sourcePath()); |
20
|
18 |
|
$this->publish(); |
21
|
|
|
|
22
|
18 |
|
return $this->result(); |
23
|
|
|
} |
24
|
|
|
|
25
|
18 |
|
protected function publish(): void |
26
|
|
|
{ |
27
|
18 |
|
$file = new SplFileInfo($this->sourcePath()); |
28
|
|
|
|
29
|
18 |
|
if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
18 |
|
$filename = $this->getTargetFilename($file); |
34
|
18 |
|
$src_file = $this->getSourceFilePath($file); |
35
|
18 |
|
$dst_file = $this->targetPath($filename); |
36
|
|
|
|
37
|
18 |
|
if ($this->force || ! File::exists($dst_file)) { |
38
|
18 |
|
$this->copy($src_file, $dst_file, $filename); |
39
|
18 |
|
$this->push($filename, Status::COPIED); |
40
|
|
|
|
41
|
18 |
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->push($filename, Status::SKIPPED); |
45
|
|
|
} |
46
|
|
|
|
47
|
30 |
|
protected function sourcePath(): string |
48
|
|
|
{ |
49
|
30 |
|
return Path::source($this->locale); |
50
|
|
|
} |
51
|
|
|
|
52
|
18 |
|
protected function targetPath(string $filename): string |
53
|
|
|
{ |
54
|
18 |
|
return Path::target($this->locale, $filename); |
55
|
|
|
} |
56
|
|
|
|
57
|
18 |
|
protected function copy(string $source, string $target, string $filename): void |
58
|
|
|
{ |
59
|
18 |
|
$key = File::name($filename); |
60
|
|
|
|
61
|
18 |
|
$this->isValidation($filename) |
62
|
|
|
? $this->copyValidations($source, $target, $key) |
63
|
18 |
|
: $this->copyOthers($source, $target, $key); |
64
|
18 |
|
} |
65
|
|
|
|
66
|
|
|
protected function copyValidations(string $src, string $dst, string $filename): void |
67
|
|
|
{ |
68
|
|
|
$source = File::load($src); |
69
|
|
|
$target = File::load($dst, true); |
70
|
|
|
|
71
|
|
|
$source_custom = Arr::get($source, 'custom', []); |
72
|
|
|
$source_attributes = Arr::get($source, 'attributes', []); |
73
|
|
|
|
74
|
|
|
$target_custom = Arr::get($target, 'custom', []); |
75
|
|
|
$target_attributes = Arr::get($target, 'attributes', []); |
76
|
|
|
|
77
|
|
|
$excluded_target = $this->excluded($target, $filename); |
78
|
|
|
$excluded_custom = $this->excluded($target_custom, $filename); |
79
|
|
|
$excluded_attributes = $this->excluded($target_attributes, $filename); |
80
|
|
|
|
81
|
|
|
$custom = array_merge($source_custom, $target_custom, $excluded_custom); |
82
|
|
|
$attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes); |
83
|
|
|
|
84
|
|
|
$result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes')); |
85
|
|
|
|
86
|
|
|
File::save($dst, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
18 |
|
protected function copyOthers(string $src, string $dst, string $filename): void |
90
|
|
|
{ |
91
|
18 |
|
$source = File::load($src); |
92
|
18 |
|
$target = File::load($dst, true); |
93
|
|
|
|
94
|
18 |
|
$excluded = $this->excluded($target, $filename); |
95
|
|
|
|
96
|
18 |
|
$result = array_merge($target, $source, $excluded); |
97
|
|
|
|
98
|
18 |
|
File::save($dst, $result); |
99
|
18 |
|
} |
100
|
|
|
|
101
|
18 |
|
protected function isValidation(string $filename): bool |
102
|
|
|
{ |
103
|
18 |
|
return Str::startsWith($filename, 'validation'); |
104
|
|
|
} |
105
|
|
|
|
106
|
18 |
|
protected function isInline(string $filename): bool |
107
|
|
|
{ |
108
|
18 |
|
return Str::contains($filename, 'inline'); |
109
|
|
|
} |
110
|
|
|
|
111
|
18 |
|
protected function excluded(array $array, string $key): array |
112
|
|
|
{ |
113
|
18 |
|
$keys = Config::getExclude($key, [], true); |
114
|
|
|
|
115
|
|
|
return array_filter($array, static function ($value) use ($keys) { |
116
|
12 |
|
return in_array($value, $keys); |
117
|
18 |
|
}, ARRAY_FILTER_USE_KEY); |
118
|
|
|
} |
119
|
|
|
|
120
|
18 |
|
protected function getSourceFilePath(SplFileInfo $file): string |
121
|
|
|
{ |
122
|
18 |
|
if (Config::isInline()) { |
123
|
|
|
$path = $file->getPath(); |
124
|
|
|
$extension = $file->getExtension(); |
125
|
|
|
$basename = $file->getBasename('.' . $extension); |
126
|
|
|
|
127
|
|
|
$inline = $path . '/' . $basename . '-inline.' . $extension; |
128
|
|
|
|
129
|
|
|
return file_exists($inline) |
130
|
|
|
? $inline |
131
|
|
|
: $file->getRealPath(); |
132
|
|
|
} |
133
|
|
|
|
134
|
18 |
|
return $file->getRealPath(); |
135
|
|
|
} |
136
|
|
|
|
137
|
18 |
|
protected function getTargetFilename(SplFileInfo $file): string |
138
|
|
|
{ |
139
|
18 |
|
if ($this->isInline($file->getFilename())) { |
140
|
|
|
return Str::replaceLast('-inline', '', $file->getFilename()); |
141
|
|
|
} |
142
|
|
|
|
143
|
18 |
|
return $file->getFilename(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|