1 | <?php |
||
2 | |||
3 | namespace LeKoala\EmailTemplates\Helpers; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use SilverStripe\i18n\i18n; |
||
7 | use TractorCow\Fluent\Model\Locale; |
||
0 ignored issues
–
show
|
|||
8 | use TractorCow\Fluent\State\FluentState; |
||
0 ignored issues
–
show
The type
TractorCow\Fluent\State\FluentState was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
9 | use TractorCow\Fluent\Extension\FluentExtension; |
||
0 ignored issues
–
show
The type
TractorCow\Fluent\Extension\FluentExtension was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
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 void |
||
61 | */ |
||
62 | public static function withLocale($locale, $cb) |
||
63 | { |
||
64 | if (!self::usesFluent() || !$locale) { |
||
65 | $cb(); |
||
66 | return; |
||
67 | } |
||
68 | $state = FluentState::singleton(); |
||
69 | $state->withState(function ($state) use ($locale, $cb) { |
||
70 | if (is_object($locale)) { |
||
71 | $locale = $locale->Locale; |
||
72 | } |
||
73 | $state->setLocale($locale); |
||
74 | $cb(); |
||
75 | }); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Execute the callback for all locales |
||
80 | * |
||
81 | * @param callable $cb |
||
82 | * @return void |
||
83 | */ |
||
84 | public static function withLocales($cb) |
||
85 | { |
||
86 | if (!self::usesFluent()) { |
||
87 | $cb(); |
||
88 | return; |
||
89 | } |
||
90 | $allLocales = Locale::get(); |
||
91 | foreach ($allLocales as $locale) { |
||
92 | self::withLocale($locale, $cb); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get a locale from the lang |
||
98 | * |
||
99 | * @param string $lang |
||
100 | * @return string |
||
101 | */ |
||
102 | public static function get_locale_from_lang($lang) |
||
103 | { |
||
104 | // Normalize if needed |
||
105 | if (strlen($lang) > 2) { |
||
106 | $lang = self::get_lang($lang); |
||
107 | } |
||
108 | |||
109 | // Use fluent data |
||
110 | if (class_exists(Locale::class)) { |
||
111 | if (empty(self::$locale_cache)) { |
||
112 | $fluentLocales = Locale::getLocales(); |
||
113 | foreach ($fluentLocales as $locale) { |
||
114 | self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale; |
||
115 | } |
||
116 | } |
||
117 | if (isset(self::$locale_cache[$lang])) { |
||
118 | return self::$locale_cache[$lang]; |
||
119 | } |
||
120 | } |
||
121 | // Guess |
||
122 | $localesData = i18n::getData(); |
||
123 | return $localesData->localeFromLang($lang); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get the right locale (using fluent data if exists) |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public static function get_locale() |
||
132 | { |
||
133 | if (class_exists(FluentState::class)) { |
||
134 | return FluentState::singleton()->getLocale(); |
||
135 | } |
||
136 | return i18n::get_locale(); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Make sure we get a proper two characters lang |
||
141 | * |
||
142 | * @param string|object $lang a string or a fluent locale object |
||
143 | * @return string a two chars lang |
||
144 | */ |
||
145 | public static function get_lang($lang = null) |
||
146 | { |
||
147 | if (!$lang) { |
||
148 | $lang = self::get_locale(); |
||
149 | } |
||
150 | if (is_object($lang)) { |
||
151 | $lang = $lang->Locale; |
||
152 | } |
||
153 | return substr($lang, 0, 2); |
||
154 | } |
||
155 | } |
||
156 |
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