From de287abf24425287d638cb7425e74d65c1552297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Wed, 6 Apr 2016 19:06:03 +0200 Subject: [PATCH] OPTIMALIZATION: cache widget area content for one pageload --- app/Helpers/WidgetAreas.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/Helpers/WidgetAreas.php b/app/Helpers/WidgetAreas.php index cf52fe70..27ac6924 100644 --- a/app/Helpers/WidgetAreas.php +++ b/app/Helpers/WidgetAreas.php @@ -5,11 +5,28 @@ use App\Models\WidgetArea; class WidgetAreas { + /** + * @var array list of inicialized widget areas + */ + protected static $widgetAreas = []; + + /** + * Renderer for whole widget area. + * + * @param $uniqueCode + * + * @return mixed + */ public static function render($uniqueCode) { - $widgetArea = new WidgetArea(); + if (isset(self::$widgetAreas[$uniqueCode])) { + echo self::$widgetAreas[$uniqueCode]; + return; + } - $widgets = $widgetArea->getWidgets($uniqueCode)->get(); + $widgetArea = new WidgetArea(); + $widgets = $widgetArea->getWidgets($uniqueCode) + ->get(); $content = ''; foreach ($widgets as $widget) { @@ -19,6 +36,9 @@ class WidgetAreas $content .= call_user_func([$presenter, 'render'], json_decode($widget->settings)); } + // cache this widget area + self::$widgetAreas[$uniqueCode] = $content; + echo $content; } } \ No newline at end of file