From 7fb6cb007d0314e30472fe8824be36823e7b87e6 Mon Sep 17 00:00:00 2001
From: Mike Shiyan <dev@mike.pp.ua>
Date: Thu, 29 Oct 2015 13:09:16 +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 3cfec09..3437893 100644
--- a/styleswitcher.module
+++ b/styleswitcher.module
@@ -503,13 +503,18 @@ function styleswitcher_custom_styles() {
  * @see styleswitcher_style_load()
  */
 function styleswitcher_theme_styles($theme, $original_theme = NULL) {
-  $styles = array();
+  $styles = &drupal_static(__FUNCTION__, 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();
@@ -521,7 +526,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'])) {
@@ -531,7 +536,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' => $theme_path . '/' . $path,
@@ -545,14 +550,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'] || $theme_path . '/' . $default == $style['path']) {
           styleswitcher_theme_default_style_key($original_theme, $name);
           $default_in_existing = TRUE;
@@ -568,13 +573,18 @@ function styleswitcher_theme_styles($theme, $original_theme = NULL) {
           'path' => $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

