4cc499391fd68c111458dfe93dc6d355a02e1e0c
2 /***************************************************************
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
33 * @author Dominique Feyer <dominique.feyer@reelpeek.net>
35 class t3lib_l10n_Store
implements t3lib_Singleton
{
38 * File extension supported by the localization parser
42 protected $supportedExtensions;
45 * Information about parsed file
47 * If data come from the cache, this array does not contain
48 * any information about this file
52 protected $configuration;
55 * Parsed localization file
64 public function __construct() {
69 * Initializes the current class.
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']);
77 $this->supportedExtensions
= array('xlf', 'xml', 'php');
82 * Checks if the store contains parsed data.
84 * @param string $fileReference File reference
85 * @param string $languageKey Valid language key
88 public function hasData($fileReference, $languageKey) {
89 if (isset($this->data
[$fileReference][$languageKey]) && is_array($this->data
[$fileReference][$languageKey])) {
96 * Retrieves data from the store.
98 * This method returns all parsed languages for the current file reference.
100 * @param string $fileReference File reference
103 public function getData($fileReference) {
104 return $this->data
[$fileReference];
108 * Retrieves data from the store for a language.
110 * @param string $fileReference File reference
111 * @param string $languageKey Valid language key
113 * @see self::getData()
115 public function getDataByLanguage($fileReference, $languageKey) {
116 return $this->data
[$fileReference][$languageKey];
120 * Sets data for a specific file reference and a language.
122 * @param string $fileReference File reference
123 * @param string $languageKey Valid language key
125 * @return t3lib_l10n_Store This instance to allow method chaining
127 public function setData($fileReference, $languageKey, $data) {
128 $this->data
[$fileReference][$languageKey] = $data;
135 * @param string $fileReference
136 * @return t3lib_l10n_Store This instance to allow method chaining
138 public function flushData($fileReference) {
139 unset($this->data
[$fileReference]);
144 * Checks file reference configuration (charset, extension, ...).
146 * @throws t3lib_l10n_exception_InvalidParser|t3lib_l10n_exception_FileNotFound
147 * @param string $fileReference File reference
148 * @param string $languageKey Valid language key
149 * @param string $charset Rendering charset
150 * @return t3lib_l10n_Store This instance to allow method chaining
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
161 $fileWithoutExtension = t3lib_div
::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
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;
171 if ($this->configuration
[$fileReference]['fileExtension'] === FALSE) {
172 throw new t3lib_l10n_exception_FileNotFound(
173 sprintf('Source localization file (%s) not found', $fileReference),
178 $extension = $this->configuration
[$fileReference]['fileExtension'];
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];
183 throw new t3lib_l10n_exception_InvalidParser(
184 'TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.',
189 if (!class_exists($this->configuration
[$fileReference]['parserClass']) ||
trim($this->configuration
[$fileReference]['parserClass']) === '') {
190 throw new t3lib_l10n_exception_InvalidParser(
191 'TYPO3 Fatal Error: l10n parser "' . $this->configuration
[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!',
200 * Get the filereference without the extension
202 * @param string $fileReference File reference
205 public function getFileReferenceWithoutExtension($fileReference) {
206 if (!isset($this->configuration
[$fileReference]['fileReferenceWithoutExtension'])) {
207 $this->configuration
[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\.[a-z0-9]+$/i' , '' , $fileReference);
209 return $this->configuration
[$fileReference]['fileReferenceWithoutExtension'];
213 * Returns the correct parser for a specific file reference.
215 * @throws t3lib_l10n_exception_InvalidParser
216 * @param string $fileReference File reference
217 * @return t3lib_l10n_parser
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']);
223 throw new t3lib_l10n_exception_InvalidParser(
224 sprintf('Invalid parser configuration for the current file (%s)', $fileReference),
231 * Gets the absolute file path.
233 * @throws InvalidArgumentException
234 * @param string $fileReference
237 public function getAbsoluteFileReference($fileReference) {
238 if (isset($this->configuration
[$fileReference]['fileReference']) && trim($this->configuration
[$fileReference]['fileReference']) !== '') {
239 return (string) $this->configuration
[$fileReference]['fileReference'];
241 throw new InvalidArgumentException(
242 sprintf('Invalid file reference configuration for the current file (%s)', $fileReference),
249 * Get supported extensions
253 public function getSupportedExtensions() {
254 return $this->supportedExtensions
;