Commit 81f6f3aa authored by Fabian Galinski's avatar Fabian Galinski 😾
Browse files

[TASK] Code-Cleanup

parent 0e0cde8f
......@@ -179,7 +179,7 @@ class ConfigurationService extends AbstractService {
if ($this->extConfig['viewLocalExt']) {
if (\count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathLocalExt, $this->extConfig['viewStateExt'],
PATH_site . Typo3Lib::PATH_LOCAL_EXT, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
......@@ -189,10 +189,10 @@ class ConfigurationService extends AbstractService {
}
// global extensions
if ($this->extConfig['viewGlobalExt'] && is_dir(Typo3Lib::pathGlobalExt)) {
if ($this->extConfig['viewGlobalExt'] && is_dir(Typo3Lib::PATH_GLOBAL_EXT)) {
if (\count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathGlobalExt, $this->extConfig['viewStateExt'],
PATH_site . Typo3Lib::PATH_GLOBAL_EXT, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
......@@ -205,7 +205,7 @@ class ConfigurationService extends AbstractService {
if ($this->extConfig['viewSysExt']) {
if (\count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathSysExt, $this->extConfig['viewStateExt'],
PATH_site . Typo3Lib::PATH_SYS_EXT, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
......
......@@ -392,13 +392,7 @@ class FileBackupService extends FileService {
throw new LFException('failure.backup.notRead');
}
// convert all values back to their original charsets
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($localLang, FALSE, array('default'));
} else {
$this->localLang = $localLang;
}
$this->localLang = $localLang;
$this->originLang = $originLang;
$this->meta = $phpArray['meta'];
}
......@@ -409,13 +403,6 @@ class FileBackupService extends FileService {
* @return string prepared content (xml)
*/
private function prepareBackupContent() {
// convert all values to utf-8
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$localLang = Typo3Lib::utf8($this->localLang, TRUE, array('default'));
} else {
$localLang = $this->localLang;
}
// set meta
$phpArray['meta'] = $this->meta;
......@@ -429,18 +416,18 @@ class FileBackupService extends FileService {
}
// set data
if (is_array($localLang[$lang])) {
foreach ($localLang[$lang] as $labelKey => $labelVal) {
if (\is_array($this->localLang[$lang])) {
foreach ($this->localLang[$lang] as $labelKey => $labelVal) {
$phpArray['data'][$lang]['langData'][$labelKey] = $labelVal;
}
}
}
// define assocTagNames
$options['parentTagMap'] = array(
$options['parentTagMap'] = [
'data' => 'languageKey',
'langData' => 'label'
);
];
// get xml
return GeneralUtility::array2xml($phpArray, '', 0, 'LFBackup', 0, $options);
......
......@@ -39,32 +39,15 @@ class FileBasePHPService extends FileBaseService {
*
* @param string $file name of the file (can be a path, if you need this (no check))
* @param string $path path to the file
*
* @return void
* @throws LFException
*/
public function init($file, $path, $metaFile) {
$this->setVar(array('fileType' => 'php'));
$this->setVar(['fileType' => 'php']);
parent::init($file, $path, $metaFile);
}
/**
* calls the parent function and convert all values from the original charset to utf-8
*
* @throws LFException raised if the parent read file method fails
* @return void
*/
public function readFile() {
try {
parent::readFile();
} catch (LFException $e) {
throw $e;
}
// convert all language values from the original charset to utf-8
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, TRUE, array('default'));
}
}
/**
* reads a language file
*
......@@ -231,11 +214,6 @@ class FileBasePHPService extends FileBaseService {
* @return array language files as key and content as value
*/
protected function prepareFileContents($editedLanguages = NULL) {
// convert all language values from utf-8 to the original charset
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, FALSE, array('default'));
}
// prepare Content
$mainFileContent = '';
$languages = SgLib::getSystemLanguages();
......
......@@ -62,11 +62,6 @@ class FileBaseXLFService extends FileBaseService {
$this->localLang['default'][$constant] = '';
}
}
// convert all language values from utf-8 to the original charset
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, FALSE, array('default'));
}
}
/**
......@@ -364,11 +359,6 @@ class FileBaseXLFService extends FileBaseService {
* @return array language files as key and content as value
*/
protected function prepareFileContents($editedLanguages = NULL) {
// convert all language values to utf-8
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, TRUE, array('default'));
}
// prepare Content
$metaData = $this->prepareMeta();
$languages = SgLib::getSystemLanguages();
......
......@@ -43,32 +43,15 @@ class FileBaseXMLService extends FileBaseService {
*
* @param string $file name of the file (can be a path, if you need this (no check))
* @param string $path path to the file
*
* @return void
* @throws LFException
*/
public function init($file, $path, $metaFile) {
$this->setVar(array('fileType' => 'xml'));
$this->setVar(['fileType' => 'xml']);
parent::init($file, $path, $metaFile);
}
/**
* calls the parent function and convert all values from utf-8 to the original charset
*
* @throws LFException raised if the parent read file method fails
* @return void
*/
public function readFile() {
try {
parent::readFile();
} catch (LFException $e) {
throw $e;
}
// convert all language values from utf-8 to the original charset
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, FALSE, array('default'));
}
}
/**
* reads a language file
*
......@@ -298,23 +281,19 @@ class FileBaseXMLService extends FileBaseService {
* @return array language files as key and content as value
*/
protected function prepareFileContents($editedLanguages = NULL) {
// convert all language values to utf-8
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, TRUE, array('default'));
}
$mergedFile = TRUE;
// prepare Content
$mainFileContent = array('meta' => $this->prepareMeta());
$mainFileContent =['meta' => $this->prepareMeta()];
$languages = SgLib::getSystemLanguages();
$languageFiles = array();
$languageFiles = [];
foreach ($languages as $lang) {
// get content of localized and main file
if ($this->checkLocalizedFile(basename($this->originLang[$lang]), $lang)) {
$mergedFile = FALSE;
if (is_array($this->localLang[$lang]) && count($this->localLang[$lang])
if (\is_array($this->localLang[$lang]) && \count($this->localLang[$lang])
// check if this language content was edited
&& ($editedLanguages === NULL || in_array($lang, $editedLanguages))
&& ($editedLanguages === NULL || \in_array($lang, $editedLanguages))
) {
$languageFiles[$this->originLang[$lang]] .=
$this->array2xml(
......
......@@ -82,9 +82,6 @@ class FileOverrideService extends FileBaseXMLService {
public function readFile() {
if (is_file($this->absFile)) {
parent::readFile();
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($this->localLang, FALSE, array('default'));
}
}
$this->originalFileObject->readFile();
......
......@@ -28,22 +28,20 @@ namespace SGalinski\Lfeditor\Utility;
use Exception;
use RuntimeException;
use TYPO3\CMS\Core\Charset\CharsetConverter;
use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
/**
* includes special typo3 methods
*/
class Typo3Lib {
public const pathLocalExt = 'typo3conf/ext/';
const PATH_LOCAL_EXT = 'typo3conf/ext/';
public const pathGlobalExt = 'typo3/ext/';
const PATH_GLOBAL_EXT = 'typo3/ext/';
public const pathSysExt = 'typo3/sysext/';
const PATH_SYS_EXT = 'typo3/sysext/';
public const pathL10n = 'typo3conf/l10n/';
const PATH_L10N = 'typo3conf/l10n/';
/**
* checks the file location type
......@@ -51,20 +49,20 @@ class Typo3Lib {
* @param string $file
* @return string
*/
public static function checkFileLocation($file): ?string {
if (strpos($file, self::pathLocalExt) !== FALSE) {
public static function checkFileLocation($file): string {
if (strpos($file, self::PATH_LOCAL_EXT) !== FALSE) {
return 'local';
}
if (strpos($file, self::pathGlobalExt) !== FALSE) {
if (strpos($file, self::PATH_GLOBAL_EXT) !== FALSE) {
return 'global';
}
if (strpos($file, self::pathSysExt) !== FALSE) {
if (strpos($file, self::PATH_SYS_EXT) !== FALSE) {
return 'system';
}
if (strpos($file, self::pathL10n) !== FALSE) {
if (strpos($file, self::PATH_L10N) !== FALSE) {
return 'l10n';
}
......@@ -79,10 +77,10 @@ class Typo3Lib {
* @param boolean $mode generate to relative(false) or absolute file
* @return string converted file path
*/
public static function transTypo3File($file, $mode): ?string {
$extType['local'] = self::pathLocalExt;
$extType['global'] = self::pathGlobalExt;
$extType['system'] = self::pathSysExt;
public static function transTypo3File($file, $mode): string {
$extType['local'] = self::PATH_LOCAL_EXT;
$extType['global'] = self::PATH_GLOBAL_EXT;
$extType['system'] = self::PATH_SYS_EXT;
// relative to absolute
if ($mode) {
......@@ -125,68 +123,6 @@ class Typo3Lib {
return GeneralUtility::fixWindowsFilePath(str_replace('//', '/', $file));
}
/**
* decodes or encodes all values in the given language array to utf-8
*
* @param array $localLang language content array
* @param boolean $mode to utf-8 (true) or to original charset (false)
* @param array $ignoreKeys language keys to ignore
* @return array decoded or encoded language content array
* @throws \InvalidArgumentException
*/
public static function utf8($localLang, $mode, $ignoreKeys): array {
// check
if (!\is_array($localLang) || !\count($localLang)) {
return $localLang;
}
// get charset object
/** @var $csConvObj CharsetConverter */
$csConvObj = &$GLOBALS['LANG']->csConvObj;
// loop all possible languages
foreach ($localLang as $langKey => $convContent) {
if (!\is_array($convContent) || !\count($convContent) || \in_array($langKey, $ignoreKeys, TRUE)) {
continue;
}
$origCharset = $csConvObj->parse_charset(
$csConvObj->charSetArray[$langKey] ?
$csConvObj->charSetArray[$langKey] : 'iso-8859-1'
);
if ($csConvObj->charSetArray[$langKey] === 'utf-8') {
continue;
}
foreach ($convContent as $labelKey => $value) {
if ($mode) {
$localLang[$langKey][$labelKey] = $csConvObj->utf8_encode($value, $origCharset);
} else {
$localLang[$langKey][$labelKey] = $csConvObj->utf8_decode($value, $origCharset);
}
}
}
return $localLang;
}
/**
* Returns true if the TYPO3 backend is UTF-8 ready.
*
* @static
* @return bool
*/
public static function isTypo3BackendInUtf8Mode(): bool {
$isInUtf8Mode = FALSE;
$isTypo347 = (VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version) >= 4007000);
if ($isTypo347 || $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] === 'utf-8') {
$isInUtf8Mode = TRUE;
}
return $isInUtf8Mode;
}
/**
* Adds configuration line to AdditionalConfiguration.php.
*
......@@ -194,7 +130,7 @@ class Typo3Lib {
* @return void
* @throws \InvalidArgumentException
*/
public static function writeLineToAdditionalConfiguration($configLine): void {
public static function writeLineToAdditionalConfiguration($configLine) {
/** @var ConfigurationManager $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$additionalConfigurationFilePath = $configurationManager->getAdditionalConfigurationFileLocation();
......
......@@ -43,15 +43,17 @@ class InlineLanguageLabelsViewHelper extends AbstractViewHelper {
/**
* Renders the required javascript to make the language labels available
*
* @param mixed $labels Comma separated list of label keys to include
* @param string|array $labels Comma separated list of label keys to include
* @param boolean $htmlEscape
* @return void
*/
public function render($labels = '', $htmlEscape = FALSE) {
$extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
if (\is_array($labels)) {
$labels = GeneralUtility::trimExplode(',', $labels, TRUE);
}
$labels = GeneralUtility::trimExplode(',', $labels, TRUE);
$languageArray = array();
$languageArray = [];
foreach ($labels as $key) {
$value = LocalizationUtility::translate($key, $extensionName);
$languageArray[$key] = ($htmlEscape ? htmlentities($value) : $value);
......@@ -69,4 +71,4 @@ class InlineLanguageLabelsViewHelper extends AbstractViewHelper {
$this->getPageRenderer()->addJsInlineCode($extensionName, $javascriptCode);
}
}
\ No newline at end of file
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment