|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Gettext\Scanner; |
|
5
|
|
|
|
|
6
|
|
|
use Gettext\Translations; |
|
7
|
|
|
use Gettext\Translation; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base class with common funtions for all scanners |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Scanner implements ScannerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
protected $translations; |
|
16
|
|
|
protected $defaultDomain; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Translations ...$allTranslations) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->setTranslations(...$allTranslations); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setDefaultDomain(string $defaultDomain): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->defaultDomain = $defaultDomain; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getDefaultDomain(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->defaultDomain; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setTranslations(Translations ...$allTranslations): void |
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($allTranslations as $translations) { |
|
36
|
|
|
$domain = $translations->getDomain(); |
|
37
|
|
|
$this->translations[$domain] = $translations; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getTranslations(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return array_values($this->translations); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function scanFile(string $filename): void |
|
47
|
|
|
{ |
|
48
|
|
|
$string = static::readFile($filename); |
|
49
|
|
|
|
|
50
|
|
|
$this->scanString($string, $filename); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function scanString(string $string, string $filename = null): void |
|
54
|
|
|
{ |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function saveTranslation(?string $domain, ?string $context, string $original, string $plural = null): ?Translation |
|
58
|
|
|
{ |
|
59
|
|
|
if (is_null($domain)) { |
|
60
|
|
|
$domain = $this->defaultDomain; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!isset($this->translations[$domain])) { |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$translation = Translation::create($context, $original); |
|
68
|
|
|
|
|
69
|
|
|
$this->translations[$domain]->add($translation); |
|
70
|
|
|
|
|
71
|
|
|
if (isset($plural)) { |
|
72
|
|
|
$translation->setPlural($plural); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $translation; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Reads and returns the content of a file. |
|
80
|
|
|
*/ |
|
81
|
|
|
protected static function readFile(string $file): string |
|
82
|
|
|
{ |
|
83
|
|
|
$length = filesize($file); |
|
84
|
|
|
|
|
85
|
|
|
if (!($fd = fopen($file, 'rb'))) { |
|
86
|
|
|
throw new Exception("Cannot read the file '$file', probably permissions"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$content = $length ? fread($fd, $length) : ''; |
|
90
|
|
|
fclose($fd); |
|
91
|
|
|
|
|
92
|
|
|
return $content; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|