From 53432c6d5c590dd88aa3e74fd8ab143815d91c1a Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 25 Jan 2018 13:13:42 +0100 Subject: [PATCH] ImportModifiers: friendlier errors on PHP 7.x --- application/forms/ImportRowModifierForm.php | 9 ++++++-- .../Director/Objects/ImportRowModifier.php | 4 ++++ .../Web/Table/PropertymodifierTable.php | 23 +++++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/application/forms/ImportRowModifierForm.php b/application/forms/ImportRowModifierForm.php index f99619e6..2be07143 100644 --- a/application/forms/ImportRowModifierForm.php +++ b/application/forms/ImportRowModifierForm.php @@ -27,9 +27,14 @@ class ImportRowModifierForm extends DirectorObjectForm $this->addElement('select', 'property_name', array( 'label' => $this->translate('Property'), 'description' => $this->translate('This must be an import source column (property)'), - 'multiOptions' => $this->optionalEnum($this->enumSourceColumns()), 'required' => true, )); + try { + $sourceColumns = $this->enumSourceColumns(); + $this->getElement('property_name')->multiOptions = $this->optionalEnum($sourceColumns); + } catch (Exception $e) { + $this->getElement('property_name')->addError($e->getMessage()); + } $this->addElement('text', 'target_property', array( 'label' => $this->translate('Target property'), @@ -57,7 +62,7 @@ class ImportRowModifierForm extends DirectorObjectForm $error = $e->getMessage(); $mods = $this->optionalEnum(array()); } - + $this->addElement('select', 'provider_class', array( 'label' => $this->translate('Modifier'), 'required' => true, diff --git a/library/Director/Objects/ImportRowModifier.php b/library/Director/Objects/ImportRowModifier.php index 994bd750..95117c47 100644 --- a/library/Director/Objects/ImportRowModifier.php +++ b/library/Director/Objects/ImportRowModifier.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Objects; +use Icinga\Exception\ConfigurationError; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; use Icinga\Module\Director\Hook\PropertyModifierHook; use Icinga\Module\Director\Objects\Extension\PriorityColumn; @@ -37,6 +38,9 @@ class ImportRowModifier extends DbObjectWithSettings if ($this->hookInstance === null) { $class = $this->get('provider_class'); /** @var PropertyModifierHook $obj */ + if (! class_exists($class)) { + throw new ConfigurationError('Cannot instantiate Property modifier %s', $class); + } $obj = new $class; $obj->setSettings($this->getSettings()); $obj->setTargetProperty($this->get('target_property')); diff --git a/library/Director/Web/Table/PropertymodifierTable.php b/library/Director/Web/Table/PropertymodifierTable.php index f6a07a10..0dd124c3 100644 --- a/library/Director/Web/Table/PropertymodifierTable.php +++ b/library/Director/Web/Table/PropertymodifierTable.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Web\Table; +use Error; use Exception; use Icinga\Module\Director\Hook\ImportSourceHook; use Icinga\Module\Director\Objects\ImportSource; @@ -73,11 +74,9 @@ class PropertymodifierTable extends ZfQueryBasedTable $hook = new $class; $caption .= ': ' . $hook->getName(); } catch (Exception $e) { - $caption = [ - $caption, - ': ', - $this::tag('span', ['class' => 'error'], $e->getMessage()) - ]; + $caption = $this->createErrorCaption($caption, $e); + } catch (Error $e) { + $caption = $this->createErrorCaption($caption, $e); } } else { $caption .= ': ' . $row->description; @@ -94,6 +93,20 @@ class PropertymodifierTable extends ZfQueryBasedTable ); } + /** + * @param $caption + * @param Exception|Error $e + * @return array + */ + protected function createErrorCaption($caption, $e) + { + return [ + $caption, + ': ', + $this::tag('span', ['class' => 'error'], $e->getMessage()) + ]; + } + public function getColumnsToBeRendered() { return [