Skip to content

[6.0] Cache language files #45289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion libraries/src/Language/LanguageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Log\Log;
use Joomla\Database\DatabaseInterface;
use Joomla\Filesystem\Exception\FilesystemException;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

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

$cacheFile = JPATH_CACHE . '/language/' . ltrim(str_replace([JPATH_ROOT, '/'], ['', '-'], $fileName), '-') . '.' . filemtime($fileName) . '.php';

if (is_file($cacheFile)) {
return include $cacheFile;
}

// This was required for https://github.com/joomla/joomla-cms/issues/17198 but not sure what server setup
// issue it is solving
$disabledFunctions = explode(',', \ini_get('disable_functions'));
Expand Down Expand Up @@ -471,10 +479,28 @@ public static function parseIniFile($fileName, $debug = false)
restore_error_handler();
}

if (!\is_array($strings)) {
$strings = [];
}

// Ini files are processed in the "RAW" mode of parse_ini_string, leaving escaped quotes untouched - lets postprocess them
$strings = str_replace('\"', '"', $strings);

return \is_array($strings) ? $strings : [];
// Write cache
try {
Folder::create(\dirname($cacheFile));

$data = "<?php\ndefined('_JEXEC') or die;\nreturn " . var_export($strings, true) . ";\n";
$bytesWritten = file_put_contents($cacheFile, $data);

if ($bytesWritten === false || $bytesWritten < \strlen($data)) {
throw new FilesystemException('Unable to write cache file');
}
} catch (\FilesystemException $e) {
// We ignore the error, as the file is for caching only.
}

return $strings;
}

/**
Expand Down