Skip to content

Commit 354faf0

Browse files
authored
[6.0] Cache language files (#45289)
1 parent aa6aacd commit 354faf0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

libraries/src/Language/LanguageHelper.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Joomla\CMS\Installer\Installer;
1616
use Joomla\CMS\Log\Log;
1717
use Joomla\Database\DatabaseInterface;
18+
use Joomla\Filesystem\Exception\FilesystemException;
1819
use Joomla\Filesystem\File;
20+
use Joomla\Filesystem\Folder;
1921
use Joomla\Registry\Registry;
2022
use Joomla\Utilities\ArrayHelper;
2123

@@ -444,6 +446,12 @@ public static function parseIniFile($fileName, $debug = false)
444446
return [];
445447
}
446448

449+
$cacheFile = JPATH_CACHE . '/language/' . ltrim(str_replace([JPATH_ROOT, '/'], ['', '-'], $fileName), '-') . '.' . filemtime($fileName) . '.php';
450+
451+
if (is_file($cacheFile)) {
452+
return include $cacheFile;
453+
}
454+
447455
// This was required for https://github.com/joomla/joomla-cms/issues/17198 but not sure what server setup
448456
// issue it is solving
449457
$disabledFunctions = explode(',', \ini_get('disable_functions'));
@@ -471,10 +479,28 @@ public static function parseIniFile($fileName, $debug = false)
471479
restore_error_handler();
472480
}
473481

482+
if (!\is_array($strings)) {
483+
$strings = [];
484+
}
485+
474486
// Ini files are processed in the "RAW" mode of parse_ini_string, leaving escaped quotes untouched - lets postprocess them
475487
$strings = str_replace('\"', '"', $strings);
476488

477-
return \is_array($strings) ? $strings : [];
489+
// Write cache
490+
try {
491+
Folder::create(\dirname($cacheFile));
492+
493+
$data = "<?php\ndefined('_JEXEC') or die;\nreturn " . var_export($strings, true) . ";\n";
494+
$bytesWritten = file_put_contents($cacheFile, $data);
495+
496+
if ($bytesWritten === false || $bytesWritten < \strlen($data)) {
497+
throw new FilesystemException('Unable to write cache file');
498+
}
499+
} catch (\FilesystemException $e) {
500+
// We ignore the error, as the file is for caching only.
501+
}
502+
503+
return $strings;
478504
}
479505

480506
/**

0 commit comments

Comments
 (0)