‪TYPO3CMS  ‪main
AbstractXmlParser.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
29 {
33  protected ‪$sourcePath;
34 
38  protected ‪$languageKey;
39 
44  public function ‪parseExtensionResource(string ‪$sourcePath, string ‪$languageKey, string $fileNamePattern): array
45  {
46  $fileName = ‪Environment::getLabelsPath() . sprintf($fileNamePattern, ‪$languageKey);
47 
48  return $this->‪_getParsedData($sourcePath, ‪$languageKey, $fileName);
49  }
50 
60  {
61  return $this->‪_getParsedData($sourcePath, ‪$languageKey, null);
62  }
63 
72  protected function ‪_getParsedData(‪$sourcePath, ‪$languageKey, ?string $labelsPath)
73  {
74  $this->sourcePath = ‪$sourcePath;
75  $this->languageKey = ‪$languageKey;
76  if ($this->languageKey !== 'default') {
77  $this->sourcePath = $labelsPath ?? $this->‪getLocalizedFileName($this->sourcePath, $this->languageKey);
78  if (!@is_file($this->sourcePath)) {
79  // Global localization is not available, try split localization file
80  $this->sourcePath = $this->‪getLocalizedFileName($sourcePath, ‪$languageKey, true);
81  }
82  if (!@is_file($this->sourcePath)) {
83  throw new ‪FileNotFoundException('Localization file does not exist', 1306332397);
84  }
85  }
86  $LOCAL_LANG = [];
87  $LOCAL_LANG[‪$languageKey] = $this->‪parseXmlFile();
88  return $LOCAL_LANG;
89  }
90 
97  protected function ‪parseXmlFile()
98  {
99  $xmlContent = file_get_contents($this->sourcePath);
100  if ($xmlContent === false) {
101  throw new ‪InvalidXmlFileException(
102  'The path provided does not point to an existing and accessible file.',
103  1278155987
104  );
105  }
106  $rootXmlNode = simplexml_load_string($xmlContent, \SimpleXMLElement::class, LIBXML_NOWARNING);
107  if ($rootXmlNode === false) {
108  $xmlError = libxml_get_last_error();
109  throw new ‪InvalidXmlFileException(
110  'The path provided does not point to an existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
111  1278155988
112  );
113  }
114  return $this->‪doParsingFromRoot($rootXmlNode);
115  }
116 
126  protected function ‪getLocalizedFileName(string $fileRef, string $language, bool $sameLocation = false)
127  {
128  // If $fileRef is already prefixed with "[language key]" then we should return it as is
129  $fileName = ‪PathUtility::basename($fileRef);
130  if (str_starts_with($fileName, $language . '.')) {
131  return GeneralUtility::getFileAbsFileName($fileRef);
132  }
133 
134  if ($sameLocation) {
135  return GeneralUtility::getFileAbsFileName(str_replace($fileName, $language . '.' . $fileName, $fileRef));
136  }
137 
138  // Analyze file reference
139  if (str_starts_with($fileRef, ‪Environment::getFrameworkBasePath() . '/')) {
140  // Is system
141  $validatedPrefix = ‪Environment::getFrameworkBasePath() . '/';
142  } elseif (str_starts_with($fileRef, ‪Environment::getExtensionsPath() . '/')) {
143  // Is local
144  $validatedPrefix = ‪Environment::getExtensionsPath() . '/';
145  } else {
146  $validatedPrefix = '';
147  }
148  if ($validatedPrefix) {
149  // Divide file reference into extension key, directory (if any) and base name:
150  [$extensionKey, $file_extPath] = explode('/', substr($fileRef, strlen($validatedPrefix)), 2);
151  $temp = ‪GeneralUtility::revExplode('/', $file_extPath, 2);
152  if (count($temp) === 1) {
153  array_unshift($temp, '');
154  }
155  // Add empty first-entry if not there.
156  [$file_extPath, $file_fileName] = $temp;
157  // The filename is prefixed with "[language key]." because it prevents the llxmltranslate tool from detecting it.
158  return ‪Environment::getLabelsPath() . '/' . $language . '/' . $extensionKey . '/' . ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName;
159  }
160  return '';
161  }
162 
169  abstract protected function ‪doParsingFromRoot(\SimpleXMLElement $root);
170 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
Definition: LocalizationParserInterface.php:22
‪TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
Definition: FileNotFoundException.php:22
‪TYPO3\CMS\Core\Core\Environment\getLabelsPath
‪static getLabelsPath()
Definition: Environment.php:233
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static getExtensionsPath()
Definition: Environment.php:264
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$sourcePath
‪string $sourcePath
Definition: AbstractXmlParser.php:32
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static basename(string $path)
Definition: PathUtility.php:219
‪TYPO3\CMS\Core\Utility\GeneralUtility\revExplode
‪static list< string > revExplode($delimiter, $string, $limit=0)
Definition: GeneralUtility.php:882
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser
Definition: AbstractXmlParser.php:29
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\_getParsedData
‪array _getParsedData($sourcePath, $languageKey, ?string $labelsPath)
Definition: AbstractXmlParser.php:70
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\parseExtensionResource
‪parseExtensionResource(string $sourcePath, string $languageKey, string $fileNamePattern)
Definition: AbstractXmlParser.php:42
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\doParsingFromRoot
‪array doParsingFromRoot(\SimpleXMLElement $root)
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$languageKey
‪string $languageKey
Definition: AbstractXmlParser.php:36
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static getFrameworkBasePath()
Definition: Environment.php:256
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getLocalizedFileName
‪string getLocalizedFileName(string $fileRef, string $language, bool $sameLocation=false)
Definition: AbstractXmlParser.php:124
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Localization\Parser
Definition: AbstractXmlParser.php:16
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\parseXmlFile
‪array parseXmlFile()
Definition: AbstractXmlParser.php:95
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
Definition: InvalidXmlFileException.php:22
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getParsedData
‪array getParsedData($sourcePath, $languageKey)
Definition: AbstractXmlParser.php:57