| Total Complexity | 117 |
| Total Lines | 617 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Factory implements IFactory { |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | protected $requestLanguage = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * cached instances |
||
| 61 | * @var array Structure: Lang => App => \OCP\IL10N |
||
| 62 | */ |
||
| 63 | protected $instances = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array Structure: App => string[] |
||
| 67 | */ |
||
| 68 | protected $availableLanguages = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $localeCache = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $availableLocales = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array Structure: string => callable |
||
| 82 | */ |
||
| 83 | protected $pluralFunctions = []; |
||
| 84 | |||
| 85 | public const COMMON_LANGUAGE_CODES = [ |
||
| 86 | 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
||
| 87 | 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** @var IConfig */ |
||
| 91 | protected $config; |
||
| 92 | |||
| 93 | /** @var IRequest */ |
||
| 94 | protected $request; |
||
| 95 | |||
| 96 | /** @var IUserSession */ |
||
| 97 | protected $userSession; |
||
| 98 | |||
| 99 | /** @var string */ |
||
| 100 | protected $serverRoot; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param IConfig $config |
||
| 104 | * @param IRequest $request |
||
| 105 | * @param IUserSession $userSession |
||
| 106 | * @param string $serverRoot |
||
| 107 | */ |
||
| 108 | public function __construct( |
||
| 109 | IConfig $config, |
||
| 110 | IRequest $request, |
||
| 111 | IUserSession $userSession, |
||
| 112 | $serverRoot |
||
| 113 | ) { |
||
| 114 | $this->config = $config; |
||
| 115 | $this->request = $request; |
||
| 116 | $this->userSession = $userSession; |
||
| 117 | $this->serverRoot = $serverRoot; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get a language instance |
||
| 122 | * |
||
| 123 | * @param string $app |
||
| 124 | * @param string|null $lang |
||
| 125 | * @param string|null $locale |
||
| 126 | * @return \OCP\IL10N |
||
| 127 | */ |
||
| 128 | public function get($app, $lang = null, $locale = null) { |
||
| 129 | return new LazyL10N(function () use ($app, $lang, $locale) { |
||
| 130 | $app = \OC_App::cleanAppId($app); |
||
| 131 | if ($lang !== null) { |
||
| 132 | $lang = str_replace(['\0', '/', '\\', '..'], '', $lang); |
||
| 133 | } |
||
| 134 | |||
| 135 | $forceLang = $this->config->getSystemValue('force_language', false); |
||
| 136 | if (is_string($forceLang)) { |
||
| 137 | $lang = $forceLang; |
||
| 138 | } |
||
| 139 | |||
| 140 | $forceLocale = $this->config->getSystemValue('force_locale', false); |
||
| 141 | if (is_string($forceLocale)) { |
||
| 142 | $locale = $forceLocale; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($lang === null || !$this->languageExists($app, $lang)) { |
||
| 146 | $lang = $this->findLanguage($app); |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($locale === null || !$this->localeExists($locale)) { |
||
| 150 | $locale = $this->findLocale($lang); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!isset($this->instances[$lang][$app])) { |
||
| 154 | $this->instances[$lang][$app] = new L10N( |
||
| 155 | $this, |
||
| 156 | $app, |
||
| 157 | $lang, |
||
| 158 | $locale, |
||
| 159 | $this->getL10nFilesForApp($app, $lang) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $this->instances[$lang][$app]; |
||
| 164 | }); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Find the best language |
||
| 169 | * |
||
| 170 | * @param string|null $appId App id or null for core |
||
| 171 | * |
||
| 172 | * @return string language If nothing works it returns 'en' |
||
| 173 | */ |
||
| 174 | public function findLanguage(?string $appId = null): string { |
||
| 175 | // Step 1: Forced language always has precedence over anything else |
||
| 176 | $forceLang = $this->config->getSystemValue('force_language', false); |
||
| 177 | if (is_string($forceLang)) { |
||
| 178 | $this->requestLanguage = $forceLang; |
||
| 179 | } |
||
| 180 | |||
| 181 | // Step 2: Return cached language |
||
| 182 | if ($this->requestLanguage !== '' && $this->languageExists($appId, $this->requestLanguage)) { |
||
| 183 | return $this->requestLanguage; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Step 3: At this point Nextcloud might not yet be installed and thus the lookup |
||
| 188 | * in the preferences table might fail. For this reason we need to check |
||
| 189 | * whether the instance has already been installed |
||
| 190 | * |
||
| 191 | * @link https://github.com/owncloud/core/issues/21955 |
||
| 192 | */ |
||
| 193 | if ($this->config->getSystemValue('installed', false)) { |
||
| 194 | $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
||
| 195 | if (!is_null($userId)) { |
||
| 196 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
||
| 197 | } else { |
||
| 198 | $userLang = null; |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $userId = null; |
||
| 202 | $userLang = null; |
||
| 203 | } |
||
| 204 | if ($userLang) { |
||
| 205 | $this->requestLanguage = $userLang; |
||
| 206 | if ($this->languageExists($appId, $userLang)) { |
||
| 207 | return $userLang; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // Step 4: Check the request headers |
||
| 212 | try { |
||
| 213 | // Try to get the language from the Request |
||
| 214 | $lang = $this->getLanguageFromRequest($appId); |
||
| 215 | if ($userId !== null && $appId === null && !$userLang) { |
||
| 216 | $this->config->setUserValue($userId, 'core', 'lang', $lang); |
||
| 217 | } |
||
| 218 | return $lang; |
||
| 219 | } catch (LanguageNotFoundException $e) { |
||
| 220 | // Finding language from request failed fall back to default language |
||
| 221 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 222 | if ($defaultLanguage !== false && $this->languageExists($appId, $defaultLanguage)) { |
||
| 223 | return $defaultLanguage; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // Step 5: fall back to English |
||
| 228 | return 'en'; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function findGenericLanguage(string $appId = null): string { |
||
| 232 | // Step 1: Forced language always has precedence over anything else |
||
| 233 | $forcedLanguage = $this->config->getSystemValue('force_language', false); |
||
| 234 | if ($forcedLanguage !== false) { |
||
| 235 | return $forcedLanguage; |
||
| 236 | } |
||
| 237 | |||
| 238 | // Step 2: Check if we have a default language |
||
| 239 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 240 | if ($defaultLanguage !== false && $this->languageExists($appId, $defaultLanguage)) { |
||
| 241 | return $defaultLanguage; |
||
| 242 | } |
||
| 243 | |||
| 244 | // Step 3.1: Check if Nextcloud is already installed before we try to access user info |
||
| 245 | if (!$this->config->getSystemValue('installed', false)) { |
||
| 246 | return 'en'; |
||
| 247 | } |
||
| 248 | // Step 3.2: Check the current user (if any) for their preferred language |
||
| 249 | $user = $this->userSession->getUser(); |
||
| 250 | if ($user !== null) { |
||
| 251 | $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
||
| 252 | if ($userLang !== null) { |
||
|
|
|||
| 253 | return $userLang; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | // Step 4: Check the request headers |
||
| 258 | try { |
||
| 259 | return $this->getLanguageFromRequest($appId); |
||
| 260 | } catch (LanguageNotFoundException $e) { |
||
| 261 | // Ignore and continue |
||
| 262 | } |
||
| 263 | |||
| 264 | // Step 5: fall back to English |
||
| 265 | return 'en'; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * find the best locale |
||
| 270 | * |
||
| 271 | * @param string $lang |
||
| 272 | * @return null|string |
||
| 273 | */ |
||
| 274 | public function findLocale($lang = null) { |
||
| 275 | $forceLocale = $this->config->getSystemValue('force_locale', false); |
||
| 276 | if (is_string($forceLocale) && $this->localeExists($forceLocale)) { |
||
| 277 | return $forceLocale; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($this->config->getSystemValue('installed', false)) { |
||
| 281 | $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; |
||
| 282 | $userLocale = null; |
||
| 283 | if (null !== $userId) { |
||
| 284 | $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); |
||
| 285 | } |
||
| 286 | } else { |
||
| 287 | $userId = null; |
||
| 288 | $userLocale = null; |
||
| 289 | } |
||
| 290 | |||
| 291 | if ($userLocale && $this->localeExists($userLocale)) { |
||
| 292 | return $userLocale; |
||
| 293 | } |
||
| 294 | |||
| 295 | // Default : use system default locale |
||
| 296 | $defaultLocale = $this->config->getSystemValue('default_locale', false); |
||
| 297 | if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { |
||
| 298 | return $defaultLocale; |
||
| 299 | } |
||
| 300 | |||
| 301 | // If no user locale set, use lang as locale |
||
| 302 | if (null !== $lang && $this->localeExists($lang)) { |
||
| 303 | return $lang; |
||
| 304 | } |
||
| 305 | |||
| 306 | // At last, return USA |
||
| 307 | return 'en_US'; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * find the matching lang from the locale |
||
| 312 | * |
||
| 313 | * @param string $app |
||
| 314 | * @param string $locale |
||
| 315 | * @return null|string |
||
| 316 | */ |
||
| 317 | public function findLanguageFromLocale(string $app = 'core', string $locale = null) { |
||
| 318 | if ($this->languageExists($app, $locale)) { |
||
| 319 | return $locale; |
||
| 320 | } |
||
| 321 | |||
| 322 | // Try to split e.g: fr_FR => fr |
||
| 323 | $locale = explode('_', $locale)[0]; |
||
| 324 | if ($this->languageExists($app, $locale)) { |
||
| 325 | return $locale; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Find all available languages for an app |
||
| 331 | * |
||
| 332 | * @param string|null $app App id or null for core |
||
| 333 | * @return string[] an array of available languages |
||
| 334 | */ |
||
| 335 | public function findAvailableLanguages($app = null): array { |
||
| 336 | $key = $app; |
||
| 337 | if ($key === null) { |
||
| 338 | $key = 'null'; |
||
| 339 | } |
||
| 340 | |||
| 341 | // also works with null as key |
||
| 342 | if (!empty($this->availableLanguages[$key])) { |
||
| 343 | return $this->availableLanguages[$key]; |
||
| 344 | } |
||
| 345 | |||
| 346 | $available = ['en']; //english is always available |
||
| 347 | $dir = $this->findL10nDir($app); |
||
| 348 | if (is_dir($dir)) { |
||
| 349 | $files = scandir($dir); |
||
| 350 | if ($files !== false) { |
||
| 351 | foreach ($files as $file) { |
||
| 352 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 353 | $available[] = substr($file, 0, -5); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | // merge with translations from theme |
||
| 360 | $theme = $this->config->getSystemValue('theme'); |
||
| 361 | if (!empty($theme)) { |
||
| 362 | $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
||
| 363 | |||
| 364 | if (is_dir($themeDir)) { |
||
| 365 | $files = scandir($themeDir); |
||
| 366 | if ($files !== false) { |
||
| 367 | foreach ($files as $file) { |
||
| 368 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 369 | $available[] = substr($file, 0, -5); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->availableLanguages[$key] = $available; |
||
| 377 | return $available; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return array|mixed |
||
| 382 | */ |
||
| 383 | public function findAvailableLocales() { |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string|null $app App id or null for core |
||
| 396 | * @param string $lang |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function languageExists($app, $lang) { |
||
| 400 | if ($lang === 'en') { //english is always available |
||
| 401 | return true; |
||
| 402 | } |
||
| 403 | |||
| 404 | $languages = $this->findAvailableLanguages($app); |
||
| 405 | return in_array($lang, $languages); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function getLanguageIterator(IUser $user = null): ILanguageIterator { |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Return the language to use when sending something to a user |
||
| 418 | * |
||
| 419 | * @param IUser|null $user |
||
| 420 | * @return string |
||
| 421 | * @since 20.0.0 |
||
| 422 | */ |
||
| 423 | public function getUserLanguage(IUser $user = null): string { |
||
| 424 | $language = $this->config->getSystemValue('force_language', false); |
||
| 425 | if ($language !== false) { |
||
| 426 | return $language; |
||
| 427 | } |
||
| 428 | |||
| 429 | if ($user instanceof IUser) { |
||
| 430 | $language = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
||
| 431 | if ($language !== null) { |
||
| 432 | return $language; |
||
| 433 | } |
||
| 434 | |||
| 435 | // Use language from request |
||
| 436 | if ($this->userSession->getUser() instanceof IUser && |
||
| 437 | $user->getUID() === $this->userSession->getUser()->getUID()) { |
||
| 438 | try { |
||
| 439 | return $this->getLanguageFromRequest(); |
||
| 440 | } catch (LanguageNotFoundException $e) { |
||
| 441 | } |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | return $this->config->getSystemValue('default_language', 'en'); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $locale |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function localeExists($locale) { |
||
| 453 | if ($locale === 'en') { //english is always available |
||
| 454 | return true; |
||
| 455 | } |
||
| 456 | |||
| 457 | if ($this->localeCache === []) { |
||
| 458 | $locales = $this->findAvailableLocales(); |
||
| 459 | foreach ($locales as $l) { |
||
| 460 | $this->localeCache[$l['code']] = true; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | return isset($this->localeCache[$locale]); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @throws LanguageNotFoundException |
||
| 469 | */ |
||
| 470 | private function getLanguageFromRequest(?string $app = null): string { |
||
| 471 | $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
||
| 472 | if ($header !== '') { |
||
| 473 | $available = $this->findAvailableLanguages($app); |
||
| 474 | |||
| 475 | // E.g. make sure that 'de' is before 'de_DE'. |
||
| 476 | sort($available); |
||
| 477 | |||
| 478 | $preferences = preg_split('/,\s*/', strtolower($header)); |
||
| 479 | foreach ($preferences as $preference) { |
||
| 480 | [$preferred_language] = explode(';', $preference); |
||
| 481 | $preferred_language = str_replace('-', '_', $preferred_language); |
||
| 482 | |||
| 483 | foreach ($available as $available_language) { |
||
| 484 | if ($preferred_language === strtolower($available_language)) { |
||
| 485 | return $this->respectDefaultLanguage($app, $available_language); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | // Fallback from de_De to de |
||
| 490 | foreach ($available as $available_language) { |
||
| 491 | if (substr($preferred_language, 0, 2) === $available_language) { |
||
| 492 | return $available_language; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | throw new LanguageNotFoundException(); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * if default language is set to de_DE (formal German) this should be |
||
| 503 | * preferred to 'de' (non-formal German) if possible |
||
| 504 | */ |
||
| 505 | protected function respectDefaultLanguage(?string $app, string $lang): string { |
||
| 506 | $result = $lang; |
||
| 507 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 508 | |||
| 509 | // use formal version of german ("Sie" instead of "Du") if the default |
||
| 510 | // language is set to 'de_DE' if possible |
||
| 511 | if ( |
||
| 512 | is_string($defaultLanguage) && |
||
| 513 | strtolower($lang) === 'de' && |
||
| 514 | strtolower($defaultLanguage) === 'de_de' && |
||
| 515 | $this->languageExists($app, 'de_DE') |
||
| 516 | ) { |
||
| 517 | $result = 'de_DE'; |
||
| 518 | } |
||
| 519 | |||
| 520 | return $result; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Checks if $sub is a subdirectory of $parent |
||
| 525 | * |
||
| 526 | * @param string $sub |
||
| 527 | * @param string $parent |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | private function isSubDirectory($sub, $parent) { |
||
| 531 | // Check whether $sub contains no ".." |
||
| 532 | if (strpos($sub, '..') !== false) { |
||
| 533 | return false; |
||
| 534 | } |
||
| 535 | |||
| 536 | // Check whether $sub is a subdirectory of $parent |
||
| 537 | if (strpos($sub, $parent) === 0) { |
||
| 538 | return true; |
||
| 539 | } |
||
| 540 | |||
| 541 | return false; |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get a list of language files that should be loaded |
||
| 546 | * |
||
| 547 | * @param string $app |
||
| 548 | * @param string $lang |
||
| 549 | * @return string[] |
||
| 550 | */ |
||
| 551 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
||
| 552 | // FIXME This is also the reason, why it is not in the public interface |
||
| 553 | public function getL10nFilesForApp($app, $lang) { |
||
| 554 | $languageFiles = []; |
||
| 555 | |||
| 556 | $i18nDir = $this->findL10nDir($app); |
||
| 557 | $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
||
| 558 | |||
| 559 | if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
||
| 560 | || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
||
| 561 | || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/')) |
||
| 562 | && file_exists($transFile) |
||
| 563 | ) { |
||
| 564 | // load the translations file |
||
| 565 | $languageFiles[] = $transFile; |
||
| 566 | } |
||
| 567 | |||
| 568 | // merge with translations from theme |
||
| 569 | $theme = $this->config->getSystemValue('theme'); |
||
| 570 | if (!empty($theme)) { |
||
| 571 | $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
||
| 572 | if (file_exists($transFile)) { |
||
| 573 | $languageFiles[] = $transFile; |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | return $languageFiles; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * find the l10n directory |
||
| 582 | * |
||
| 583 | * @param string $app App id or empty string for core |
||
| 584 | * @return string directory |
||
| 585 | */ |
||
| 586 | protected function findL10nDir($app = null) { |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @inheritDoc |
||
| 600 | */ |
||
| 601 | public function getLanguages(): array { |
||
| 602 | $forceLanguage = $this->config->getSystemValue('force_language', false); |
||
| 674 |