[BUGFIX] Fix phpdoc after namespacing
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / LanguageStore.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
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 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Language store.
31 *
32 * @author Dominique Feyer <dominique.feyer@reelpeek.net>
33 */
34 class LanguageStore implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * File extension supported by the localization parser
38 *
39 * @var array
40 */
41 protected $supportedExtensions;
42
43 /**
44 * Information about parsed file
45 *
46 * If data come from the cache, this array does not contain
47 * any information about this file
48 *
49 * @var array
50 */
51 protected $configuration;
52
53 /**
54 * Parsed localization file
55 *
56 * @var array
57 */
58 protected $data;
59
60 /**
61 * Constructor
62 */
63 public function __construct() {
64 $this->initialize();
65 }
66
67 /**
68 * Initializes the current class.
69 *
70 * @return void
71 */
72 public function initialize() {
73 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
74 $this->supportedExtensions = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
75 } else {
76 $this->supportedExtensions = array('xlf', 'xml', 'php');
77 }
78 }
79
80 /**
81 * Checks if the store contains parsed data.
82 *
83 * @param string $fileReference File reference
84 * @param string $languageKey Valid language key
85 * @return boolean
86 */
87 public function hasData($fileReference, $languageKey) {
88 if (isset($this->data[$fileReference][$languageKey]) && is_array($this->data[$fileReference][$languageKey])) {
89 return TRUE;
90 }
91 return FALSE;
92 }
93
94 /**
95 * Retrieves data from the store.
96 *
97 * This method returns all parsed languages for the current file reference.
98 *
99 * @param string $fileReference File reference
100 * @return array
101 */
102 public function getData($fileReference) {
103 return $this->data[$fileReference];
104 }
105
106 /**
107 * Retrieves data from the store for a language.
108 *
109 * @param string $fileReference File reference
110 * @param string $languageKey Valid language key
111 * @return array
112 * @see self::getData()
113 */
114 public function getDataByLanguage($fileReference, $languageKey) {
115 return $this->data[$fileReference][$languageKey];
116 }
117
118 /**
119 * Sets data for a specific file reference and a language.
120 *
121 * @param string $fileReference File reference
122 * @param string $languageKey Valid language key
123 * @param array $data
124 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
125 */
126 public function setData($fileReference, $languageKey, $data) {
127 $this->data[$fileReference][$languageKey] = $data;
128 return $this;
129 }
130
131 /**
132 * Flushes data.
133 *
134 * @param string $fileReference
135 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
136 */
137 public function flushData($fileReference) {
138 unset($this->data[$fileReference]);
139 return $this;
140 }
141
142 /**
143 * Checks file reference configuration (charset, extension, ...).
144 *
145 * @param string $fileReference File reference
146 * @param string $languageKey Valid language key
147 * @param string $charset Rendering charset
148 * @return \TYPO3\CMS\Core\Localization\LanguageStore This instance to allow method chaining
149 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
150 * @throws \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
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 $fileWithoutExtension = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
161 foreach ($this->supportedExtensions as $extension) {
162 if (@is_file(($fileWithoutExtension . '.' . $extension))) {
163 $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
164 $this->configuration[$fileReference]['fileExtension'] = $extension;
165 break;
166 }
167 }
168 if ($this->configuration[$fileReference]['fileExtension'] === FALSE) {
169 throw new \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException(sprintf('Source localization file (%s) not found', $fileReference), 1306410755);
170 }
171 $extension = $this->configuration[$fileReference]['fileExtension'];
172 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
173 $this->configuration[$fileReference]['parserClass'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
174 } else {
175 throw new \TYPO3\CMS\Core\Localization\Exception\InvalidParserException('TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.', 1301579637);
176 }
177 if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
178 throw new \TYPO3\CMS\Core\Localization\Exception\InvalidParserException('TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!', 1270853900);
179 }
180 return $this;
181 }
182
183 /**
184 * Get the filereference without the extension
185 *
186 * @param string $fileReference File reference
187 * @return string
188 */
189 public function getFileReferenceWithoutExtension($fileReference) {
190 if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
191 $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\\.[a-z0-9]+$/i', '', $fileReference);
192 }
193 return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
194 }
195
196 /**
197 * Returns the correct parser for a specific file reference.
198 *
199 * @param string $fileReference File reference
200 * @return \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
201 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidParserException
202 */
203 public function getParserInstance($fileReference) {
204 if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
205 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance((string) $this->configuration[$fileReference]['parserClass']);
206 } else {
207 throw new \TYPO3\CMS\Core\Localization\Exception\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 if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
220 return (string) $this->configuration[$fileReference]['fileReference'];
221 } else {
222 throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1307293692);
223 }
224 }
225
226 /**
227 * Get supported extensions
228 *
229 * @return array
230 */
231 public function getSupportedExtensions() {
232 return $this->supportedExtensions;
233 }
234
235 }
236
237
238 ?>