LanguageOverride::prepareLoader()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 23
cp 0
rs 8.9848
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
1
<?php
2
3
/**
4
 * Loading LanguageOverride.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Loader;
9
10
use HDNET\Autoloader\Loader;
11
use HDNET\Autoloader\LoaderInterface;
12
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Core\Utility\PathUtility;
15
16
/**
17
 * Loading LanguageOverride.
18
 */
19
class LanguageOverride implements LoaderInterface
20
{
21
    /**
22
     * Get all the complex data for the loader.
23
     * This return value will be cached and stored in the database
24
     * There is no file monitoring for this cache.
25
     */
26
    public function prepareLoader(Loader $loader, int $type): array
27
    {
28
        $languageOverride = [];
29
        if (LoaderInterface::EXT_TABLES === $type) {
30
            return $languageOverride;
31
        }
32
33
        $languageOverridePath = ExtensionManagementUtility::extPath($loader->getExtensionKey()) . 'Resources/Private/Language/Overrides/';
34
        if (!is_dir($languageOverridePath)) {
35
            return $languageOverride;
36
        }
37
38
        $files = GeneralUtility::getAllFilesAndFoldersInPath([], $languageOverridePath, 'xlf,php,xml', false, 99);
39
40
        foreach ($files as $file) {
41
            $file = str_replace($languageOverridePath, '', $file);
42
            $parts = GeneralUtility::trimExplode('/', $file, true);
43
            $extension = GeneralUtility::camelCaseToLowerCaseUnderscored($parts[0]);
44
            unset($parts[0]);
45
            $parts = array_values($parts);
46
47
            // language
48
            $language = 'default';
49
            $fileParts = GeneralUtility::trimExplode('.', PathUtility::basename($file), true);
50
            if (2 === mb_strlen($fileParts[0])) {
51
                $language = $fileParts[0];
52
                unset($fileParts[0]);
53
                $parts[\count($parts) - 1] = implode('.', $fileParts);
54
            }
55
56
            $languageOverride[] = [
57
                'language' => $language,
58
                'original' => 'EXT:' . $extension . '/' . implode('/', $parts),
59
                'override' => 'EXT:' . $loader->getExtensionKey() . '/Resources/Private/Language/Overrides/' . $file,
60
            ];
61
        }
62
63
        return $languageOverride;
64
    }
65
66
    /**
67
     * Run the loading process for the ext_tables.php file.
68
     */
69
    public function loadExtensionTables(Loader $autoLoader, array $loaderInformation): void
70
    {
71
    }
72
73
    /**
74
     * Run the loading process for the ext_localconf.php file.
75
     */
76
    public function loadExtensionConfiguration(Loader $autoLoader, array $loaderInformation): void
77
    {
78
        if (!empty($loaderInformation)) {
79
            foreach ($loaderInformation as $files) {
80
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$files['language']][$files['original']][] = $files['override'];
81
            }
82
        }
83
    }
84
}
85