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