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
|
|
|
private array $skippedLines = []; |
21
|
|
|
|
22
|
|
|
/** @var string[] */ |
23
|
|
|
private array $except = [ |
24
|
|
|
'.svn', |
25
|
|
|
'.git', |
26
|
|
|
'.gitignore', |
27
|
|
|
'.gitkeep', |
28
|
|
|
'.hgignore', |
29
|
|
|
'.hgkeep', |
30
|
|
|
'/messages', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* TranslationExtractor constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $path |
37
|
|
|
* @param string[]|null $only |
38
|
|
|
* @param string[]|null $except |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct(string $path, ?array $only = null, ?array $except = null) |
41
|
|
|
{ |
42
|
7 |
|
if (!is_dir($path)) { |
43
|
1 |
|
throw new \RuntimeException(sprintf('Directory "%s" does not exist.', $path)); |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
$this->path = $path; |
47
|
|
|
|
48
|
6 |
|
if ($only !== null) { |
49
|
2 |
|
$this->only = $only; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
if ($except !== null) { |
53
|
2 |
|
$this->except = $except; |
54
|
|
|
} |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string|null $defaultCategory |
59
|
|
|
* @param string|null $translator |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
6 |
|
public function extract(?string $defaultCategory = null, ?string $translator = null): array |
64
|
|
|
{ |
65
|
6 |
|
$messages = []; |
66
|
6 |
|
$parser = new ContentParser($defaultCategory, $translator); |
67
|
|
|
|
68
|
6 |
|
$files = FileHelper::findFiles($this->path, [ |
69
|
6 |
|
'filter' => (new pathMatcher())->only(...$this->only)->except(...$this->except), |
70
|
|
|
'recursive' => true, |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
/** @var string[] $files */ |
74
|
6 |
|
foreach ($files as $file) { |
75
|
6 |
|
$fileContent = file_get_contents($file); |
76
|
6 |
|
$messages = array_merge_recursive($messages, $parser->extract($fileContent)); |
77
|
6 |
|
if ($parser->hasSkippedLines()) { |
78
|
4 |
|
$this->skippedLines[$file] = $parser->getSkippedLines(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
return $messages; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
public function hasSkippedLines(): bool |
86
|
|
|
{ |
87
|
6 |
|
return !empty($this->skippedLines); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* ```php |
92
|
|
|
* return [ |
93
|
|
|
* 'fileName' => [ |
94
|
|
|
* [ |
95
|
|
|
* int $numberOfLine, |
96
|
|
|
* string $incorrectLine, |
97
|
|
|
* ], |
98
|
|
|
* ], |
99
|
|
|
* ] |
100
|
|
|
* ``` |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
4 |
|
public function getSkippedLines(): array |
105
|
|
|
{ |
106
|
4 |
|
return $this->skippedLines; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|