From 9905a3f6bdca736f27f354d7a7ac562396c3d63a Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Mon, 21 Mar 2016 17:08:17 +0000 Subject: [PATCH] Cache widget registry for an hour. Fixes historylog not loading entries. --- api/src/Etemplate.php | 36 -------------------- api/src/Etemplate/Widget.php | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/api/src/Etemplate.php b/api/src/Etemplate.php index 7ce188e040..4e86ffe73b 100644 --- a/api/src/Etemplate.php +++ b/api/src/Etemplate.php @@ -687,39 +687,3 @@ class Etemplate extends Etemplate\Widget\Template return (int)$size; } } - -// Try to discover all widgets, as names don't always match tags (eg: listbox is in menupopup) -foreach(scandir($dir=__DIR__ . '/Etemplate/Widget') as $filename) -{ - if(substr($filename, -4) == '.php') - { - try - { - include_once($dir.'/'.$filename); - } - catch(Exception $e) - { - error_log($e->getMessage()); - } - } -} - -// Use hook to load custom widgets from other apps -$widgets = $GLOBALS['egw']->hooks->process('etemplate2_register_widgets'); -foreach($widgets as $app => $list) -{ - if (is_array($list)) - { - foreach($list as $class) - { - try - { - class_exists($class); // trigger autoloader - } - catch(Exception $e) - { - error_log($e->getMessage()); - } - } - } -} diff --git a/api/src/Etemplate/Widget.php b/api/src/Etemplate/Widget.php index 0df277aa91..4ee8df8486 100644 --- a/api/src/Etemplate/Widget.php +++ b/api/src/Etemplate/Widget.php @@ -14,6 +14,7 @@ namespace EGroupware\Api\Etemplate; use EGroupware\Api; +use EGroupware\Api\Cache; use XMLReader; use ReflectionMethod; @@ -287,6 +288,68 @@ class Widget self::$widget_registry[$widget] = $class; } } + + /** + * Try to discover all widgets, as names don't always match tags (eg: + * listbox is in menupopup) + * + * Look through filesystem for widgets, then process the hook + * 'etemplate2_register_widgets', which may return a list of widget class + * names. + * + * The list is cached for an hour, to avoid rescanning the filesystem but + * also to make sure the list is always available, even when calling static + * functions of widgets. + */ + public static function scanForWidgets() + { + + $widget_registry = Cache::getInstance('etemplate', 'widget_registry'); + + if (!$widget_registry) // not in instance cache --> rescan from filesystem + { + foreach(scandir($dir=__DIR__ . '/Widget') as $filename) + { + if(substr($filename, -4) == '.php') + { + try + { + include_once($dir.'/'.$filename); + } + catch(Exception $e) + { + error_log($e->getMessage()); + } + } + } + + // Use hook to load custom widgets from other apps + $widgets = $GLOBALS['egw']->hooks->process('etemplate2_register_widgets',array(),true); + foreach($widgets as $app => $list) + { + if (is_array($list)) + { + foreach($list as $class) + { + try + { + class_exists($class); // trigger autoloader + } + catch(Exception $e) + { + error_log($e->getMessage()); + } + } + } + } + Cache::setInstance('etemplate', 'widget_registry', self::$widget_registry, 3600); + } + else + { + self::$widget_registry = $widget_registry; + } + return self::$widget_registry; + } /** * Factory method to construct all widgets @@ -913,3 +976,6 @@ class Widget return self::setElementAttribute($name, 'disabled', $disabled); } } + +// Scan for widget classes and cache for 1 hour +Widget::scanForWidgets(); \ No newline at end of file