From 90c0633354ed81ef918e4d5bc4352fb0e5ac064b Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 22 Apr 2022 09:28:20 +0200 Subject: [PATCH] Use some renamed tratis & move from/to array methods to BaseDashboard --- .../Web/Dashboard/Common/BaseDashboard.php | 41 ++++++++++++++++--- .../Web/Dashboard/Common/DashboardManager.php | 8 ++-- library/Icinga/Web/Dashboard/Dashboard.php | 4 +- .../Icinga/Web/Dashboard/DashboardHome.php | 15 ++++--- library/Icinga/Web/Dashboard/Pane.php | 17 ++++---- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/library/Icinga/Web/Dashboard/Common/BaseDashboard.php b/library/Icinga/Web/Dashboard/Common/BaseDashboard.php index 60d03954f..7ba78fcf2 100644 --- a/library/Icinga/Web/Dashboard/Common/BaseDashboard.php +++ b/library/Icinga/Web/Dashboard/Common/BaseDashboard.php @@ -4,8 +4,6 @@ namespace Icinga\Web\Dashboard\Common; -use Icinga\Common\DataExtractor; - /** * Base class for all dashboard widget types * @@ -13,8 +11,6 @@ use Icinga\Common\DataExtractor; */ abstract class BaseDashboard implements DashboardEntry { - use DataExtractor; - /** * Not translatable name of this widget * @@ -69,7 +65,7 @@ abstract class BaseDashboard implements DashboardEntry $this->title = $name; if (! empty($properties)) { - $this->fromArray($properties); + $this->setProperties($properties); } } @@ -182,7 +178,7 @@ abstract class BaseDashboard implements DashboardEntry /** * Set the widget's description * - * @param string $description + * @param ?string $description * * @return $this */ @@ -217,6 +213,39 @@ abstract class BaseDashboard implements DashboardEntry return $this->order; } + /** + * Set properties from the given list (no matching setter) are ignored + * + * @param array $data + * + * @return $this + */ + public function setProperties(array $data): self + { + foreach ($data as $name => $value) { + $func = 'set' . ucfirst($name); + if (method_exists($this, $func)) { + $this->$func($value); + } + } + + return $this; + } + + /** + * Get this class's structure as array + * + * Stringifies the attrs or set to null if it doesn't have a value, when $stringify is true + * + * @param bool $stringify Whether, the attributes should be returned unmodified + * + * @return array + */ + public function toArray(bool $stringify = true): array + { + return []; + } + public function hasEntries() { } diff --git a/library/Icinga/Web/Dashboard/Common/DashboardManager.php b/library/Icinga/Web/Dashboard/Common/DashboardManager.php index ad00ba416..5e9063ed6 100644 --- a/library/Icinga/Web/Dashboard/Common/DashboardManager.php +++ b/library/Icinga/Web/Dashboard/Common/DashboardManager.php @@ -274,13 +274,13 @@ trait DashboardManager foreach ($mg->getLoadedModules() as $module) { foreach ($module->getDashboard() as $dashboard) { $pane = new Pane($dashboard->getName()); - $pane->fromArray($dashboard->getProperties()); + $pane->setProperties($dashboard->getProperties()); $priority = 0; foreach ($dashboard->getDashlets() as $name => $configPart) { $uuid = self::getSHA1($module->getName() . $pane->getName() . $name); $dashlet = new Dashlet($name, $configPart['url'], $pane); - $dashlet->fromArray($configPart); + $dashlet->setProperties($configPart); $dashlet ->setUuid($uuid) ->setModuleDashlet(true) @@ -305,7 +305,7 @@ trait DashboardManager foreach ($module->getDashlets() as $dashlet) { $identifier = self::getSHA1($module->getName() . $dashlet->getName()); $newDashlet = new Dashlet($dashlet->getName(), $dashlet->getUrl()); - $newDashlet->fromArray($dashlet->getProperties()); + $newDashlet->setProperties($dashlet->getProperties()); $newDashlet ->setUuid($identifier) ->setModule($module->getName()) @@ -382,7 +382,7 @@ trait DashboardManager $dashlet->setDescription(t($moduleDashlet->description)); } - $dashlet->fromArray([ + $dashlet->setProperties([ 'label' => t($moduleDashlet->label), 'priority' => $moduleDashlet->priority, 'uuid' => $moduleDashlet->id, diff --git a/library/Icinga/Web/Dashboard/Dashboard.php b/library/Icinga/Web/Dashboard/Dashboard.php index b13016ac9..6e87ceec4 100644 --- a/library/Icinga/Web/Dashboard/Dashboard.php +++ b/library/Icinga/Web/Dashboard/Dashboard.php @@ -6,7 +6,7 @@ namespace Icinga\Web\Dashboard; use Icinga\Exception\ConfigurationError; use Icinga\Exception\ProgrammingError; -use Icinga\Web\Dashboard\Common\DashboardControls; +use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\DashboardEntry; use Icinga\Web\Dashboard\Common\DashboardManager; use ipl\Html\BaseHtmlElement; @@ -27,7 +27,7 @@ use ipl\Web\Widget\Tabs; class Dashboard extends BaseHtmlElement implements DashboardEntry { use DashboardManager; - use DashboardControls; + use DashboardEntries; /** * Base path of our new dashboards controller diff --git a/library/Icinga/Web/Dashboard/DashboardHome.php b/library/Icinga/Web/Dashboard/DashboardHome.php index 2f62721fc..aeda4971d 100644 --- a/library/Icinga/Web/Dashboard/DashboardHome.php +++ b/library/Icinga/Web/Dashboard/DashboardHome.php @@ -7,7 +7,7 @@ namespace Icinga\Web\Dashboard; use Icinga\Exception\ProgrammingError; use Icinga\Model\Home; use Icinga\Web\Dashboard\Common\BaseDashboard; -use Icinga\Web\Dashboard\Common\DashboardControls; +use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\Sortable; use ipl\Stdlib\Filter; @@ -15,7 +15,7 @@ use function ipl\Stdlib\get_php_type; class DashboardHome extends BaseDashboard implements Sortable { - use DashboardControls; + use DashboardEntries; /** * Name of the default home @@ -178,12 +178,11 @@ class DashboardHome extends BaseDashboard implements Sortable foreach ($panes as $pane) { $newPane = new Pane($pane->name); - $newPane->fromArray([ - 'uuid' => $pane->id, - 'title' => $pane->label, - 'priority' => $pane->priority, - 'home' => $this - ]); + $newPane + ->setHome($this) + ->setUuid($pane->id) + ->setTitle($pane->label) + ->setPriority($pane->priority); $newPane->loadDashboardEntries($name); $this->addEntry($newPane); diff --git a/library/Icinga/Web/Dashboard/Pane.php b/library/Icinga/Web/Dashboard/Pane.php index 8fd28bf92..c2498bea5 100644 --- a/library/Icinga/Web/Dashboard/Pane.php +++ b/library/Icinga/Web/Dashboard/Pane.php @@ -8,7 +8,7 @@ use Icinga\Web\Dashboard\Common\BaseDashboard; use Icinga\Exception\ProgrammingError; use Icinga\Exception\ConfigurationError; use Icinga\Model; -use Icinga\Web\Dashboard\Common\DashboardControls; +use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\Sortable; use ipl\Stdlib\Filter; use ipl\Web\Url; @@ -20,7 +20,7 @@ use function ipl\Stdlib\get_php_type; */ class Pane extends BaseDashboard implements Sortable { - use DashboardControls; + use DashboardEntries; const TABLE = 'icingaweb_dashboard'; @@ -98,13 +98,12 @@ class Pane extends BaseDashboard implements Sortable $this->setEntries([]); foreach ($dashlets as $dashlet) { $newDashlet = new Dashlet($dashlet->name, $dashlet->url, $this); - $newDashlet->fromArray([ - 'uuid' => $dashlet->id, - 'title' => $dashlet->label, - 'priority' => $dashlet->priority, - 'pane' => $this, - 'description' => $dashlet->icingaweb_module_dashlet->description - ]); + $newDashlet + ->setPane($this) + ->setUuid($dashlet->id) + ->setTitle($dashlet->label) + ->setPriority($dashlet->priority) + ->setDescription($dashlet->icingaweb_module_dashlet->description); $this->addEntry($newDashlet); }