Bladeren bron

Refactorise RSSThemes

* Évite de charger les informations de tous les thèmes lorsque ce n'est
pas nécessaire (c.à.d. en dehors de la page de configuration).
* Permettra de choisir des icônes différentes selon les thèmes sans
nécessairement passer par une CSS.
* Contribue à https://github.com/marienfressinaud/FreshRSS/issues/284
Alexandre Alapetite 12 jaren geleden
bovenliggende
commit
eae19e1363
3 gewijzigde bestanden met toevoegingen van 40 en 31 verwijderingen
  1. 0 1
      app/App_FrontController.php
  2. 1 0
      app/models/RSSConfiguration.php
  3. 39 30
      app/models/RSSThemes.php

+ 0 - 1
app/App_FrontController.php

@@ -11,7 +11,6 @@ class App_FrontController extends FrontController {
 		$this->loadModels ();
 
 		Session::init ();
-		RSSThemes::init ();
 		Translate::init ();
 
 		$this->loadParamsView ();

+ 1 - 0
app/models/RSSConfiguration.php

@@ -49,6 +49,7 @@ class RSSConfiguration extends Model {
 		$this->_markWhen ($confDAO->mark_when);
 		$this->_urlShaarli ($confDAO->url_shaarli);
 		$this->_theme ($confDAO->theme);
+		RSSThemes::setThemeId ($confDAO->theme);
 		$this->_anonAccess ($confDAO->anon_access);
 		$this->_token ($confDAO->token);
 		$this->_autoLoadMore ($confDAO->auto_load_more);

+ 39 - 30
app/models/RSSThemes.php

@@ -1,48 +1,50 @@
 <?php
 
 class RSSThemes extends Model {
-	private static $themes_dir = '/themes';
-
-	private static $list = array();
-
-	public static function init() {
-		$basedir = PUBLIC_PATH . self::$themes_dir;
+	private static $themesUrl = '/themes/';
+	private static $defaultIconsUrl = '/themes/icons/';
 
+	public static function get() {
 		$themes_list = array_diff(
-			scandir($basedir),
+			scandir(PUBLIC_PATH . self::$themesUrl),
 			array('..', '.')
 		);
 
+		$list = array();
 		foreach ($themes_list as $theme_dir) {
-			$json_filename = $basedir . '/' . $theme_dir . '/metadata.json';
-			if(file_exists($json_filename)) {
+			$theme = self::get_infos($theme_dir);
+			if ($theme) {
+				$list[$theme_dir] = $theme;
+			}
+		}
+		return $list;
+	}
+
+	public static function get_infos($theme_id) {
+		$theme_dir = PUBLIC_PATH . self::$themesUrl . $theme_id ;
+		if (is_dir($theme_dir)) {
+			$json_filename = $theme_dir . '/metadata.json';
+			if (file_exists($json_filename)) {
 				$content = file_get_contents($json_filename);
 				$res = json_decode($content, true);
-
-				if($res &&
-					isset($res['name']) &&
-					isset($res['author']) &&
-					isset($res['description']) &&
-					isset($res['version']) &&
-					isset($res['files']) && is_array($res['files'])) {
-					$theme = $res;
-					$theme['path'] = $theme_dir;
-					self::$list[$theme_dir] = $theme;
+				if ($res && isset($res['files']) && is_array($res['files'])) {
+					$res['path'] = $theme_id;
+					return $res;
 				}
 			}
 		}
+		return false;
 	}
 
-	public static function get() {
-		return self::$list;
-	}
-
-	public static function get_infos($theme_id) {
-		if (isset(self::$list[$theme_id])) {
-			return self::$list[$theme_id];
-		}
+	private static $themeIconsUrl;
+	private static $themeIcons;
 
-		return false;
+	public static function setThemeId($theme_id) {
+		self::$themeIconsUrl = self::$themesUrl . $theme_id . '/icons/';
+		self::$themeIcons = is_dir(PUBLIC_PATH . self::$themeIconsUrl) ? array_fill_keys(array_diff(
+			scandir(PUBLIC_PATH . self::$themeIconsUrl),
+			array('..', '.')
+		), 1) : array();
 	}
 
 	public static function icon($name) {
@@ -72,8 +74,15 @@ class RSSThemes extends Model {
 			'tag' => '⚐',
 			'up' => '△',
 		);
-		$alt = isset($alts[$name]) ? $alts[$name] : '?';
+		if (!isset($alts[$name])) {
+			return '';
+		}
+
+		$url = $name . '.svg';
+		$url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) :
+			(self::$defaultIconsUrl . $url);
+
 		return '<i class="icon i_' . $name . '">' . $alts[$name] . '</i>';
-		//return '<img class="icon" src="' . Url::display('/themes/icons/' . $name . '.svg') . '" alt="' . $alts[$name] . '" />';
+		//return '<img class="icon" src="' . Url::display($url) . '" alt="' . $alts[$name] . '" />';
 	}
 }