Completed
Push — master ( e28c49...c020e9 )
by
unknown
19:48
created

LocalizationFactory::getParsedData()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 20
nop 5
dl 0
loc 44
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\Localization;
17
18
use TYPO3\CMS\Core\Cache\CacheManager;
19
use TYPO3\CMS\Core\Localization\Exception\FileNotFoundException;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Core\Utility\ArrayUtility;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Provides a language parser factory.
26
 */
27
class LocalizationFactory implements SingletonInterface
28
{
29
    /**
30
     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
31
     */
32
    protected $cacheInstance;
33
34
    /**
35
     * @var \TYPO3\CMS\Core\Localization\LanguageStore
36
     */
37
    public $store;
38
39
    public function __construct(LanguageStore $languageStore, CacheManager $cacheManager)
40
    {
41
        $this->store = $languageStore;
42
        $this->cacheInstance = $cacheManager->getCache('l10n');
43
    }
44
45
    /**
46
     * Returns parsed data from a given file and language key.
47
     *
48
     * @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
49
     * @param string $languageKey Language key
50
     * @param null $_ unused
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $_ is correct as it would always require null to be passed?
Loading history...
51
     * @param null $__ unused
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $__ is correct as it would always require null to be passed?
Loading history...
52
     * @param bool $isLocalizationOverride TRUE if $fileReference is a localization override
53
     * @return array|bool
54
     */
55
    public function getParsedData($fileReference, $languageKey, $_ = null, $__ = null, $isLocalizationOverride = false)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function getParsedData($fileReference, $languageKey, /** @scrutinizer ignore-unused */ $_ = null, $__ = null, $isLocalizationOverride = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $__ is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function getParsedData($fileReference, $languageKey, $_ = null, /** @scrutinizer ignore-unused */ $__ = null, $isLocalizationOverride = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        $hash = md5($fileReference . $languageKey);
58
59
        // Check if the default language is processed before processing other language
60
        if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
61
            $this->getParsedData($fileReference, 'default');
62
        }
63
        // If the content is parsed (local cache), use it
64
        if ($this->store->hasData($fileReference, $languageKey)) {
65
            return $this->store->getData($fileReference);
66
        }
67
68
        // If the content is in cache (system cache), use it
69
        $data = $this->cacheInstance->get($hash);
70
        if ($data !== false) {
71
            $this->store->setData($fileReference, $languageKey, $data);
72
            return $this->store->getData($fileReference);
73
        }
74
75
        try {
76
            $this->store->setConfiguration($fileReference, $languageKey);
77
            /** @var \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface $parser */
78
            $parser = $this->store->getParserInstance($fileReference);
79
            // Get parsed data
80
            $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey);
81
        } catch (FileNotFoundException $exception) {
82
            // Source localization file not found, set empty data as there could be an override
83
            $this->store->setData($fileReference, $languageKey, []);
84
            $LOCAL_LANG = $this->store->getData($fileReference);
85
        }
86
87
        // Override localization
88
        if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
89
            $this->localizationOverride($fileReference, $languageKey, $LOCAL_LANG);
90
        }
91
92
        // Save parsed data in cache
93
        $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
94
95
        // Cache processed data
96
        $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
97
98
        return $this->store->getData($fileReference);
99
    }
100
101
    /**
102
     * Override localization file
103
     *
104
     * This method merges the content of the override file with the default file
105
     *
106
     * @param string $fileReference
107
     * @param string $languageKey
108
     * @param array $LOCAL_LANG
109
     */
110
    protected function localizationOverride($fileReference, $languageKey, array &$LOCAL_LANG)
111
    {
112
        $overrides = [];
113
        $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
114
        $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
115
        foreach ($this->store->getSupportedExtensions() as $extension) {
116
            if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
117
                $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
118
            } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
119
                $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
120
            }
121
        }
122
        if (!empty($overrides)) {
123
            foreach ($overrides as $overrideFile) {
124
                $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
125
                ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, null, null, true));
126
            }
127
        }
128
    }
129
}
130