Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / LocalizationFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Provides a language parser factory.
23 */
24 class LocalizationFactory implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
28 */
29 protected $cacheInstance;
30
31 /**
32 * @var \TYPO3\CMS\Core\Localization\LanguageStore
33 */
34 public $store;
35
36 /**
37 * Class constructor
38 */
39 public function __construct()
40 {
41 $this->store = GeneralUtility::makeInstance(LanguageStore::class);
42 $this->initializeCache();
43 }
44
45 /**
46 * Initialize cache instance to be ready to use
47 */
48 protected function initializeCache()
49 {
50 $this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n');
51 }
52
53 /**
54 * Returns parsed data from a given file and language key.
55 *
56 * @param string $fileReference Input is a file-reference (see \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName). That file is expected to be a supported locallang file format
57 * @param string $languageKey Language key
58 * @param null $_ unused
59 * @param null $__ unused
60 * @param bool $isLocalizationOverride TRUE if $fileReference is a localization override
61 * @return array|bool
62 */
63 public function getParsedData($fileReference, $languageKey, $_ = null, $__ = null, $isLocalizationOverride = false)
64 {
65 $hash = md5($fileReference . $languageKey);
66
67 // Check if the default language is processed before processing other language
68 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
69 $this->getParsedData($fileReference, 'default');
70 }
71 // If the content is parsed (local cache), use it
72 if ($this->store->hasData($fileReference, $languageKey)) {
73 return $this->store->getData($fileReference);
74 }
75
76 // If the content is in cache (system cache), use it
77 $data = $this->cacheInstance->get($hash);
78 if ($data !== false) {
79 $this->store->setData($fileReference, $languageKey, $data);
80 return $this->store->getData($fileReference);
81 }
82
83 try {
84 $this->store->setConfiguration($fileReference, $languageKey);
85 /** @var \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface $parser */
86 $parser = $this->store->getParserInstance($fileReference);
87 // Get parsed data
88 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey);
89 } catch (Exception\FileNotFoundException $exception) {
90 // Source localization file not found, set empty data as there could be an override
91 $this->store->setData($fileReference, $languageKey, []);
92 $LOCAL_LANG = $this->store->getData($fileReference);
93 }
94
95 // Override localization
96 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
97 $this->localizationOverride($fileReference, $languageKey, $LOCAL_LANG);
98 }
99
100 // Save parsed data in cache
101 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
102
103 // Cache processed data
104 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
105
106 return $this->store->getData($fileReference);
107 }
108
109 /**
110 * Override localization file
111 *
112 * This method merges the content of the override file with the default file
113 *
114 * @param string $fileReference
115 * @param string $languageKey
116 * @param array $LOCAL_LANG
117 */
118 protected function localizationOverride($fileReference, $languageKey, array &$LOCAL_LANG)
119 {
120 $overrides = [];
121 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
122 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
123 foreach ($this->store->getSupportedExtensions() as $extension) {
124 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
125 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
126 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
127 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
128 }
129 }
130 if (!empty($overrides)) {
131 foreach ($overrides as $overrideFile) {
132 $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
133 ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, null, null, true));
134 }
135 }
136 }
137 }