|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\PrettyArray\Services\File as Pretty; |
|
6
|
|
|
use Helldar\Support\Facades\Helpers\Ables\Arrayable; |
|
7
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
|
8
|
|
|
|
|
9
|
|
|
final class Json extends Filesystem |
|
10
|
28 |
|
{ |
|
11
|
|
|
public function load(string $path, string $main_path = null): array |
|
12
|
28 |
|
{ |
|
13
|
|
|
$this->log('Loading the contents of the file:', $path, '(', $main_path, ')'); |
|
14
|
28 |
|
|
|
15
|
28 |
|
if ($this->doesntExists($path) || ($main_path && $this->doesntExists($main_path))) { |
|
16
|
|
|
$path = $this->doesntExists($path) ? $path : $main_path; |
|
17
|
28 |
|
|
|
18
|
|
|
$this->log('File not found:', $path); |
|
19
|
|
|
|
|
20
|
28 |
|
return []; |
|
21
|
|
|
} |
|
22
|
28 |
|
|
|
23
|
|
|
if (! empty($path) && empty($main_path)) { |
|
24
|
28 |
|
return $this->loadTranslations($path); |
|
25
|
|
|
} |
|
26
|
28 |
|
|
|
27
|
|
|
$keys = $this->loadKeys($path); |
|
28
|
|
|
$source = $this->loadTranslations($main_path); |
|
|
|
|
|
|
29
|
28 |
|
|
|
30
|
|
|
return Arr::only($source, $keys); |
|
31
|
28 |
|
} |
|
32
|
|
|
|
|
33
|
28 |
|
public function store(string $path, array $content) |
|
34
|
28 |
|
{ |
|
35
|
|
|
$this->log('Saving an array to a file:', $path); |
|
36
|
|
|
|
|
37
|
|
|
Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function loadKeys(string $path): array |
|
41
|
|
|
{ |
|
42
|
|
|
$this->log('Loading keys from a file:', $path); |
|
43
|
|
|
|
|
44
|
|
|
return $this->loadFile($path); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function loadTranslations(string $path): array |
|
48
|
|
|
{ |
|
49
|
|
|
$this->log('Loading translations from a file:', $path); |
|
50
|
|
|
|
|
51
|
|
|
$items = $this->loadFile($path); |
|
52
|
|
|
|
|
53
|
|
|
return Arrayable::of($items) |
|
54
|
|
|
->renameKeys(static function ($key, $value) { |
|
55
|
|
|
return is_numeric($key) ? $value : $key; |
|
56
|
|
|
})->get(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function loadFile(string $path): array |
|
60
|
|
|
{ |
|
61
|
|
|
$this->log('Loading data from a file:', $path); |
|
62
|
|
|
|
|
63
|
|
|
$content = Pretty::make()->loadRaw($path); |
|
64
|
|
|
|
|
65
|
|
|
$items = json_decode($content, true); |
|
66
|
|
|
|
|
67
|
|
|
return $this->correctValues($items); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|