From 932a40a025b79e9d90e7db318894a6d3dcc19480 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 28 Feb 2015 12:12:22 +1100 Subject: [PATCH] Streamline the process of filling a widget with its config options --- modules/backend/classes/WidgetBase.php | 20 +++++++++ modules/backend/widgets/ReportContainer.php | 47 +++++++++------------ 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/modules/backend/classes/WidgetBase.php b/modules/backend/classes/WidgetBase.php index 4e64d9a8a..66367f477 100644 --- a/modules/backend/classes/WidgetBase.php +++ b/modules/backend/classes/WidgetBase.php @@ -80,6 +80,26 @@ abstract class WidgetBase } } + /** + * Transfers config values stored inside the $config property directly + * on to the root object properties. If no properties are defined + * all config will be transferred if finds a match. + * @param array $properties + * @return void + */ + protected function fillFromConfig($properties = null) + { + if ($properties === null) { + $properties = array_keys((array) $this->config); + } + + foreach ($properties as $property) { + if (property_exists($this, $property)) { + $this->{$property} = $this->getConfig($property, $this->{$property}); + } + } + } + /** * Initialize the widget, called by the constructor and free from its parameters. * @return void diff --git a/modules/backend/widgets/ReportContainer.php b/modules/backend/widgets/ReportContainer.php index a4df95d64..5010e45d8 100644 --- a/modules/backend/widgets/ReportContainer.php +++ b/modules/backend/widgets/ReportContainer.php @@ -40,13 +40,14 @@ class ReportContainer extends WidgetBase * @var array A list of default widgets to load. * This structure could be defined in the widget configuration file (for example config_report_container.yaml). * Example YAML structure: + * * defaultWidgets: - * trafficOverview: - * class: RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview - * sortOrder: 1 - * configuration: - * title: 'Traffic overview' - * ocWidgetWidth: 10 + * trafficOverview: + * class: RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview + * sortOrder: 1 + * configuration: + * title: 'Traffic overview' + * ocWidgetWidth: 10 */ public $defaultWidgets = []; @@ -74,9 +75,19 @@ class ReportContainer extends WidgetBase */ public function __construct($controller) { - parent::__construct($controller, []); + $configFile = 'config_' . snake_case($this->alias) . '.yaml'; + $path = $controller->getConfigPath($configFile); + if (File::isFile($path)) { + $config = $this->makeConfig($configFile); + } + else { + $config = []; + } + + parent::__construct($controller, $config); + $this->bindToController(); - $this->applyConfigToSelf(); + $this->fillFromConfig(); } /** @@ -90,26 +101,6 @@ class ReportContainer extends WidgetBase parent::bindToController(); } - /** - * Most widgets store their configuration inside the $config class - * property, this container stores it against the root object. - */ - protected function applyConfigToSelf() - { - $configFile = 'config_' . snake_case($this->alias) . '.yaml'; - - $path = $this->controller->getConfigPath($configFile); - if (!File::isFile($path)) return; - - $config = $this->makeConfig($configFile); - - foreach ($config as $field => $value) { - if (property_exists($this, $field)) { - $this->$field = $value; - } - } - } - /** * Renders this widget along with its collection of report widgets. */