/
Excludes.php
75 lines (56 loc) · 1.79 KB
/
Excludes.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
<?php
namespace LaravelLang\Lang\Processors;
use Helldar\Support\Facades\Helpers\Arr;
use Helldar\Support\Facades\Helpers\Filesystem\File;
final class Excludes extends Processor
{
protected string $target_path = 'excludes';
protected array $keys = [];
protected ?string $stub = 'exclude.stub';
public function run(): void
{
$this->loadKeys();
$this->handle();
}
protected function handle(): void
{
foreach ($this->targetFiles() as $filename) {
$this->process($this->getTargetPath($filename), $filename);
}
}
protected function process(string $target_path, string $filename, string $locale = null): void
{
$target = $this->load($target_path);
$content = $this->merge($this->keys, $target, $filename);
$this->sort($content);
$this->store($target_path, array_values($content), $this->stub, true);
}
protected function merge(array $source, array $target, string $filename): array
{
return array_intersect($source, $target);
}
protected function sort(array &$array): void
{
$array = Arr::sort(array_unique($array));
}
protected function loadKeys(): void
{
foreach ($this->sourceFiles() as $filename) {
$keys = $this->getSourceKeys($filename);
$this->keys = array_merge($this->keys, $keys);
}
}
protected function sourceFiles(): array
{
return File::names($this->getSourcePath(), recursive: true);
}
protected function targetFiles(): array
{
return File::names($this->getTargetPath());
}
protected function getSourceKeys(string $filename): array
{
$items = $this->load($this->getSourcePath($filename));
return array_keys($items);
}
}