| Total Complexity | 111 |
| Total Lines | 629 |
| 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 |
||
| 49 | class Factory implements IFactory { |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | protected $requestLanguage = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * cached instances |
||
| 56 | * @var array Structure: Lang => App => \OCP\IL10N |
||
| 57 | */ |
||
| 58 | protected $instances = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array Structure: App => string[] |
||
| 62 | */ |
||
| 63 | protected $availableLanguages = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $availableLocales = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array Structure: string => callable |
||
| 72 | */ |
||
| 73 | protected $pluralFunctions = []; |
||
| 74 | |||
| 75 | public const COMMON_LANGUAGE_CODES = [ |
||
| 76 | 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
||
| 77 | 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** @var IConfig */ |
||
| 81 | protected $config; |
||
| 82 | |||
| 83 | /** @var IRequest */ |
||
| 84 | protected $request; |
||
| 85 | |||
| 86 | /** @var IUserSession */ |
||
| 87 | protected $userSession; |
||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | protected $serverRoot; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param IConfig $config |
||
| 94 | * @param IRequest $request |
||
| 95 | * @param IUserSession $userSession |
||
| 96 | * @param string $serverRoot |
||
| 97 | */ |
||
| 98 | public function __construct(IConfig $config, |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get a language instance |
||
| 110 | * |
||
| 111 | * @param string $app |
||
| 112 | * @param string|null $lang |
||
| 113 | * @param string|null $locale |
||
| 114 | * @return \OCP\IL10N |
||
| 115 | */ |
||
| 116 | public function get($app, $lang = null, $locale = null) { |
||
| 117 | return new LazyL10N(function () use ($app, $lang, $locale) { |
||
| 118 | $app = \OC_App::cleanAppId($app); |
||
| 119 | if ($lang !== null) { |
||
| 120 | $lang = str_replace(['\0', '/', '\\', '..'], '', (string)$lang); |
||
| 121 | } |
||
| 122 | |||
| 123 | $forceLang = $this->config->getSystemValue('force_language', false); |
||
| 124 | if (is_string($forceLang)) { |
||
| 125 | $lang = $forceLang; |
||
| 126 | } |
||
| 127 | |||
| 128 | $forceLocale = $this->config->getSystemValue('force_locale', false); |
||
| 129 | if (is_string($forceLocale)) { |
||
| 130 | $locale = $forceLocale; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($lang === null || !$this->languageExists($app, $lang)) { |
||
| 134 | $lang = $this->findLanguage($app); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($locale === null || !$this->localeExists($locale)) { |
||
| 138 | $locale = $this->findLocale($lang); |
||
| 139 | } |
||
| 140 | |||
| 141 | if (!isset($this->instances[$lang][$app])) { |
||
| 142 | $this->instances[$lang][$app] = new L10N( |
||
| 143 | $this, $app, $lang, $locale, |
||
| 144 | $this->getL10nFilesForApp($app, $lang) |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $this->instances[$lang][$app]; |
||
| 149 | }); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Find the best language |
||
| 154 | * |
||
| 155 | * @param string|null $app App id or null for core |
||
| 156 | * @return string language If nothing works it returns 'en' |
||
| 157 | */ |
||
| 158 | public function findLanguage($app = null) { |
||
| 159 | $forceLang = $this->config->getSystemValue('force_language', false); |
||
| 160 | if (is_string($forceLang)) { |
||
| 161 | $this->requestLanguage = $forceLang; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
||
| 165 | return $this->requestLanguage; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * At this point Nextcloud might not yet be installed and thus the lookup |
||
| 170 | * in the preferences table might fail. For this reason we need to check |
||
| 171 | * whether the instance has already been installed |
||
| 172 | * |
||
| 173 | * @link https://github.com/owncloud/core/issues/21955 |
||
| 174 | */ |
||
| 175 | if ($this->config->getSystemValue('installed', false)) { |
||
| 176 | $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
||
| 177 | if (!is_null($userId)) { |
||
| 178 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
||
| 179 | } else { |
||
| 180 | $userLang = null; |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | $userId = null; |
||
| 184 | $userLang = null; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($userLang) { |
||
| 188 | $this->requestLanguage = $userLang; |
||
| 189 | if ($this->languageExists($app, $userLang)) { |
||
| 190 | return $userLang; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | try { |
||
| 195 | // Try to get the language from the Request |
||
| 196 | $lang = $this->getLanguageFromRequest($app); |
||
| 197 | if ($userId !== null && $app === null && !$userLang) { |
||
| 198 | $this->config->setUserValue($userId, 'core', 'lang', $lang); |
||
| 199 | } |
||
| 200 | return $lang; |
||
| 201 | } catch (LanguageNotFoundException $e) { |
||
| 202 | // Finding language from request failed fall back to default language |
||
| 203 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 204 | if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
||
| 205 | return $defaultLanguage; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // We could not find any language so fall back to english |
||
| 210 | return 'en'; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * find the best locale |
||
| 215 | * |
||
| 216 | * @param string $lang |
||
| 217 | * @return null|string |
||
| 218 | */ |
||
| 219 | public function findLocale($lang = null) { |
||
| 220 | $forceLocale = $this->config->getSystemValue('force_locale', false); |
||
| 221 | if (is_string($forceLocale) && $this->localeExists($forceLocale)) { |
||
| 222 | return $forceLocale; |
||
| 223 | } |
||
| 224 | |||
| 225 | if ($this->config->getSystemValue('installed', false)) { |
||
| 226 | $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; |
||
| 227 | $userLocale = null; |
||
| 228 | if (null !== $userId) { |
||
| 229 | $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); |
||
| 230 | } |
||
| 231 | } else { |
||
| 232 | $userId = null; |
||
|
|
|||
| 233 | $userLocale = null; |
||
| 234 | } |
||
| 235 | |||
| 236 | if ($userLocale && $this->localeExists($userLocale)) { |
||
| 237 | return $userLocale; |
||
| 238 | } |
||
| 239 | |||
| 240 | // Default : use system default locale |
||
| 241 | $defaultLocale = $this->config->getSystemValue('default_locale', false); |
||
| 242 | if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { |
||
| 243 | return $defaultLocale; |
||
| 244 | } |
||
| 245 | |||
| 246 | // If no user locale set, use lang as locale |
||
| 247 | if (null !== $lang && $this->localeExists($lang)) { |
||
| 248 | return $lang; |
||
| 249 | } |
||
| 250 | |||
| 251 | // At last, return USA |
||
| 252 | return 'en_US'; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * find the matching lang from the locale |
||
| 257 | * |
||
| 258 | * @param string $app |
||
| 259 | * @param string $locale |
||
| 260 | * @return null|string |
||
| 261 | */ |
||
| 262 | public function findLanguageFromLocale(string $app = 'core', string $locale = null) { |
||
| 263 | if ($this->languageExists($app, $locale)) { |
||
| 264 | return $locale; |
||
| 265 | } |
||
| 266 | |||
| 267 | // Try to split e.g: fr_FR => fr |
||
| 268 | $locale = explode('_', $locale)[0]; |
||
| 269 | if ($this->languageExists($app, $locale)) { |
||
| 270 | return $locale; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Find all available languages for an app |
||
| 276 | * |
||
| 277 | * @param string|null $app App id or null for core |
||
| 278 | * @return array an array of available languages |
||
| 279 | */ |
||
| 280 | public function findAvailableLanguages($app = null) { |
||
| 281 | $key = $app; |
||
| 282 | if ($key === null) { |
||
| 283 | $key = 'null'; |
||
| 284 | } |
||
| 285 | |||
| 286 | // also works with null as key |
||
| 287 | if (!empty($this->availableLanguages[$key])) { |
||
| 288 | return $this->availableLanguages[$key]; |
||
| 289 | } |
||
| 290 | |||
| 291 | $available = ['en']; //english is always available |
||
| 292 | $dir = $this->findL10nDir($app); |
||
| 293 | if (is_dir($dir)) { |
||
| 294 | $files = scandir($dir); |
||
| 295 | if ($files !== false) { |
||
| 296 | foreach ($files as $file) { |
||
| 297 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 298 | $available[] = substr($file, 0, -5); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | // merge with translations from theme |
||
| 305 | $theme = $this->config->getSystemValue('theme'); |
||
| 306 | if (!empty($theme)) { |
||
| 307 | $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
||
| 308 | |||
| 309 | if (is_dir($themeDir)) { |
||
| 310 | $files = scandir($themeDir); |
||
| 311 | if ($files !== false) { |
||
| 312 | foreach ($files as $file) { |
||
| 313 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 314 | $available[] = substr($file, 0, -5); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | $this->availableLanguages[$key] = $available; |
||
| 322 | return $available; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return array|mixed |
||
| 327 | */ |
||
| 328 | public function findAvailableLocales() { |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string|null $app App id or null for core |
||
| 341 | * @param string $lang |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function languageExists($app, $lang) { |
||
| 345 | if ($lang === 'en') {//english is always available |
||
| 346 | return true; |
||
| 347 | } |
||
| 348 | |||
| 349 | $languages = $this->findAvailableLanguages($app); |
||
| 350 | return array_search($lang, $languages) !== false; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function getLanguageIterator(IUser $user = null): ILanguageIterator { |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Return the language to use when sending something to a user |
||
| 363 | * |
||
| 364 | * @param IUser|null $user |
||
| 365 | * @return string |
||
| 366 | * @since 20.0.0 |
||
| 367 | */ |
||
| 368 | public function getUserLanguage(IUser $user = null): string { |
||
| 369 | $language = $this->config->getSystemValue('force_language', false); |
||
| 370 | if ($language !== false) { |
||
| 371 | return $language; |
||
| 372 | } |
||
| 373 | |||
| 374 | if ($user instanceof IUser) { |
||
| 375 | $language = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
||
| 376 | if ($language !== null) { |
||
| 377 | return $language; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | return $this->config->getSystemValue('default_language', 'en'); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $locale |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function localeExists($locale) { |
||
| 389 | if ($locale === 'en') { //english is always available |
||
| 390 | return true; |
||
| 391 | } |
||
| 392 | |||
| 393 | $locales = $this->findAvailableLocales(); |
||
| 394 | $userLocale = array_filter($locales, function ($value) use ($locale) { |
||
| 395 | return $locale === $value['code']; |
||
| 396 | }); |
||
| 397 | |||
| 398 | return !empty($userLocale); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string|null $app |
||
| 403 | * @return string |
||
| 404 | * @throws LanguageNotFoundException |
||
| 405 | */ |
||
| 406 | private function getLanguageFromRequest($app) { |
||
| 407 | $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
||
| 408 | if ($header !== '') { |
||
| 409 | $available = $this->findAvailableLanguages($app); |
||
| 410 | |||
| 411 | // E.g. make sure that 'de' is before 'de_DE'. |
||
| 412 | sort($available); |
||
| 413 | |||
| 414 | $preferences = preg_split('/,\s*/', strtolower($header)); |
||
| 415 | foreach ($preferences as $preference) { |
||
| 416 | list($preferred_language) = explode(';', $preference); |
||
| 417 | $preferred_language = str_replace('-', '_', $preferred_language); |
||
| 418 | |||
| 419 | foreach ($available as $available_language) { |
||
| 420 | if ($preferred_language === strtolower($available_language)) { |
||
| 421 | return $this->respectDefaultLanguage($app, $available_language); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | // Fallback from de_De to de |
||
| 426 | foreach ($available as $available_language) { |
||
| 427 | if (substr($preferred_language, 0, 2) === $available_language) { |
||
| 428 | return $available_language; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | throw new LanguageNotFoundException(); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * if default language is set to de_DE (formal German) this should be |
||
| 439 | * preferred to 'de' (non-formal German) if possible |
||
| 440 | * |
||
| 441 | * @param string|null $app |
||
| 442 | * @param string $lang |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | protected function respectDefaultLanguage($app, $lang) { |
||
| 446 | $result = $lang; |
||
| 447 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 448 | |||
| 449 | // use formal version of german ("Sie" instead of "Du") if the default |
||
| 450 | // language is set to 'de_DE' if possible |
||
| 451 | if (is_string($defaultLanguage) && |
||
| 452 | strtolower($lang) === 'de' && |
||
| 453 | strtolower($defaultLanguage) === 'de_de' && |
||
| 454 | $this->languageExists($app, 'de_DE') |
||
| 455 | ) { |
||
| 456 | $result = 'de_DE'; |
||
| 457 | } |
||
| 458 | |||
| 459 | return $result; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Checks if $sub is a subdirectory of $parent |
||
| 464 | * |
||
| 465 | * @param string $sub |
||
| 466 | * @param string $parent |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | private function isSubDirectory($sub, $parent) { |
||
| 470 | // Check whether $sub contains no ".." |
||
| 471 | if (strpos($sub, '..') !== false) { |
||
| 472 | return false; |
||
| 473 | } |
||
| 474 | |||
| 475 | // Check whether $sub is a subdirectory of $parent |
||
| 476 | if (strpos($sub, $parent) === 0) { |
||
| 477 | return true; |
||
| 478 | } |
||
| 479 | |||
| 480 | return false; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get a list of language files that should be loaded |
||
| 485 | * |
||
| 486 | * @param string $app |
||
| 487 | * @param string $lang |
||
| 488 | * @return string[] |
||
| 489 | */ |
||
| 490 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
||
| 491 | // FIXME This is also the reason, why it is not in the public interface |
||
| 492 | public function getL10nFilesForApp($app, $lang) { |
||
| 493 | $languageFiles = []; |
||
| 494 | |||
| 495 | $i18nDir = $this->findL10nDir($app); |
||
| 496 | $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
||
| 497 | |||
| 498 | if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
||
| 499 | || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
||
| 500 | || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
||
| 501 | ) |
||
| 502 | && file_exists($transFile)) { |
||
| 503 | // load the translations file |
||
| 504 | $languageFiles[] = $transFile; |
||
| 505 | } |
||
| 506 | |||
| 507 | // merge with translations from theme |
||
| 508 | $theme = $this->config->getSystemValue('theme'); |
||
| 509 | if (!empty($theme)) { |
||
| 510 | $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
||
| 511 | if (file_exists($transFile)) { |
||
| 512 | $languageFiles[] = $transFile; |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | return $languageFiles; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * find the l10n directory |
||
| 521 | * |
||
| 522 | * @param string $app App id or empty string for core |
||
| 523 | * @return string directory |
||
| 524 | */ |
||
| 525 | protected function findL10nDir($app = null) { |
||
| 535 | } |
||
| 536 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * Creates a function from the plural string |
||
| 540 | * |
||
| 541 | * Parts of the code is copied from Habari: |
||
| 542 | * https://github.com/habari/system/blob/master/classes/locale.php |
||
| 543 | * @param string $string |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function createPluralFunction($string) { |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * returns the common language and other languages in an |
||
| 604 | * associative array |
||
| 605 | * |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | public function getLanguages() { |
||
| 681 |