1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\EmailTemplates\Helpers; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\i18n\i18n; |
7
|
|
|
use TractorCow\Fluent\Model\Locale; |
|
|
|
|
8
|
|
|
use TractorCow\Fluent\State\FluentState; |
|
|
|
|
9
|
|
|
use TractorCow\Fluent\Extension\FluentExtension; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Helps providing base functionnalities where including |
13
|
|
|
* subsite module is optional and yet provide a consistent api |
14
|
|
|
* |
15
|
|
|
* TODO: externalize this to a specific module |
16
|
|
|
*/ |
17
|
|
|
class FluentHelper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
protected static $previousState; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected static $previousSubsite; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $locale_cache = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Do we have the subsite module installed |
36
|
|
|
* TODO: check if it might be better to use module manifest instead? |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public static function usesFluent() |
41
|
|
|
{ |
42
|
|
|
return class_exists(FluentState::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $class |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public static function isClassTranslated($class) |
50
|
|
|
{ |
51
|
|
|
$singl = singleton($class); |
52
|
|
|
return $singl->hasExtension(FluentExtension::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Execute the callback in given subsite |
57
|
|
|
* |
58
|
|
|
* @param string|Locale $locale |
59
|
|
|
* @param callable $cb |
60
|
|
|
* @return mixed callback result |
61
|
|
|
*/ |
62
|
|
|
public static function withLocale($locale, $cb) |
63
|
|
|
{ |
64
|
|
|
if (!self::usesFluent() || !$locale) { |
65
|
|
|
$cb(); |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$state = FluentState::singleton(); |
69
|
|
|
return $state->withState(function ($state) use ($locale, $cb) { |
70
|
|
|
if (is_object($locale)) { |
71
|
|
|
$locale = $locale->Locale; |
72
|
|
|
} |
73
|
|
|
$state->setLocale($locale); |
74
|
|
|
return $cb(); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Execute the callback for all locales |
80
|
|
|
* |
81
|
|
|
* @param callable $cb |
82
|
|
|
* @return array an array of callback results |
83
|
|
|
*/ |
84
|
|
|
public static function withLocales($cb) |
85
|
|
|
{ |
86
|
|
|
if (!self::usesFluent()) { |
87
|
|
|
$cb(); |
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
$allLocales = Locale::get(); |
91
|
|
|
$results = []; |
92
|
|
|
foreach ($allLocales as $locale) { |
93
|
|
|
$results[] = self::withLocale($locale, $cb); |
94
|
|
|
} |
95
|
|
|
return $results; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get a locale from the lang |
100
|
|
|
* |
101
|
|
|
* @param string $lang |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public static function get_locale_from_lang($lang) |
105
|
|
|
{ |
106
|
|
|
// Normalize if needed |
107
|
|
|
if (strlen($lang) > 2) { |
108
|
|
|
$lang = self::get_lang($lang); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Use fluent data |
112
|
|
|
if (class_exists(Locale::class)) { |
113
|
|
|
if (empty(self::$locale_cache)) { |
114
|
|
|
$fluentLocales = Locale::getLocales(); |
115
|
|
|
foreach ($fluentLocales as $locale) { |
116
|
|
|
self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
if (isset(self::$locale_cache[$lang])) { |
120
|
|
|
return self::$locale_cache[$lang]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
// Guess |
124
|
|
|
$localesData = i18n::getData(); |
125
|
|
|
return $localesData->localeFromLang($lang); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the right locale (using fluent data if exists) |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public static function get_locale() |
134
|
|
|
{ |
135
|
|
|
if (class_exists(FluentState::class)) { |
136
|
|
|
return FluentState::singleton()->getLocale(); |
137
|
|
|
} |
138
|
|
|
return i18n::get_locale(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Make sure we get a proper two characters lang |
143
|
|
|
* |
144
|
|
|
* @param string|object $lang a string or a fluent locale object |
145
|
|
|
* @return string a two chars lang |
146
|
|
|
*/ |
147
|
|
|
public static function get_lang($lang = null) |
148
|
|
|
{ |
149
|
|
|
if (!$lang) { |
150
|
|
|
$lang = self::get_locale(); |
151
|
|
|
} |
152
|
|
|
if (is_object($lang)) { |
153
|
|
|
$lang = $lang->Locale; |
154
|
|
|
} |
155
|
|
|
return substr($lang, 0, 2); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths