|
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\Bundle\Catalogue; |
|
13
|
|
|
|
|
14
|
|
|
use Nyholm\NSA; |
|
15
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
|
16
|
|
|
use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
|
17
|
|
|
use Translation\Bundle\Model\Configuration; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Fetches catalogues from source files. This will only work with local file storage |
|
21
|
|
|
* and the actions are read only. |
|
22
|
|
|
* |
|
23
|
|
|
* This should be considered as a "ReadFromCache" service. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class CatalogueFetcher |
|
28
|
|
|
{ |
|
29
|
|
|
private $reader; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(TranslationReaderInterface $reader) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->reader = $reader; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* load any existing messages from the translation files. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getCatalogues(Configuration $config, array $locales = []): array |
|
40
|
|
|
{ |
|
41
|
|
|
$dirs = $config->getPathsToTranslationFiles(); |
|
42
|
|
|
if (empty($locales)) { |
|
43
|
|
|
$locales = $config->getLocales(); |
|
44
|
|
|
} |
|
45
|
|
|
$catalogues = []; |
|
46
|
|
|
foreach ($locales as $locale) { |
|
47
|
|
|
$currentCatalogue = new MessageCatalogue($locale); |
|
48
|
|
|
foreach ($dirs as $path) { |
|
49
|
|
|
if (\is_dir($path)) { |
|
50
|
|
|
$this->reader->read($path, $currentCatalogue); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
foreach ($currentCatalogue->getDomains() as $domain) { |
|
55
|
|
|
if (!$this->isValidDomain($config, $domain)) { |
|
56
|
|
|
$messages = $currentCatalogue->all(); |
|
57
|
|
|
unset($messages[$domain]); |
|
58
|
|
|
NSA::setProperty($currentCatalogue, 'messages', $messages); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$catalogues[] = $currentCatalogue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $catalogues; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function isValidDomain(Configuration $config, string $domain): bool |
|
69
|
|
|
{ |
|
70
|
|
|
$blacklist = $config->getBlacklistDomains(); |
|
71
|
|
|
$whitelist = $config->getWhitelistDomains(); |
|
72
|
|
|
|
|
73
|
|
|
if (!empty($blacklist) && \in_array($domain, $blacklist, true)) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (!empty($whitelist) && !\in_array($domain, $whitelist, true)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|