728e4f1c6114e02f9f955e47bfb6224b28525955
[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 public function initialize()
62 {
63 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
64 $this->supportedExtensions = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
65 } else {
66 $this->supportedExtensions = ['xlf', 'xml'];
67 }
68 }
69
70 /**
71 * Checks if the store contains parsed data.
72 *
73 * @param string $fileReference File reference
74 * @param string $languageKey Valid language key
75 * @return bool
76 */
77 public function hasData($fileReference, $languageKey)
78 {
79 if (isset($this->data[$fileReference][$languageKey]) && is_array($this->data[$fileReference][$languageKey])) {
80 return true;
81 }
82 return false;
83 }
84
85 /**
86 * Retrieves data from the store.
87 *
88 * This method returns all parsed languages for the current file reference.
89 *
90 * @param string $fileReference File reference
91 * @return array
92 */
93 public function getData($fileReference)
94 {
95 return $this->data[$fileReference];
96 }
97
98 /**
99 * Retrieves data from the store for a language.
100 *
101 * @param string $fileReference File reference
102 * @param string $languageKey Valid language key
103 * @return array
104 * @see self::getData()
105 */
106 public function getDataByLanguage($fileReference, $languageKey)
107 {
108 return $this->data[$fileReference][$languageKey];
109 }
110
111 /**
112 * Sets data for a specific file reference and a language.
113 *
114 * @param string $fileReference File reference
115 * @param string $languageKey Valid language key
116 * @param array $data
117 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
118 */
119 public function setData($fileReference, $languageKey, $data)
120 {
121 $this->data[$fileReference][$languageKey] = $data;
122 return $this;
123 }
124
125 /**
126 * Flushes data.
127 *
128 * @param string $fileReference
129 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
130 */
131 public function flushData($fileReference)
132 {
133 unset($this->data[$fileReference]);
134 return $this;
135 }
136
137 /**
138 * Checks file reference configuration (charset, extension, ...).
139 *
140 * @param string $fileReference File reference
141 * @param string $languageKey Valid language key
142 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
143 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
144 * @throws \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
145 */
146 public function setConfiguration($fileReference, $languageKey)
147 {
148 $this->configuration[$fileReference] = [
149 'fileReference' => $fileReference,
150 'fileExtension' => false,
151 'parserClass' => null,
152 'languageKey' => $languageKey
153 ];
154 $fileWithoutExtension = GeneralUtility::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
155 foreach ($this->supportedExtensions as $extension) {
156 if (@is_file($fileWithoutExtension . '.' . $extension)) {
157 $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
158 $this->configuration[$fileReference]['fileExtension'] = $extension;
159 break;
160 }
161 }
162 if ($this->configuration[$fileReference]['fileExtension'] === false) {
163 throw new FileNotFoundException(sprintf('Source localization file (%s) not found', $fileReference), 1306410755);
164 }
165 $extension = $this->configuration[$fileReference]['fileExtension'];
166 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
167 $this->configuration[$fileReference]['parserClass'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
168 } else {
169 throw new InvalidParserException('TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.', 1301579637);
170 }
171 if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
172 throw new InvalidParserException('TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!', 1270853900);
173 }
174 return $this;
175 }
176
177 /**
178 * Get the fileReference without the extension
179 *
180 * @param string $fileReference File reference
181 * @return string
182 */
183 public function getFileReferenceWithoutExtension($fileReference)
184 {
185 if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
186 $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\\.[a-z0-9]+$/i', '', $fileReference);
187 }
188 return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
189 }
190
191 /**
192 * Returns the correct parser for a specific file reference.
193 *
194 * @param string $fileReference File reference
195 * @return \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
196 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
197 */
198 public function getParserInstance($fileReference)
199 {
200 if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
201 return GeneralUtility::makeInstance((string)$this->configuration[$fileReference]['parserClass']);
202 } else {
203 throw new InvalidParserException(sprintf('Invalid parser configuration for the current file (%s)', $fileReference), 1307293692);
204 }
205 }
206
207 /**
208 * Gets the absolute file path.
209 *
210 * @param string $fileReference
211 * @return string
212 * @throws \InvalidArgumentException
213 */
214 public function getAbsoluteFileReference($fileReference)
215 {
216 if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
217 return (string)$this->configuration[$fileReference]['fileReference'];
218 } else {
219 throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1307293693);
220 }
221 }
222
223 /**
224 * Get supported extensions
225 *
226 * @return array
227 */
228 public function getSupportedExtensions()
229 {
230 return $this->supportedExtensions;
231 }
232 }