From f426077b01441304b147d6dc983df3b3645388a6 Mon Sep 17 00:00:00 2001
From: Mike Shiyan <dev@mike.pp.ua>
Date: Fri, 13 Nov 2015 13:15:53 +0200
Subject: [PATCH] Issue #2604120 by pingwin4eg: Cache statically result of
 styleswitcher_theme_styles()

---
 styleswitcher.module | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/styleswitcher.module b/styleswitcher.module
index 095fe7a..3e43e28 100644
--- a/styleswitcher.module
+++ b/styleswitcher.module
@@ -500,13 +500,18 @@ function styleswitcher_custom_styles() {
  * @see styleswitcher_style_load()
  */
 function styleswitcher_theme_styles($theme, $original_theme = NULL) {
-  $styles = array();
+  static $styles = array();
 
   // Theme can be an empty string if custom style is loading.
   if (!$theme) {
-    return $styles;
+    return array();
+  }
+
+  if (isset($styles[$theme])) {
+    return $styles[$theme];
   }

+  $theme_styles = array();
   $theme_path = drupal_get_path('theme', $theme);
   // Do not use system_get_info() because it skips disabled base themes.
   $themes = list_themes();
@@ -518,7 +523,7 @@ function styleswitcher_theme_styles($theme, $original_theme = NULL) {
 
   // Search base themes for styleswitcher info.
   if (isset($theme_info['base theme'])) {
-    $styles = styleswitcher_theme_styles($theme_info['base theme'], $original_theme);
+    $theme_styles = styleswitcher_theme_styles($theme_info['base theme'], $original_theme);
   }
 
   if (!empty($theme_info['styleswitcher'])) {
@@ -528,7 +533,7 @@ function styleswitcher_theme_styles($theme, $original_theme = NULL) {
     if (!empty($info['css'])) {
       foreach ($info['css'] as $label => $path) {
         $name = 'theme/' . _styleswitcher_style_name($label);
-        $styles[$name] = array(
+        $theme_styles[$name] = array(
           'name' => $name,
           'label' => $label,
           'path' => menu_path_is_external($path) ? $path : $theme_path . '/' . $path,
@@ -542,14 +547,14 @@ function styleswitcher_theme_styles($theme, $original_theme = NULL) {
     }
     elseif (isset($info['css']['default'])) {
       $default = $info['css']['default'];
-      unset($styles['theme/default']);
+      unset($theme_styles['theme/default']);
     }
 
     if (isset($default)) {
       $default_in_existing = FALSE;
 
       // Check if Default points to one of existing alternatives.
-      foreach ($styles as $name => $style) {
+      foreach ($theme_styles as $name => $style) {
         if ($default == $style['label'] || $default == $style['path'] || $theme_path . '/' . $default == $style['path']) {
           styleswitcher_theme_default_style_key($original_theme, $name);
           $default_in_existing = TRUE;
@@ -565,13 +570,18 @@ function styleswitcher_theme_styles($theme, $original_theme = NULL) {
           'path' => menu_path_is_external($default) ? $default : $theme_path . '/' . $default,
         );
         // Place default style above others.
-        $styles = $defaults + $styles;
+        $theme_styles = $defaults + $theme_styles;
         styleswitcher_theme_default_style_key($original_theme, 'theme/default');
       }
     }
   }
 
-  return $styles;
+  // Do not inflate the memory.
+  if ($theme == $original_theme) {
+    $styles[$theme] = $theme_styles;
+  }
+
+  return $theme_styles;
 }
 
 /**
-- 
1.9.4.msysgit.1

