generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTranslationExtractor.php
More file actions
121 lines (104 loc) · 3.08 KB
/
TranslationExtractor.php
File metadata and controls
121 lines (104 loc) · 3.08 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace Yiisoft\Translator\Extractor;
use RuntimeException;
use Yiisoft\Files\FileHelper;
use Yiisoft\Files\PathMatcher\PathMatcher;
/**
* Extracts translator IDs from files within a given path.
*/
final class TranslationExtractor
{
private string $path;
/** @var string[] */
private array $only = ['**.php'];
private array $skippedLines = [];
/** @var string[] */
private array $except = [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
];
/**
* `TranslationExtractor` constructor.
*
* @param string $path Path to start extraction at.
* @param string[]|null $only List of patterns that the files or directories should match. See {@see PathMatcher}.
* @param string[]|null $except List of patterns that the files or directories should not match. See
* {@see PathMatcher}.
*/
public function __construct(string $path, ?array $only = null, ?array $except = null)
{
if (!is_dir($path)) {
throw new RuntimeException(sprintf('Directory "%s" does not exist.', $path));
}
$this->path = $path;
if ($only !== null) {
$this->only = $only;
}
if ($except !== null) {
$this->except = $except;
}
}
/**
* Extract messages.
*
* @param string $defaultCategory Category to use if category isn't set in translation call.
* @param string|null $translatorCall Translation call to look for.
*
* @return array Extracted messages.
*/
public function extract(string $defaultCategory = 'app', ?string $translatorCall = null): array
{
$messages = [];
$parser = new ContentParser($defaultCategory, $translatorCall);
$files = FileHelper::findFiles($this->path, [
'filter' => (new PathMatcher())
->only(...$this->only)
->except(...$this->except),
'recursive' => true,
]);
foreach ($files as $file) {
/**
* @var string $fileContent We assume that `file_get_contents` can always read the file.
*/
$fileContent = file_get_contents($file);
$messages = array_merge_recursive($messages, $parser->extract($fileContent));
if ($parser->hasSkippedLines()) {
$this->skippedLines[$file] = $parser->getSkippedLines();
}
}
return $messages;
}
/**
* @return bool Whether there are skipped lines.
*/
public function hasSkippedLines(): bool
{
return !empty($this->skippedLines);
}
/**
* @return array Lines that were skipped during parsing.
*
* The format is:
*
* ```php
* return [
* 'fileName' => [
* [
* int $numberOfLine,
* string $incorrectLine,
* ],
* ],
* ]
* ```
*/
public function getSkippedLines(): array
{
return $this->skippedLines;
}
}