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