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\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader; |
16
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
17
|
|
|
use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
18
|
|
|
use Translation\Bundle\Model\Configuration; |
19
|
|
|
use Translation\SymfonyStorage\LegacyTranslationReader; |
20
|
|
|
use Translation\SymfonyStorage\TranslationLoader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Fetches catalogues from source files. This will only work with local file storage |
24
|
|
|
* and the actions are read only. |
25
|
|
|
* |
26
|
|
|
* This should be considered as a "ReadFromCache" service. |
27
|
|
|
* |
28
|
|
|
* @author Tobias Nyholm <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class CatalogueFetcher |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var TranslationReaderInterface |
34
|
|
|
*/ |
35
|
|
|
private $reader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader |
39
|
|
|
*/ |
40
|
|
|
public function __construct($reader) |
41
|
|
|
{ |
42
|
|
|
if (!$reader instanceof TranslationReaderInterface) { |
43
|
|
|
$reader = new LegacyTranslationReader($reader); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->reader = $reader; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* load any existing messages from the translation files. |
51
|
|
|
* |
52
|
|
|
* @param Configuration $config |
53
|
|
|
* @param array $locales |
54
|
|
|
* |
55
|
|
|
* @return MessageCatalogue[] |
56
|
|
|
*/ |
57
|
|
|
public function getCatalogues(Configuration $config, array $locales = []) |
58
|
|
|
{ |
59
|
|
|
$dirs = $config->getPathsToTranslationFiles(); |
60
|
|
|
if (empty($locales)) { |
61
|
|
|
$locales = $config->getLocales(); |
62
|
|
|
} |
63
|
|
|
$catalogues = []; |
64
|
|
|
foreach ($locales as $locale) { |
65
|
|
|
$currentCatalogue = new MessageCatalogue($locale); |
66
|
|
|
foreach ($dirs as $path) { |
67
|
|
|
if (is_dir($path)) { |
68
|
|
|
$this->reader->read($path, $currentCatalogue); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($currentCatalogue->getDomains() as $domain) { |
73
|
|
|
if (!$this->isValidDomain($config, $domain)) { |
74
|
|
|
$messages = $currentCatalogue->all(); |
75
|
|
|
unset($messages[$domain]); |
76
|
|
|
NSA::setProperty($currentCatalogue, 'messages', $messages); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$catalogues[] = $currentCatalogue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $catalogues; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $domain |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
private function isValidDomain(Configuration $config, $domain) |
92
|
|
|
{ |
93
|
|
|
$blacklist = $config->getBlacklistDomains(); |
94
|
|
|
$whitelist = $config->getWhitelistDomains(); |
95
|
|
|
|
96
|
|
|
if (!empty($blacklist) && in_array($domain, $blacklist)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!empty($whitelist) && !in_array($domain, $whitelist)) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.