/
Processor.php
163 lines (123 loc) · 4.65 KB
/
Processor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace LaravelLang\Lang\Processors;
use Helldar\Support\Concerns\Makeable;
use Helldar\Support\Facades\Helpers\Arr;
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
use Helldar\Support\Tools\Stub;
use LaravelLang\Lang\Application;
use LaravelLang\Lang\Concerns\Contains;
use LaravelLang\Lang\Contracts\Filesystem;
use LaravelLang\Lang\Contracts\Processable;
use LaravelLang\Lang\Contracts\Stringable;
use LaravelLang\Lang\Services\Filesystem\Base;
use LaravelLang\Lang\Services\Filesystem\Json as JsonFilesystem;
use LaravelLang\Lang\Services\Filesystem\Markdown as MarkdownFilesystem;
use LaravelLang\Lang\Services\Filesystem\Php as PhpFilesystem;
abstract class Processor implements Processable
{
use Contains;
use Makeable;
protected Application $app;
protected array $source = [];
protected string $target_path;
/** @var \LaravelLang\Lang\Contracts\Filesystem[]|array */
protected array $filesystems = [];
public function application(Application $app): Processable
{
$this->app = $app;
return $this;
}
protected function merge(array $source, array $target, string $filename): array
{
$target = $this->filter($target, $source);
$this->sort($source);
$this->sort($target);
if ($this->isValidation($filename)) {
$custom = $this->getFallbackValue($source, $target, 'custom');
$attributes = $this->getFallbackValue($source, $target, 'attributes');
$source = Arr::except($source, ['custom', 'attributes']);
$target = Arr::except($target, ['custom', 'attributes']);
return array_merge($source, $target, compact('custom', 'attributes'));
}
return array_merge($source, $target);
}
protected function process(string $target_path, string $filename, string $locale = null): void
{
$source = $this->source($filename);
$target = $this->load($target_path);
$content = $this->merge($source, $target, $filename);
$this->store($target_path, $content, $filename);
}
protected function source(string $filename): array
{
if ($this->source[$filename] ?? false) {
return $this->source[$filename];
}
return $this->source[$filename] = $this->load($this->getSourcePath($filename));
}
protected function getFallbackValue(array $source, array $target, string $key)
{
return Arr::get($target, $key) ?: Arr::get($source, $key, []);
}
protected function getTargetPath(string $path = null): string
{
if ($path = $this->app->cleanPath($path)) {
$path = '/' . $path;
}
return $this->app->path($this->target_path . $path);
}
protected function getSourcePath(string $filename = null): string
{
return $this->app->sourcePath($filename);
}
protected function filter(array $first, array $second): array
{
$keys = array_keys($second);
return Arr::only($first, $keys);
}
protected function sort(array &$array): void
{
$array = Arr::ksort($array);
}
protected function load(string $path): array
{
return $this->getFilesystem($path)->load($path);
}
protected function locales(): array
{
return Directory::names($this->getTargetPath());
}
protected function resolveFilename(string $filename, string $locale): string
{
return $this->isMainJson($filename) ? $locale . '.json' : $filename;
}
protected function getFilesystem(string $filename): Filesystem
{
$class = $this->getFilesystemClass($filename);
if ($this->filesystems[$class] ?? false) {
return $this->filesystems[$class];
}
return $this->filesystems[$class] = $this->loadFilesystem($class);
}
protected function getFilesystemClass(string $path): string
{
return match (true) {
$this->isJson($path) => JsonFilesystem::class,
$this->isMarkdown($path) => MarkdownFilesystem::class,
default => PhpFilesystem::class,
};
}
protected function loadFilesystem(Filesystem|Base|string $filesystem): Filesystem
{
return $filesystem::make()->application($this->app);
}
protected function store(string $path, array|string|Stringable $content, string $source_filename = null, bool $is_simple = false): void
{
$stub = $this->getStubPath($source_filename);
$this->getFilesystem($path)->store($path, $content, $is_simple, $stub);
}
protected function getStubPath(?string $filename): ?string
{
return empty($filename) ? Stub::PHP_ARRAY : $filename;
}
}