1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Translator\Extractor; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Files\FileHelper; |
8
|
|
|
use Yiisoft\Files\PathMatcher\PathMatcher; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Extracts translator IDs from files within a given path. |
12
|
|
|
*/ |
13
|
|
|
final class TranslationExtractor |
14
|
|
|
{ |
15
|
|
|
private string $path; |
16
|
|
|
|
17
|
|
|
/** @var string[] */ |
18
|
|
|
private array $only = ['**.php']; |
19
|
|
|
|
20
|
|
|
/** @psalm-var array<string, array<array<string|array{0: int, 1: string, 2: int}>>> */ |
21
|
|
|
private array $skippedLines = []; |
22
|
|
|
|
23
|
|
|
/** @var string[] */ |
24
|
|
|
private array $except = [ |
25
|
|
|
'.svn', |
26
|
|
|
'.git', |
27
|
|
|
'.gitignore', |
28
|
|
|
'.gitkeep', |
29
|
|
|
'.hgignore', |
30
|
|
|
'.hgkeep', |
31
|
|
|
'/messages', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* TranslationExtractor constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $path |
38
|
|
|
* @param string[]|null $only |
39
|
|
|
* @param string[]|null $except |
40
|
|
|
*/ |
41
|
7 |
|
public function __construct(string $path, ?array $only = null, ?array $except = null) |
42
|
|
|
{ |
43
|
7 |
|
if (!is_dir($path)) { |
44
|
1 |
|
throw new \RuntimeException(sprintf('Directory "%s" does not exist.', $path)); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$this->path = $path; |
48
|
|
|
|
49
|
6 |
|
if ($only !== null) { |
50
|
2 |
|
$this->only = $only; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
if ($except !== null) { |
54
|
2 |
|
$this->except = $except; |
55
|
|
|
} |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string|null $defaultCategory |
60
|
|
|
* @param string|null $translator |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
6 |
|
public function extract(?string $defaultCategory = null, ?string $translator = null): array |
65
|
|
|
{ |
66
|
6 |
|
$messages = []; |
67
|
6 |
|
$parser = new ContentParser($defaultCategory, $translator); |
68
|
|
|
|
69
|
6 |
|
$files = FileHelper::findFiles($this->path, [ |
70
|
6 |
|
'filter' => (new pathMatcher())->only(...$this->only)->except(...$this->except), |
71
|
|
|
'recursive' => true, |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
/** @var string[] $files */ |
75
|
6 |
|
foreach ($files as $file) { |
76
|
6 |
|
$fileContent = file_get_contents($file); |
77
|
6 |
|
$messages = array_merge_recursive($messages, $parser->extract($fileContent)); |
78
|
6 |
|
if ($parser->hasSkippedLines()) { |
79
|
4 |
|
$this->skippedLines[$file] = $parser->getSkippedLines(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
return $messages; |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
public function hasSkippedLines(): bool |
87
|
|
|
{ |
88
|
6 |
|
return !empty($this->skippedLines); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @psalm-return array<string, array<array<string|array{0: int, 1: string, 2: int}>>> |
93
|
|
|
*/ |
94
|
4 |
|
public function getSkippedLines(): array |
95
|
|
|
{ |
96
|
4 |
|
return $this->skippedLines; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|