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
|
14 |
|
public function package(string $package): Contract |
36
|
|
|
{ |
37
|
14 |
|
$this->package = $package; |
38
|
|
|
|
39
|
14 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
15 |
|
public function locale(string $locale): Contract |
43
|
|
|
{ |
44
|
15 |
|
$this->locale = $locale; |
45
|
|
|
|
46
|
15 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
14 |
|
public function filename(string $filename, bool $is_inline = true): Contract |
50
|
|
|
{ |
51
|
14 |
|
$this->setSourcePath($filename, $is_inline); |
52
|
14 |
|
$this->setTargetPath($filename); |
53
|
|
|
|
54
|
14 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
15 |
|
public function force(bool $force = false): Contract |
58
|
|
|
{ |
59
|
15 |
|
$this->force = $force; |
60
|
|
|
|
61
|
15 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public function full(bool $full = false): Contract |
65
|
|
|
{ |
66
|
2 |
|
$this->full = $full; |
67
|
|
|
|
68
|
2 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
15 |
|
public function whenPackage(?string $package): Contract |
72
|
|
|
{ |
73
|
15 |
|
if ($package) { |
74
|
14 |
|
$this->package($package); |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
15 |
|
public function whenLocale(?string $locale): Contract |
81
|
|
|
{ |
82
|
15 |
|
if ($locale) { |
83
|
15 |
|
$this->locale($locale); |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
15 |
|
public function whenFilename(?string $filename, bool $is_inline = true): Contract |
90
|
|
|
{ |
91
|
15 |
|
if ($filename) { |
92
|
14 |
|
$this->filename($filename, $is_inline); |
93
|
|
|
} |
94
|
|
|
|
95
|
15 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
protected function setSourcePath(string $filename, bool $is_inline): void |
99
|
|
|
{ |
100
|
14 |
|
$this->log('Setting the path to the source file:', $filename); |
101
|
|
|
|
102
|
14 |
|
if ($this->isValidation($filename) && $is_inline) { |
103
|
14 |
|
$this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline validator messages. Processing in progress...'); |
104
|
|
|
|
105
|
14 |
|
$name = $this->pathFilename($filename); |
106
|
14 |
|
$extension = $this->pathExtension($filename); |
107
|
|
|
|
108
|
14 |
|
$filename = $name . '-inline.' . $extension; |
109
|
14 |
|
} elseif ($this->isJson($filename)) { |
110
|
14 |
|
$filename = $this->locale . '.json'; |
111
|
|
|
} |
112
|
|
|
|
113
|
14 |
|
$this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename; |
114
|
14 |
|
} |
115
|
|
|
|
116
|
14 |
|
protected function setTargetPath(string $filename): void |
117
|
|
|
{ |
118
|
14 |
|
$this->log('Setting the path to the target file:', $filename); |
119
|
|
|
|
120
|
14 |
|
$is_json = $this->isJson($filename); |
121
|
|
|
|
122
|
14 |
|
$this->target_path = $this->pathTargetFull($this->locale, $filename, $is_json); |
123
|
14 |
|
} |
124
|
|
|
|
125
|
14 |
|
protected function compare(array $source, array $target): array |
126
|
|
|
{ |
127
|
14 |
|
$this->log('Find an object and perform object comparison.'); |
128
|
|
|
|
129
|
14 |
|
return Manage::make() |
130
|
14 |
|
->filename($this->source_path) |
131
|
14 |
|
->full($this->full) |
132
|
14 |
|
->source($source) |
133
|
14 |
|
->target($target) |
134
|
14 |
|
->find() |
135
|
14 |
|
->toArray(); |
136
|
|
|
} |
137
|
|
|
|
138
|
14 |
|
protected function load(string $path): array |
139
|
|
|
{ |
140
|
14 |
|
$this->log('Loading an array:', $path); |
141
|
|
|
|
142
|
14 |
|
return $this->manager()->load($path); |
143
|
|
|
} |
144
|
|
|
|
145
|
14 |
|
protected function store(string $path, array $content): void |
146
|
|
|
{ |
147
|
14 |
|
$this->log('Saving an array to a file:', $path); |
148
|
|
|
|
149
|
14 |
|
$this->manager()->store($path, $content); |
150
|
14 |
|
} |
151
|
|
|
|
152
|
14 |
|
protected function manager(): Manager |
153
|
|
|
{ |
154
|
14 |
|
$this->log('Getting a comparison object...'); |
155
|
|
|
|
156
|
14 |
|
return $this->container(Manager::class); |
157
|
|
|
} |
158
|
|
|
|
159
|
7 |
|
protected function doesntExists(): bool |
160
|
|
|
{ |
161
|
7 |
|
$this->log('Checking for the existence of a file:', $this->target_path); |
162
|
|
|
|
163
|
7 |
|
return ! File::exists($this->target_path); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|