1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Processors; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Constants\Status; |
6
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Pathable; |
7
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Processor; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
9
|
|
|
use Helldar\LaravelLangPublisher\Facades\File; |
10
|
|
|
use Helldar\LaravelLangPublisher\Facades\Locale; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use SplFileInfo; |
14
|
|
|
|
15
|
|
|
abstract class BaseProcessor implements Processor |
16
|
|
|
{ |
17
|
|
|
/** @var \Helldar\LaravelLangPublisher\Contracts\Pathable */ |
18
|
|
|
protected $path; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $locale; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
protected $is_force; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $is_full; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $extension = 'php'; |
31
|
|
|
|
32
|
78 |
|
/** @var array */ |
33
|
|
|
protected $result = []; |
34
|
78 |
|
|
35
|
78 |
|
public function __construct(Pathable $path) |
36
|
|
|
{ |
37
|
78 |
|
$this->path = $path; |
38
|
|
|
} |
39
|
78 |
|
|
40
|
|
|
public function locale(string $locale): Processor |
41
|
78 |
|
{ |
42
|
|
|
$this->locale = $locale; |
43
|
|
|
|
44
|
78 |
|
return $this; |
45
|
|
|
} |
46
|
78 |
|
|
47
|
|
|
public function force(bool $is_force = true): Processor |
48
|
78 |
|
{ |
49
|
|
|
$this->is_force = $is_force; |
50
|
|
|
|
51
|
54 |
|
return $this; |
52
|
|
|
} |
53
|
54 |
|
|
54
|
|
|
public function full(bool $is_full = true): Processor |
55
|
|
|
{ |
56
|
54 |
|
$this->is_full = $is_full; |
57
|
|
|
|
58
|
54 |
|
return $this; |
59
|
|
|
} |
60
|
54 |
|
|
61
|
54 |
|
public function result(): array |
62
|
|
|
{ |
63
|
54 |
|
return $this->result; |
64
|
|
|
} |
65
|
54 |
|
|
66
|
24 |
|
protected function push(string $filename, string $status): void |
67
|
30 |
|
{ |
68
|
30 |
|
$locale = $this->locale; |
69
|
|
|
|
70
|
18 |
|
$this->result[] = compact('locale', 'filename', 'status'); |
71
|
|
|
} |
72
|
18 |
|
|
73
|
|
|
protected function checkExists(string $path): void |
74
|
|
|
{ |
75
|
30 |
|
$this->extension === 'php' |
76
|
|
|
? File::directoryExist($path, $this->locale) |
77
|
30 |
|
: File::fileExist($path, $this->locale); |
78
|
12 |
|
} |
79
|
|
|
|
80
|
|
|
protected function isProtected(): bool |
81
|
30 |
|
{ |
82
|
30 |
|
return Locale::isProtected($this->locale); |
83
|
30 |
|
} |
84
|
|
|
|
85
|
30 |
|
protected function publishFile(SplFileInfo $file): void |
86
|
30 |
|
{ |
87
|
30 |
|
if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) { |
88
|
|
|
return; |
89
|
30 |
|
} |
90
|
|
|
|
91
|
|
|
$filename = $this->getTargetFilename($file); |
92
|
|
|
$src_file = $this->getSourceFilePath($file); |
93
|
|
|
$dst_file = $this->targetPath($filename); |
94
|
|
|
|
95
|
54 |
|
if ($this->is_force || ! File::exists($dst_file)) { |
96
|
|
|
$this->copy($src_file, $dst_file, $filename); |
97
|
54 |
|
$this->push($filename, Status::COPIED); |
98
|
|
|
|
99
|
|
|
return; |
100
|
48 |
|
} |
101
|
|
|
|
102
|
48 |
|
$this->push($filename, Status::SKIPPED); |
103
|
|
|
} |
104
|
|
|
|
105
|
30 |
|
protected function resetFile(SplFileInfo $file): void |
106
|
|
|
{ |
107
|
30 |
|
if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) { |
108
|
|
|
return; |
109
|
30 |
|
} |
110
|
12 |
|
|
111
|
30 |
|
$filename = $this->getTargetFilename($file); |
112
|
30 |
|
$src_file = $this->getSourceFilePath($file); |
113
|
|
|
$dst_file = $this->targetPath($filename); |
114
|
12 |
|
|
115
|
|
|
if (File::exists($dst_file)) { |
116
|
12 |
|
$this->reset($src_file, $dst_file, $filename); |
117
|
12 |
|
$this->push($filename, Status::RESET); |
118
|
|
|
|
119
|
12 |
|
return; |
120
|
12 |
|
} |
121
|
|
|
|
122
|
12 |
|
$this->push($filename, Status::SKIPPED); |
123
|
12 |
|
} |
124
|
|
|
|
125
|
12 |
|
protected function sourcePath(): string |
126
|
12 |
|
{ |
127
|
12 |
|
return $this->path->source($this->locale); |
128
|
|
|
} |
129
|
12 |
|
|
130
|
12 |
|
protected function targetPath(string $filename = null): string |
131
|
|
|
{ |
132
|
12 |
|
return $this->path->target($this->locale, $filename); |
133
|
|
|
} |
134
|
12 |
|
|
135
|
12 |
|
protected function copy(string $source, string $target, string $filename): void |
136
|
|
|
{ |
137
|
30 |
|
$this->isValidation($filename) |
138
|
|
|
? $this->copyValidations($source, $target, $filename) |
139
|
30 |
|
: $this->copyOthers($source, $target, $filename); |
140
|
30 |
|
} |
141
|
|
|
|
142
|
30 |
|
protected function reset(string $src, string $dst, string $filename): void |
143
|
|
|
{ |
144
|
30 |
|
$source = File::load($src); |
145
|
|
|
$target = File::load($dst, true); |
146
|
30 |
|
|
147
|
30 |
|
$result = $this->is_full |
148
|
|
|
? $source |
149
|
30 |
|
: array_merge($source, $this->excluded($target, $filename)); |
|
|
|
|
150
|
|
|
|
151
|
30 |
|
File::save($dst, $result); |
152
|
|
|
} |
153
|
|
|
|
154
|
30 |
|
protected function copyValidations(string $src, string $dst, string $filename): void |
155
|
|
|
{ |
156
|
30 |
|
$source = File::load($src); |
157
|
|
|
$target = File::load($dst, true); |
158
|
|
|
|
159
|
30 |
|
$source_custom = Arr::get($source, 'custom', []); |
160
|
|
|
$source_attributes = Arr::get($source, 'attributes', []); |
161
|
30 |
|
|
162
|
|
|
$target_custom = Arr::get($target, 'custom', []); |
163
|
|
|
$target_attributes = Arr::get($target, 'attributes', []); |
164
|
|
|
|
165
|
|
|
$excluded_target = $this->excluded($target, $filename); |
166
|
|
|
$excluded_custom = $this->excluded($target_custom, $filename); |
167
|
|
|
$excluded_attributes = $this->excluded($target_attributes, $filename); |
168
|
|
|
|
169
|
|
|
$custom = array_merge($source_custom, $target_custom, $excluded_custom); |
170
|
|
|
$attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes); |
171
|
|
|
|
172
|
|
|
$result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes')); |
173
|
30 |
|
|
174
|
|
|
File::save($dst, $result); |
175
|
|
|
} |
176
|
30 |
|
|
177
|
|
|
protected function copyOthers(string $src, string $dst, string $filename): void |
178
|
30 |
|
{ |
179
|
|
|
$source = File::load($src); |
180
|
|
|
$target = File::load($dst, true); |
181
|
|
|
|
182
|
30 |
|
$excluded = $this->excluded($target, $filename); |
183
|
|
|
|
184
|
|
|
$result = array_merge($target, $source, $excluded); |
185
|
|
|
|
186
|
|
|
File::save($dst, $result); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function isValidation(string $filename): bool |
190
|
|
|
{ |
191
|
|
|
return Str::startsWith($filename, 'validation'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function isInline(string $filename): bool |
195
|
|
|
{ |
196
|
|
|
return Str::contains($filename, 'inline'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function wantsJson(): bool |
200
|
|
|
{ |
201
|
|
|
return $this->extension === 'json'; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function getSourceFilePath(SplFileInfo $file): string |
205
|
|
|
{ |
206
|
|
|
if (Config::isInline()) { |
207
|
|
|
$path = $file->getPath(); |
208
|
|
|
$extension = $file->getExtension(); |
209
|
|
|
$basename = $file->getBasename('.' . $extension); |
210
|
|
|
|
211
|
|
|
$inline = $path . '/' . $basename . '-inline.' . $extension; |
212
|
|
|
|
213
|
|
|
return file_exists($inline) |
214
|
|
|
? $inline |
215
|
|
|
: $file->getRealPath(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $file->getRealPath(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function getTargetFilename(SplFileInfo $file): string |
222
|
|
|
{ |
223
|
|
|
if ($this->isInline($file->getFilename())) { |
224
|
|
|
return Str::replaceLast('-inline', '', $file->getFilename()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $file->getFilename(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|