[CLEANUP] Ensure variables initalized and fix code smell
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / LanguageStore.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\Localization\Exception\FileNotFoundException;
18 use TYPO3\CMS\Core\Localization\Exception\InvalidParserException;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Language store.
23 */
24 class LanguageStore implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * File extension supported by the localization parser
28 *
29 * @var array
30 */
31 protected $supportedExtensions;
32
33 /**
34 * Information about parsed file
35 *
36 * If data come from the cache, this array does not contain
37 * any information about this file
38 *
39 * @var array
40 */
41 protected $configuration;
42
43 /**
44 * Parsed localization file
45 *
46 * @var array
47 */
48 protected $data;
49
50 /**
51 * Constructor
52 */
53 public function __construct()
54 {
55 $this->initialize();
56 }
57
58 /**
59 * Initializes the current class.
60 *
61 * @return void
62 */
63 public function initialize()
64 {
65 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
66 $this->supportedExtensions = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
67 } else {
68 $this->supportedExtensions = ['xlf', 'xml'];
69 }
70 }
71
72 /**
73 * Checks if the store contains parsed data.
74 *
75 * @param string $fileReference File reference
76 * @param string $languageKey Valid language key
77 * @return bool
78 */
79 public function hasData($fileReference, $languageKey)
80 {
81 if (isset($this->data[$fileReference][$languageKey]) && is_array($this->data[$fileReference][$languageKey])) {
82 return true;
83 }
84 return false;
85 }
86
87 /**
88 * Retrieves data from the store.
89 *
90 * This method returns all parsed languages for the current file reference.
91 *
92 * @param string $fileReference File reference
93 * @return array
94 */
95 public function getData($fileReference)
96 {
97 return $this->data[$fileReference];
98 }
99
100 /**
101 * Retrieves data from the store for a language.
102 *
103 * @param string $fileReference File reference
104 * @param string $languageKey Valid language key
105 * @return array
106 * @see self::getData()
107 */
108 public function getDataByLanguage($fileReference, $languageKey)
109 {
110 return $this->data[$fileReference][$languageKey];
111 }
112
113 /**
114 * Sets data for a specific file reference and a language.
115 *
116 * @param string $fileReference File reference
117 * @param string $languageKey Valid language key
118 * @param array $data
119 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
120 */
121 public function setData($fileReference, $languageKey, $data)
122 {
123 $this->data[$fileReference][$languageKey] = $data;
124 return $this;
125 }
126
127 /**
128 * Flushes data.
129 *
130 * @param string $fileReference
131 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
132 */
133 public function flushData($fileReference)
134 {
135 unset($this->data[$fileReference]);
136 return $this;
137 }
138
139 /**
140 * Checks file reference configuration (charset, extension, ...).
141 *
142 * @param string $fileReference File reference
143 * @param string $languageKey Valid language key
144 * @param string $charset Rendering charset
145 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
146 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
147 * @throws \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
148 */
149 public function setConfiguration($fileReference, $languageKey, $charset)
150 {
151 $this->configuration[$fileReference] = [
152 'fileReference' => $fileReference,
153 'fileExtension' => false,
154 'parserClass' => null,
155 'languageKey' => $languageKey,
156 'charset' => $charset
157 ];
158 $fileWithoutExtension = GeneralUtility::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
159 foreach ($this->supportedExtensions as $extension) {
160 if (@is_file($fileWithoutExtension . '.' . $extension)) {
161 $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
162 $this->configuration[$fileReference]['fileExtension'] = $extension;
163 break;
164 }
165 }
166 if ($this->configuration[$fileReference]['fileExtension'] === false) {
167 throw new FileNotFoundException(sprintf('Source localization file (%s) not found', $fileReference), 1306410755);
168 }
169 $extension = $this->configuration[$fileReference]['fileExtension'];
170 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
171 $this->configuration[$fileReference]['parserClass'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
172 } else {
173 throw new InvalidParserException('TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.', 1301579637);
174 }
175 if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
176 throw new InvalidParserException('TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!', 1270853900);
177 }
178 return $this;
179 }
180
181 /**
182 * Get the fileReference without the extension
183 *
184 * @param string $fileReference File reference
185 * @return string
186 */
187 public function getFileReferenceWithoutExtension($fileReference)
188 {
189 if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
190 $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\\.[a-z0-9]+$/i', '', $fileReference);
191 }
192 return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
193 }
194
195 /**
196 * Returns the correct parser for a specific file reference.
197 *
198 * @param string $fileReference File reference
199 * @return \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
200 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
201 */
202 public function getParserInstance($fileReference)
203 {
204 if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
205 return GeneralUtility::makeInstance((string)$this->configuration[$fileReference]['parserClass']);
206 } else {
207 throw new InvalidParserException(sprintf('Invalid parser configuration for the current file (%s)', $fileReference), 1307293692);
208 }
209 }
210
211 /**
212 * Gets the absolute file path.
213 *
214 * @param string $fileReference
215 * @return string
216 * @throws \InvalidArgumentException
217 */
218 public function getAbsoluteFileReference($fileReference)
219 {
220 if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
221 return (string)$this->configuration[$fileReference]['fileReference'];
222 } else {
223 throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1307293693);
224 }
225 }
226
227 /**
228 * Get supported extensions
229 *
230 * @return array
231 */
232 public function getSupportedExtensions()
233 {
234 return $this->supportedExtensions;
235 }
236 }