From e639924c9bf623e44bce58d4ebc4cb97449d564b Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Mon, 3 Dec 2018 17:47:01 +0100 Subject: [PATCH] Firewall/Alias, performance regression in get_alias_description, take 1: move to our static firewall class --- src/etc/inc/legacy_bindings.inc | 17 +----------- .../app/library/OPNsense/Firewall/Util.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/etc/inc/legacy_bindings.inc b/src/etc/inc/legacy_bindings.inc index cd734bc54..cfe277186 100644 --- a/src/etc/inc/legacy_bindings.inc +++ b/src/etc/inc/legacy_bindings.inc @@ -144,20 +144,5 @@ function legacy_move_config_list_items($source, $id, $items) { */ function get_alias_description($name) { - // MVC defined aliases - foreach ((new \OPNsense\Firewall\Alias())->aliasIterator() as $alias) { - if ($alias['name'] == $name) { - if (!empty($alias['descr'])) { - return $alias['descr']; - } elseif (!empty($alias['description'])) { - return $alias['description']; - } elseif (!empty($alias['content'])) { - $tmp = array_slice(explode("\n", $alias['content']), 0, 10); - asort($tmp); - return implode("
", $tmp); - } - } - } - - return null; + return OPNsense\Firewall\Util::aliasDescription($name); } diff --git a/src/opnsense/mvc/app/library/OPNsense/Firewall/Util.php b/src/opnsense/mvc/app/library/OPNsense/Firewall/Util.php index 36f6d85c0..d0551a781 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Firewall/Util.php +++ b/src/opnsense/mvc/app/library/OPNsense/Firewall/Util.php @@ -113,6 +113,33 @@ class Util return false; } + /** + * return alias descriptions + * @param string $name name + * @return string + */ + public static function aliasDescription($name) + { + if (self::$aliasObject == null) { + // Cache the alias object to avoid object creation overhead. + self::$aliasObject = new Alias(); + } + foreach (self::$aliasObject->aliasIterator() as $alias) { + if ($alias['name'] == $name) { + if (!empty($alias['descr'])) { + return $alias['descr']; + } elseif (!empty($alias['description'])) { + return $alias['description']; + } elseif (!empty($alias['content'])) { + $tmp = array_slice(explode("\n", $alias['content']), 0, 10); + asort($tmp); + return implode("
", $tmp); + } + } + } + return null; + } + /** * Fetch port alias contents, other alias types are handled using tables so there usually no need * to know the contents within any of the scripts.