1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Extractor\FileExtractor; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
15
|
|
|
use Translation\Extractor\Model\SourceCollection; |
16
|
|
|
use Translation\Extractor\Model\SourceLocation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class BladeFileExtractor implements FileExtractor |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
3 |
|
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void |
27
|
|
|
{ |
28
|
3 |
|
$realPath = $file->getRealPath(); |
29
|
3 |
|
$messages = $this->findTranslations($file); |
30
|
3 |
|
foreach ($messages as $message) { |
31
|
3 |
|
$collection->addLocation(new SourceLocation($message, $realPath, 0)); |
32
|
|
|
} |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @author uusa35 and contributors to {@link https://github.com/barryvdh/laravel-translation-manager} |
37
|
|
|
* |
38
|
|
|
* @return string[] |
39
|
|
|
*/ |
40
|
3 |
|
public function findTranslations(SplFileInfo $file): array |
41
|
|
|
{ |
42
|
3 |
|
$keys = []; |
43
|
3 |
|
$functions = ['trans', 'trans_choice', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice']; |
44
|
|
|
$pattern = // See http://regexr.com/392hu |
45
|
|
|
"[^\w|>]".// Must not have an alphanum or _ or > before real method |
46
|
3 |
|
'('.implode('|', $functions).')'.// Must start with one of the functions |
47
|
3 |
|
"\(".// Match opening parenthese |
48
|
3 |
|
"[\'\"]".// Match " or ' |
49
|
3 |
|
'('.// Start a new group to match: |
50
|
3 |
|
'[a-zA-Z0-9_-]+'.// Must start with group |
51
|
3 |
|
'([.][^\\1)]+)+'.// Be followed by one or more items/keys |
52
|
3 |
|
')'.// Close group |
53
|
3 |
|
"[\'\"]".// Closing quote |
54
|
3 |
|
"[\),]"; // Close parentheses or new parameter |
55
|
|
|
|
56
|
|
|
// Search the current file for the pattern |
57
|
3 |
|
if (preg_match_all("/$pattern/siU", $file->getContents(), $matches)) { |
58
|
|
|
// Get all matches |
59
|
3 |
|
foreach ($matches[2] as $key) { |
60
|
3 |
|
$keys[] = $key; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $keys; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function supportsExtension(string $extension): bool |
71
|
|
|
{ |
72
|
|
|
return 'blade.php' === $extension; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|