[BUGFIX] locallangXMLOverride hook has been removed
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / classes / class.tx_lang_store.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Language store.
30 *
31 * @package Core
32 * @subpackage tx_lang
33 * @author Dominique Feyer <dominique.feyer@reelpeek.net>
34 */
35 class tx_lang_Store implements t3lib_Singleton {
36
37 /**
38 * File extension supported by the localization parser
39 *
40 * @var array
41 */
42 protected $supportedExtensions;
43
44 /**
45 * Information about parsed file
46 *
47 * If data come from the cache, this array does not contain
48 * any information about this file
49 *
50 * @var array
51 */
52 protected $configuration;
53
54 /**
55 * Parsed localization file
56 *
57 * @var array
58 */
59 protected $data;
60
61 /**
62 * Constructor
63 */
64 public function __construct() {
65 $this->initialize();
66 }
67
68 /**
69 * Initializes the current class.
70 *
71 * @return void
72 */
73 public function initialize() {
74 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
75 $this->supportedExtensions = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
76 } else {
77 $this->supportedExtensions = array('xlf', 'xml', 'php');
78 }
79 }
80
81 /**
82 * Checks if the store contains parsed data.
83 *
84 * @param string $fileReference File reference
85 * @param string $languageKey Valid language key
86 * @return bool
87 */
88 public function hasData($fileReference, $languageKey) {
89 if (isset($this->data[$fileReference][$languageKey]) && is_array($this->data[$fileReference][$languageKey])) {
90 return TRUE;
91 }
92 return FALSE;
93 }
94
95 /**
96 * Retrieves data from the store.
97 *
98 * This method returns all parsed languages for the current file reference.
99 *
100 * @param string $fileReference File reference
101 * @return array
102 */
103 public function getData($fileReference) {
104 return $this->data[$fileReference];
105 }
106
107 /**
108 * Retrieves data from the store for a language.
109 *
110 * @param string $fileReference File reference
111 * @param string $languageKey Valid language key
112 * @return array
113 * @see self::getData()
114 */
115 public function getDataByLanguage($fileReference, $languageKey) {
116 return $this->data[$fileReference][$languageKey];
117 }
118
119 /**
120 * Sets data for a specific file reference and a language.
121 *
122 * @param string $fileReference File reference
123 * @param string $languageKey Valid language key
124 * @param $data
125 * @return tx_lang_Store This instance to allow method chaining
126 */
127 public function setData($fileReference, $languageKey, $data) {
128 $this->data[$fileReference][$languageKey] = $data;
129 return $this;
130 }
131
132 /**
133 * Flushes data.
134 *
135 * @param string $fileReference
136 * @return tx_lang_Store This instance to allow method chaining
137 */
138 public function flushData($fileReference) {
139 unset($this->data[$fileReference]);
140 return $this;
141 }
142
143 /**
144 * Checks file reference configuration (charset, extension, ...).
145 *
146 * @throws tx_lang_exception_InvalidParser|tx_lang_exception_FileNotFound
147 * @param string $fileReference File reference
148 * @param string $languageKey Valid language key
149 * @param string $charset Rendering charset
150 * @return tx_lang_Store This instance to allow method chaining
151 */
152 public function setConfiguration($fileReference, $languageKey, $charset) {
153 $this->configuration[$fileReference] = array(
154 'fileReference' => $fileReference,
155 'fileExtension' => FALSE,
156 'parserClass' => NULL,
157 'languageKey' => $languageKey,
158 'charset' => $charset
159 );
160
161 $fileWithoutExtension = t3lib_div::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
162
163 foreach ($this->supportedExtensions as $extension) {
164 if (@is_file($fileWithoutExtension . '.' . $extension)) {
165 $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
166 $this->configuration[$fileReference]['fileExtension'] = $extension;
167 break;
168 }
169 }
170
171 if ($this->configuration[$fileReference]['fileExtension'] === FALSE) {
172 throw new tx_lang_exception_FileNotFound(
173 sprintf('Source localization file (%s) not found', $fileReference),
174 1306410755
175 );
176 }
177
178 $extension = $this->configuration[$fileReference]['fileExtension'];
179
180 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
181 $this->configuration[$fileReference]['parserClass'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
182 } else {
183 throw new tx_lang_exception_InvalidParser(
184 'TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.',
185 1301579637
186 );
187 }
188
189 if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
190 throw new tx_lang_exception_InvalidParser(
191 'TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!',
192 1270853900
193 );
194 }
195
196 return $this;
197 }
198
199 /**
200 * Get the filereference without the extension
201 *
202 * @param string $fileReference File reference
203 * @return string
204 */
205 public function getFileReferenceWithoutExtension($fileReference) {
206 if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
207 $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\.[a-z0-9]+$/i' , '' , $fileReference);
208 }
209 return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
210 }
211
212 /**
213 * Returns the correct parser for a specific file reference.
214 *
215 * @throws tx_lang_exception_InvalidParser
216 * @param string $fileReference File reference
217 * @return tx_lang_ParserInterface
218 */
219 public function getParserInstance($fileReference) {
220 if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
221 return t3lib_div::makeInstance((string) $this->configuration[$fileReference]['parserClass']);
222 } else {
223 throw new tx_lang_exception_InvalidParser(
224 sprintf('Invalid parser configuration for the current file (%s)', $fileReference),
225 1307293692
226 );
227 }
228 }
229
230 /**
231 * Gets the absolute file path.
232 *
233 * @throws InvalidArgumentException
234 * @param string $fileReference
235 * @return string
236 */
237 public function getAbsoluteFileReference($fileReference) {
238 if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
239 return (string) $this->configuration[$fileReference]['fileReference'];
240 } else {
241 throw new InvalidArgumentException(
242 sprintf('Invalid file reference configuration for the current file (%s)', $fileReference),
243 1307293692
244 );
245 }
246 }
247
248 /**
249 * Get supported extensions
250 *
251 * @return array
252 */
253 public function getSupportedExtensions() {
254 return $this->supportedExtensions;
255 }
256 }
257
258 ?>