1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Processors; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Containable; |
6
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Contains; |
7
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
8
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Pathable; |
9
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Processor as Contract; |
10
|
|
|
use Helldar\LaravelLangPublisher\Services\Comparators\Manage; |
11
|
|
|
use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; |
12
|
|
|
use Helldar\Support\Concerns\Makeable; |
13
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
14
|
|
|
|
15
|
|
|
abstract class Processor implements Contract |
16
|
|
|
{ |
17
|
|
|
use Containable; |
18
|
|
|
use Contains; |
19
|
|
|
use Logger; |
|
|
|
|
20
|
|
|
use Makeable; |
21
|
|
|
use Pathable; |
22
|
|
|
|
23
|
|
|
protected $package; |
24
|
|
|
|
25
|
|
|
protected $locale; |
26
|
|
|
|
27
|
|
|
protected $source_path; |
28
|
|
|
|
29
|
|
|
protected $target_path; |
30
|
|
|
|
31
|
|
|
protected $force; |
32
|
|
|
|
33
|
|
|
protected $full = false; |
34
|
|
|
|
35
|
|
|
public function package(string $package): Contract |
36
|
|
|
{ |
37
|
|
|
$this->package = $package; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function locale(string $locale): Contract |
43
|
|
|
{ |
44
|
|
|
$this->locale = $locale; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function sourceFilename(string $filename, bool $is_inline = true): Contract |
50
|
|
|
{ |
51
|
|
|
$this->setSourcePath($filename, $is_inline); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function targetFilename(string $filename): Contract |
57
|
|
|
{ |
58
|
|
|
$this->setTargetPath($filename); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function force(bool $force = false): Contract |
64
|
|
|
{ |
65
|
|
|
$this->force = $force; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function full(bool $full = false): Contract |
71
|
|
|
{ |
72
|
|
|
$this->full = $full; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function whenPackage(?string $package): Contract |
78
|
|
|
{ |
79
|
|
|
if ($package) { |
80
|
|
|
$this->package($package); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function whenLocale(?string $locale): Contract |
87
|
|
|
{ |
88
|
|
|
if ($locale) { |
89
|
|
|
$this->locale($locale); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function whenSourceFilename(?string $filename, bool $is_inline = true): Contract |
96
|
|
|
{ |
97
|
|
|
if ($filename) { |
98
|
|
|
$this->sourceFilename($filename, $is_inline); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function whenTargetFilename(?string $filename): Contract |
105
|
|
|
{ |
106
|
|
|
if ($filename) { |
107
|
|
|
$this->targetFilename($filename); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function main(): void |
114
|
|
|
{ |
115
|
|
|
$this->process($this->source_path, $this->target_path); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function process(string $source_path, string $target_path): void |
119
|
|
|
{ |
120
|
|
|
$source = $this->load($source_path); |
121
|
|
|
$target = $this->load($target_path); |
122
|
|
|
|
123
|
|
|
$result = $this->compare($source, $target); |
124
|
|
|
|
125
|
|
|
$this->store($target_path, $result); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function setSourcePath(string $filename, bool $is_inline): void |
129
|
|
|
{ |
130
|
|
|
$this->log('Setting the path to the source file:', $filename); |
131
|
|
|
|
132
|
|
|
if ($this->isValidation($filename) && $is_inline) { |
133
|
|
|
$this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline validator messages. Processing in progress...'); |
134
|
|
|
|
135
|
|
|
$name = $this->pathFilename($filename); |
136
|
|
|
$extension = $this->pathExtension($filename); |
137
|
|
|
|
138
|
|
|
$filename = $name . '-inline.' . $extension; |
139
|
|
|
} elseif ($this->isJsonMain($filename)) { |
140
|
|
|
$filename = $this->locale . '.json'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function setTargetPath(string $filename): void |
147
|
|
|
{ |
148
|
|
|
$this->log('Setting the path to the target file:', $filename); |
149
|
|
|
|
150
|
|
|
$this->target_path = $this->pathTargetFull($this->locale, $filename); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function compare(array $source, array $target): array |
154
|
|
|
{ |
155
|
|
|
$this->log('Find an object and perform object comparison.'); |
156
|
|
|
|
157
|
|
|
return Manage::make() |
158
|
|
|
->filename($this->source_path) |
159
|
|
|
->full($this->full) |
160
|
|
|
->source($source) |
161
|
|
|
->target($target) |
162
|
|
|
->find() |
163
|
|
|
->toArray(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function load(string $path): array |
167
|
|
|
{ |
168
|
|
|
$this->log('Loading an array:', $path); |
169
|
|
|
|
170
|
|
|
return $this->manager()->load($path); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function store(string $path, array $content): void |
174
|
|
|
{ |
175
|
|
|
$this->log('Saving an array to a file:', $path); |
176
|
|
|
|
177
|
|
|
$this->manager()->store($path, $content); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
protected function manager(): Manager |
181
|
|
|
{ |
182
|
|
|
$this->log('Getting a comparison object...'); |
183
|
|
|
|
184
|
|
|
return $this->container(Manager::class); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function doesntExists(): bool |
188
|
|
|
{ |
189
|
|
|
$this->log('Checking for the existence of a file:', $this->target_path); |
190
|
|
|
|
191
|
|
|
return ! File::exists($this->target_path); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|