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