Completed
Pull Request — master (#636)
by Georg
11:03
created

Lll::getLabel()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 6
nop 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\LanguageFileUnavailableException;
28
use ApacheSolrForTypo3\Solr\Util;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * view helper to replace label markers starting with "LLL:"
33
 * Replaces viewhelpers ###LLL:languageKey###
34
 *
35
 * @author Ingo Renner <[email protected]>
36
 */
37
class Lll implements ViewHelper
38
{
39
40
    /**
41
     * Path to a label file
42
     *
43
     * @var string
44
     */
45
    protected $languageFile;
46
47
    /**
48
     * Language key, f.e. default, en, de, fr, ...
49
     *
50
     * @var string
51
     */
52
    protected $llKey;
53
54
    /**
55
     * Cache for loaded labels
56
     *
57
     * @var array
58
     */
59
    protected $localLang;
60
61
    /**
62
     * An instance of the localization factory
63
     *
64
     * @var \TYPO3\CMS\Core\Localization\LocalizationFactory
65
     */
66
    protected $languageFactory;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param array $arguments
72
     * @throws \ApacheSolrForTypo3\Solr\LanguageFileUnavailableException
73
     */
74
    public function __construct(array $arguments = array())
75
    {
76
        if (!isset($arguments['languageFile'])) {
77
            throw new LanguageFileUnavailableException(
78
                'No Language File given',
79
                1234972358
80
            );
81
        }
82
        $this->languageFactory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Localization\LocalizationFactory');
83
        $this->languageFile = $arguments['languageFile'];
84
        $this->llKey = $arguments['llKey'];
85
86
        $this->loadLL();
87
    }
88
89
    /**
90
     * Loads the initially defined local lang file
91
     *
92
     * @return void
93
     */
94
    protected function loadLL()
95
    {
96
        $configuration = Util::getSolrConfiguration();
97
98
        $this->localLang[$this->languageFile] = $this->languageFactory->getParsedData($this->languageFile, $this->llKey, $GLOBALS['TSFE']->renderCharset, 3);
99
100
        $localLangConfiguration = $configuration->getLocalLangConfiguration();
101
        if (count($localLangConfiguration) == 0) {
102
            return;
103
        }
104
105
        // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):
106
        foreach ($localLangConfiguration as $language => $overrideLabels) {
107
            $language = substr($language, 0, -1);
108
            if (is_array($overrideLabels)) {
109
                foreach ($overrideLabels as $labelKey => $overrideLabel) {
110
                    if (!is_array($overrideLabel)) {
111
                        $this->localLang[$this->languageFile][$language][$labelKey] = array(array('source' => $overrideLabel));
112
                    }
113
                }
114
            }
115
        }
116
    }
117
118
    /**
119
     * Returns a label for the given key
120
     *
121
     * @param array $arguments
122
     * @return string
123
     */
124
    public function execute(array $arguments = array())
125
    {
126
        $label = '';
0 ignored issues
show
Unused Code introduced by
$label is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
128
        $isFullPath = false;
129
        if (GeneralUtility::isFirstPartOfStr($arguments[0], 'FILE')) {
130
            $arguments[0] = substr($arguments[0], 5);
131
            $isFullPath = true;
132
        }
133
134
        if ($isFullPath || GeneralUtility::isFirstPartOfStr($arguments[0],
135
                'EXT')
136
        ) {
137
            // a full path reference...
138
            $label = $this->resolveFullPathLabel($arguments[0]);
139
        } else {
140
            $label = $this->getLabel($this->languageFile, $arguments[0]);
141
        }
142
143
        return $label;
144
    }
145
146
    /**
147
     * Resolves a label given through a full LLL path by loading the specified
148
     * local lang file and then returning the requested label.
149
     *
150
     * @param string $path full path specifying a label, LLL:EXT:path/to/locallang.xml:my_label
151
     * @return string the requested label
152
     */
153
    protected function resolveFullPathLabel($path)
154
    {
155
        $pathParts = explode(':', $path);
156
157
        $labelKey = array_pop($pathParts);
158
        $path = GeneralUtility::getFileAbsFileName(implode(':', $pathParts));
159
160
        if (!isset($this->localLang[$path])) {
161
            // do some nice caching
162
            $this->localLang[$path] = GeneralUtility::readLLfile(
0 ignored issues
show
Bug introduced by
The method readLLfile() does not seem to exist on object<TYPO3\CMS\Core\Utility\GeneralUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
                $path,
164
                $this->llKey,
165
                $GLOBALS['TSFE']->renderCharset
166
            );
167
        }
168
169
        return $this->getLabel($path, $labelKey);
170
    }
171
172
    /**
173
     * Gets a label from the already loaded and cached labels
174
     *
175
     * @param string $locallang key for a local lang file
176
     * @param string $labelKey label key
177
     * @return string requested label in the current language if available, in default language otherwise
178
     */
179
    protected function getLabel($locallang, $labelKey)
180
    {
181
        $label = '';
0 ignored issues
show
Unused Code introduced by
$label is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
182
183
        if (!empty($this->localLang[$locallang][$this->llKey][$labelKey])) {
184
            $label = $this->localLang[$locallang][$this->llKey][$labelKey];
185
        } else {
186
            $label = $this->localLang[$locallang]['default'][$labelKey];
187
        }
188
189
        // TYPO3 4.6 workaround until we support xliff
190
        if (is_array($label)) {
191
            if (!empty($label[0]['target'])) {
192
                $label = $label[0]['target'];
193
            } else {
194
                $label = $label[0]['source'];
195
            }
196
        }
197
198
        return $label;
199
    }
200
}
201