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\Contracts\Processor as Contract; |
9
|
|
|
use Helldar\LaravelLangPublisher\Facades\Path; |
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
|
|
|
|
22
|
|
|
protected $locale; |
23
|
|
|
|
24
|
|
|
protected $source_path; |
25
|
|
|
|
26
|
|
|
protected $target_path; |
27
|
|
|
|
28
|
|
|
protected $force; |
29
|
|
|
|
30
|
|
|
protected $full = false; |
31
|
|
|
|
32
|
11 |
|
public function locale(string $locale): Contract |
33
|
|
|
{ |
34
|
11 |
|
$this->locale = $locale; |
35
|
|
|
|
36
|
11 |
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
11 |
|
public function filename(string $filename, bool $is_inline): Contract |
40
|
|
|
{ |
41
|
11 |
|
$this->setSourcePath($filename, $is_inline); |
42
|
11 |
|
$this->setTargetPath($filename); |
43
|
|
|
|
44
|
11 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
11 |
|
public function force(bool $force = false): Contract |
48
|
|
|
{ |
49
|
11 |
|
$this->force = $force; |
50
|
|
|
|
51
|
11 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function full(bool $full = false): Contract |
55
|
|
|
{ |
56
|
2 |
|
$this->full = $full; |
57
|
|
|
|
58
|
2 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
11 |
|
protected function setSourcePath(string $filename, bool $is_inline): void |
62
|
|
|
{ |
63
|
11 |
|
$this->log('Setting the path to the source file: ' . $filename); |
64
|
|
|
|
65
|
11 |
|
if ($this->isValidation($filename) && $is_inline) { |
66
|
|
|
$this->log('The "' . $filename . '" file is a collection of inline validator messages. Processing in progress...'); |
67
|
|
|
|
68
|
|
|
$name = Path::filename($filename); |
69
|
|
|
$extension = Path::extension($filename); |
70
|
|
|
|
71
|
|
|
$filename = $name . '-inline.' . $extension; |
72
|
11 |
|
} elseif ($this->isJson($filename)) { |
73
|
11 |
|
$filename = $this->locale . '.json'; |
74
|
|
|
} |
75
|
|
|
|
76
|
11 |
|
$this->source_path = Path::source($this->locale) . '/' . $filename; |
77
|
11 |
|
} |
78
|
|
|
|
79
|
11 |
|
protected function setTargetPath(string $filename): void |
80
|
|
|
{ |
81
|
11 |
|
$this->log('Setting the path to the target file: ' . $filename); |
82
|
|
|
|
83
|
11 |
|
$is_json = $this->isJson($filename); |
84
|
|
|
|
85
|
11 |
|
$this->target_path = Path::targetFull($this->locale, $filename, $is_json); |
86
|
11 |
|
} |
87
|
|
|
|
88
|
11 |
|
protected function compare(array $source, array $target): array |
89
|
|
|
{ |
90
|
11 |
|
$this->log('Find an object and perform object comparison.'); |
91
|
|
|
|
92
|
11 |
|
return Manage::make() |
93
|
11 |
|
->filename($this->source_path) |
94
|
11 |
|
->full($this->full) |
95
|
11 |
|
->source($source) |
96
|
11 |
|
->target($target) |
97
|
11 |
|
->find() |
98
|
11 |
|
->toArray(); |
99
|
|
|
} |
100
|
|
|
|
101
|
11 |
|
protected function load(string $path): array |
102
|
|
|
{ |
103
|
11 |
|
$this->log('Loading an array: ' . $path); |
104
|
|
|
|
105
|
11 |
|
return $this->manager()->load($path); |
106
|
|
|
} |
107
|
|
|
|
108
|
11 |
|
protected function store(string $path, array $content): void |
109
|
|
|
{ |
110
|
11 |
|
$this->log('Saving an array to a file: ' . $path); |
111
|
|
|
|
112
|
11 |
|
$this->manager()->store($path, $content); |
113
|
11 |
|
} |
114
|
|
|
|
115
|
11 |
|
protected function manager(): Manager |
116
|
|
|
{ |
117
|
11 |
|
$this->log('Getting a comparison object...'); |
118
|
|
|
|
119
|
11 |
|
return $this->container(Manager::class); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function directory(string $path): string |
123
|
|
|
{ |
124
|
|
|
$this->log('Getting the directory name for a path: ' . $path); |
125
|
|
|
|
126
|
|
|
return Path::directory($path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function extension(string $path): string |
130
|
|
|
{ |
131
|
|
|
$this->log('Getting the file extension for a path: ' . $path); |
132
|
|
|
|
133
|
|
|
return Path::extension($path); |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
protected function exists(): bool |
137
|
|
|
{ |
138
|
4 |
|
$this->log('Checking for the existence of a file: ' . $this->target_path); |
139
|
|
|
|
140
|
4 |
|
return File::exists($this->target_path); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|