1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing LanguageResolver class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository\SiteAccessAware\Language; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\Language; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Resolves language settings for use in SiteAccess aware Repository. |
15
|
|
|
*/ |
16
|
|
|
class LanguageResolver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Values typically provided by configuration. |
20
|
|
|
* |
21
|
|
|
* These will need to change when configuration (scope) changes mid flight using setters below. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $configLanguages; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
private $defaultUseAlwaysAvailable; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
private $defaultShowAllTranslations; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Values typically provided by user context, will need to be set depending on your own custom logic using setter. |
35
|
|
|
* |
36
|
|
|
* E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both |
37
|
|
|
* UI strings as well as default languages to prioritize for repository objects. |
38
|
|
|
* |
39
|
|
|
* If set, this will have priority over configured languages. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
private $contextLanguage; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
array $configLanguages, |
47
|
|
|
bool $defaultUseAlwaysAvailable = true, |
48
|
|
|
bool $defaultShowAllTranslations = false |
49
|
|
|
) { |
50
|
|
|
$this->configLanguages = $configLanguages; |
51
|
|
|
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable; |
52
|
|
|
$this->defaultShowAllTranslations = $defaultShowAllTranslations; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* For use by event listening to config resolver scope changes (or other event changing configured languages). |
57
|
|
|
* |
58
|
|
|
* @param array $configLanguages |
59
|
|
|
*/ |
60
|
|
|
public function setConfigLanguages(array $configLanguages): void |
61
|
|
|
{ |
62
|
|
|
$this->configLanguages = $configLanguages; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* For use by custom events / logic setting language for all retrieved objects from repository. |
67
|
|
|
* |
68
|
|
|
* User language will, if set, will have prepended before configured languages. But in cases PHP API consumer |
69
|
|
|
* specifies languages to retrieve repository objects in it will instead be appended as a fallback. |
70
|
|
|
* |
71
|
|
|
* If set, this will have priority over configured languages. |
72
|
|
|
* |
73
|
|
|
* @param string|null $contextLanguage |
74
|
|
|
*/ |
75
|
|
|
public function setContextLanguage(?string $contextLanguage): void |
76
|
|
|
{ |
77
|
|
|
$this->contextLanguage = $contextLanguage; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get prioritized languages taking into account forced-, context- and lastly configured-languages. |
82
|
|
|
* |
83
|
|
|
* @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getPrioritizedLanguages(?array $forcedLanguages): array |
88
|
|
|
{ |
89
|
|
|
// Skip if languages param has been set by API user |
90
|
|
|
if ($forcedLanguages !== null) { |
91
|
|
|
return $forcedLanguages; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Detect if we should load all languages by default |
95
|
|
|
if ($this->defaultShowAllTranslations) { |
96
|
|
|
return Language::ALL; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// create language based on context and configuration, where context language is made most important one |
100
|
|
|
$languages = []; |
101
|
|
|
if ($this->contextLanguage !== null) { |
102
|
|
|
$languages[] = $this->contextLanguage; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return array_values(array_unique(array_merge($languages, $this->configLanguages))); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* For use by event listening to config resolver scope changes (or other event changing configured languages). |
110
|
|
|
* |
111
|
|
|
* @param bool $defaultUseAlwaysAvailable |
112
|
|
|
*/ |
113
|
|
|
public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void |
114
|
|
|
{ |
115
|
|
|
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get currently set UseAlwaysAvailable. |
120
|
|
|
* |
121
|
|
|
* @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value, |
122
|
|
|
* typically arguments provided to API. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool |
127
|
|
|
{ |
128
|
|
|
if ($forcedUseAlwaysAvailable !== null) { |
129
|
|
|
return $forcedUseAlwaysAvailable; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->defaultUseAlwaysAvailable; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* For use by event listening to config resolver scope changes (or other event changing configured languages). |
137
|
|
|
* |
138
|
|
|
* @param bool $defaultShowAllTranslations |
139
|
|
|
*/ |
140
|
|
|
public function setShowAllTranslations(bool $defaultShowAllTranslations): void |
141
|
|
|
{ |
142
|
|
|
$this->defaultShowAllTranslations = $defaultShowAllTranslations; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get currently set showAllTranslations. |
147
|
|
|
* |
148
|
|
|
* @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value, |
149
|
|
|
* typically arguments provided to API. |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool |
154
|
|
|
{ |
155
|
|
|
if ($forcedShowAllTranslations !== null) { |
156
|
|
|
return $forcedShowAllTranslations; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->defaultShowAllTranslations; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|