Skip to content

Commit

Permalink
Navigation: Add method fromConfig()
Browse files Browse the repository at this point in the history
refs #5600
  • Loading branch information
Johannes Meyer committed Sep 2, 2015
1 parent 997b578 commit 83974b7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions library/Icinga/Web/Navigation/Navigation.php
Expand Up @@ -9,9 +9,11 @@
use InvalidArgumentException;
use IteratorAggregate;
use Icinga\Authentication\Auth;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Application\Logger;
use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\String;

Expand Down Expand Up @@ -321,6 +323,40 @@ public function getRenderer()
return new RecursiveNavigationRenderer($this);
}

/**
* Create and return a new set of navigation items for the given configuration
*
* @param Config $config
*
* @return Navigation
*
* @throws ConfigurationError In case a referenced parent does not exist
*/
public static function fromConfig(Config $config)
{
$flattened = $topLevel = array();
foreach ($config as $sectionName => $sectionConfig) {
if (! $sectionConfig->parent) {
$topLevel[$sectionName] = $sectionConfig->toArray();
$flattened[$sectionName] = & $topLevel[$sectionName];
} elseif (isset($flattened[$sectionConfig->parent])) {
$flattened[$sectionConfig->parent]['children'][$sectionName] = $sectionConfig->toArray();
$flattened[$sectionName] = & $flattened[$sectionConfig->parent]['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,
$sectionConfig->parent
);
}
}

return static::fromArray($topLevel);
}

/**
* Create and return a new set of navigation items for the given array
*
Expand Down

0 comments on commit 83974b7

Please sign in to comment.