WEB: Load localized data in models

This commit is contained in:
Mataniko
2020-10-10 01:34:06 -04:00
committed by Matan Bareket
parent 34dbf61a23
commit 08fffe6dd0
11 changed files with 32 additions and 28 deletions
+18
View File
@@ -7,6 +7,8 @@ use Phpfastcache\Exceptions\PhpfastcacheDriverException;
abstract class BasicModel
{
const FILE_NOT_FOUND = 'The filename %s could not be found';
protected static $cache;
public function __construct()
@@ -23,6 +25,22 @@ abstract class BasicModel
}
}
protected function getLocalizedFile($filename) {
global $lang;
if (!$lang) {
$lang = DEFAULT_LOCALE;
}
$localizedFilename = DIR_DATA . "/$lang/$filename";
$defaultFilename = DIR_DATA . "/" . DEFAULT_LOCALE . "/$filename";
if (is_file($localizedFilename) && is_readable($localizedFilename)) {
return $localizedFilename;
} elseif (is_file($defaultFilename) && is_readable($defaultFilename)) {
return $defaultFilename;
} else {
throw new \ErrorException(\sprintf(self::FILE_NOT_FOUND, $filename));
}
}
protected function saveToCache($data, $key = '')
{
if ($key) {
+5 -1
View File
@@ -24,12 +24,16 @@ class CompatibilityModel extends BasicModel
$this->platformsModel = new SimpleModel("Platform", "platforms.yaml");
}
public function getLastUpdated() {
return filemtime($this->getLocalizedFile("compatibility.yaml"));
}
/* Get all the groups and the respectively demos for the specified ScummVM version. */
public function getAllData($version)
{
$data = $this->getFromCache($version);
if (is_null($data)) {
$fname = DIR_DATA . "/compatibility.yaml";
$fname = $this->getLocalizedFile("compatibility.yaml");
$compatibilityEntries = \yaml_parse_file($fname);
$games = $this->gameModel->getAllGames();
$platforms = $this->platformsModel->getAllData();
+1 -1
View File
@@ -15,7 +15,7 @@ class DownloadsModel extends BasicModel
{
$sections = $this->getFromCache();
if (is_null($sections)) {
$fname = DIR_DATA . '/downloads.xml';
$fname = $this->getLocalizedFile('downloads.xml');
/* Now parse the data. */
$parser = new XMLParser();
$parsedData = $parser->parseByFilename($fname);
+1 -16
View File
@@ -19,22 +19,7 @@ class FAQModel extends BasicModel
/* Get the full path and filename for the F.A.Q. XML-file. */
public function getFilename()
{
global $lang;
if ($lang !== DEFAULT_LOCALE) {
$localized = DIR_DATA . "/faq-xml." . $lang . ".xml";
if (is_file($localized)) {
if (!is_readable($localized)) {
$file = "\n\nFilename: " . basename($localized) . "\n";
throw new \ErrorException(self::FILE_NOT_FOUND . $file);
} else {
return $localized;
}
}
}
return DIR_DATA . '/faq-xml.xml';
return $this->getLocalizedFile('faq.xml');
}
/* Get last modification time. */
+1 -1
View File
@@ -23,7 +23,7 @@ class GameDemosModel extends BasicModel
{
$groupedData = $this->getFromCache();
if (is_null($groupedData)) {
$fname = DIR_DATA . '/game_demos.yaml';
$fname = $this->getLocalizedFile('game_demos.yaml');
$demos = \yaml_parse_file($fname);
$games = $this->gameModel->getAllGames();
$platforms = $this->platformsModel->getAllData();
+1 -1
View File
@@ -12,7 +12,7 @@ class GameDownloadsModel extends BasicModel
/* Get all download entries. */
public function getAllDownloads()
{
$fname = DIR_DATA . '/games.xml';
$fname = $this->getLocalizedFile('games.xml');
/* Now parse the data. */
$parser = new XMLParser();
$parsedData = $parser->parseByFilename($fname);
+1 -1
View File
@@ -28,7 +28,7 @@ class GameModel extends BasicModel
$companies = $this->companiesModel->getAllData();
$engines = $this->enginesModel->getAllData();
$series = $this->seriesModel->getAllData();
$fname = DIR_DATA . '/games.yaml';
$fname = $this->getLocalizedFile('games.yaml');
$games = \yaml_parse_file($fname);
$data = [];
foreach ($games as $game) {
+1 -1
View File
@@ -14,7 +14,7 @@ class LinksModel extends BasicModel
{
$entries = $this->getFromCache();
if (is_null($entries)) {
$fname = DIR_DATA . '/links.yaml';
$fname = $this->getLocalizedFile('links.yaml');
$parsedData = \yaml_parse_file($fname);
$entries = [];
foreach ($parsedData as $value) {
+1 -1
View File
@@ -26,7 +26,7 @@ class ScreenshotsModel extends BasicModel
/* Get all screenshots. */
public function getAllScreenshots()
{
$fname = DIR_DATA . '/screenshots.yaml';
$fname = $this->getLocalizedFile('screenshots.yaml');
$screenshots = \yaml_parse_file($fname);
$platforms = $this->platformsModel->getAllData();
$games = $this->gameModel->getAllGames();
+1 -4
View File
@@ -16,10 +16,7 @@ class SimpleModel extends BasicModel
public function __construct($type, $filename)
{
parent::__construct();
$this->filename = DIR_DATA . "/$filename";
if (!is_file($this->filename) || !is_readable($this->filename)) {
throw new \ErrorException(\sprintf(self::FILE_NOT_FOUND, $this->filename));
}
$this->filename = $this->getLocalizedFile($filename);
$this->type = "ScummVM\Objects\\$type";
}
+1 -1
View File
@@ -13,7 +13,7 @@ class VersionsModel extends BasicModel
{
$data = $this->getFromCache();
if (is_null($data)) {
$fname = DIR_DATA . '/versions.yaml';
$fname = $this->getLocalizedFile('versions.yaml');
$versions = \yaml_parse_file($fname);
$data = [];