1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "andrey-helldar/laravel-lang-publisher" project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @author Andrey Helldar <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @copyright 2021 Andrey Helldar |
12
|
|
|
* |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* @see https://github.com/andrey-helldar/laravel-lang-publisher |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Helldar\LaravelLangPublisher\Processors; |
21
|
|
|
|
22
|
|
|
use Helldar\Contracts\LangPublisher\Processor; |
23
|
|
|
use Helldar\Contracts\LangPublisher\Provider; |
24
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Paths; |
25
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales; |
26
|
|
|
use Helldar\LaravelLangPublisher\Constants\Path; |
27
|
|
|
use Illuminate\Support\Arr; |
28
|
|
|
use Illuminate\Support\Str; |
29
|
|
|
|
30
|
|
|
abstract class BaseProcessor implements Processor |
31
|
|
|
{ |
32
|
|
|
use Paths; |
33
|
|
|
|
34
|
|
|
protected $force = false; |
35
|
|
|
|
36
|
|
|
protected $locales = []; |
37
|
|
|
|
38
|
|
|
protected $source_keys = []; |
39
|
|
|
|
40
|
|
|
protected $locales_keys = []; |
41
|
|
|
|
42
|
|
|
public function locales(array $locales): Processor |
43
|
|
|
{ |
44
|
|
|
$this->locales = $locales; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function provider(Provider $provider): Processor |
50
|
|
|
{ |
51
|
|
|
foreach ($provider->plugins() as $plugin) { |
52
|
|
|
if (! $plugin->has()) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($plugin->source() as $source) { |
57
|
|
|
$this->loadSource($provider, $source, $plugin->target()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function store(): void |
65
|
|
|
{ |
66
|
|
|
// TODO: Implement store() method. |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function hasForce(bool $force = false): Processor |
70
|
|
|
{ |
71
|
|
|
$this->force = $force; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function loadSource(Provider $provider, string $source, string $target): void |
77
|
|
|
{ |
78
|
|
|
$path = $this->path($provider->basePath(), Path::SOURCE, $source); |
79
|
|
|
|
80
|
|
|
$filename = $this->preparePath($target, Locales::ENGLISH); |
81
|
|
|
|
82
|
|
|
$content = Filesystem::load($path); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->set($this->source_keys, $filename, $content); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function set(array &$array, string $key, array $values): void |
88
|
|
|
{ |
89
|
|
|
$loaded = Arr::get($array, $key, []); |
90
|
|
|
|
91
|
|
|
Arr::set($array, $key, array_merge_recursive($loaded, $values)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function preparePath(string $path, string $locale): string |
95
|
|
|
{ |
96
|
|
|
return Str::replace('{locale}', $locale, $path); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|