Skip to content

Commit

Permalink
Navigation: Only fail if there's really no chance to create the reque…
Browse files Browse the repository at this point in the history
…sted hierarchy

refs #5600
  • Loading branch information
Johannes Meyer committed Sep 17, 2015
1 parent 7b5d414 commit d4a9198
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions library/Icinga/Web/Navigation/Navigation.php
Expand Up @@ -470,7 +470,7 @@ public static function fromConfig($config)
throw new InvalidArgumentException('Argument $config must be an array or a instance of Traversable');
}

$flattened = $topLevel = array();
$flattened = $orphans = $topLevel = array();
foreach ($config as $sectionName => $sectionConfig) {
$parentName = $sectionConfig->parent;
unset($sectionConfig->parent);
Expand All @@ -482,17 +482,40 @@ public static function fromConfig($config)
$flattened[$parentName]['children'][$sectionName] = $sectionConfig->toArray();
$flattened[$sectionName] = & $flattened[$parentName]['children'][$sectionName];
} else {
throw new ConfigurationError(
t(
'Failed to add navigation item "%s". Parent "%s" not found. Make'
. ' sure that the parent is defined prior to its child(s).'
),
$sectionName,
$parentName
);
$orphans[$parentName][$sectionName] = $sectionConfig->toArray();
$flattened[$sectionName] = & $orphans[$parentName][$sectionName];
}
}

do {
$match = false;
foreach ($orphans as $parentName => $children) {
if (isset($flattened[$parentName])) {
if (isset($flattened[$parentName]['children'])) {
$flattened[$parentName]['children'] = array_merge(
$flattened[$parentName]['children'],
$children
);
} else {
$flattened[$parentName]['children'] = $children;
}

unset($orphans[$parentName]);
$match = true;
}
}
} while ($match && !empty($orphans));

if (! empty($orphans)) {
throw new ConfigurationError(
t(
'Failed to fully parse navigation configuration. Ensure that'
. ' all referenced parents are existing navigation items: %s'
),
join(', ', array_keys($orphans))
);
}

return static::fromArray($topLevel);
}

Expand Down

0 comments on commit d4a9198

Please sign in to comment.