MenuItem renamed, small fixes

* One less namespace, better naming
* hasItem() added
* throws Exception when trying to access invalid Items
* Automagically creates missing parents when adding named childs
This commit is contained in:
Thomas Gelf 2014-02-03 14:16:17 +00:00
parent 56abc53a2b
commit 3d636423c8
2 changed files with 26 additions and 16 deletions

View File

@ -31,7 +31,7 @@ namespace Icinga\Web;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Web\Menu\Item as MenuItem;
use Icinga\Web\MenuItem;
class Menu extends MenuItem
{

View File

@ -2,9 +2,11 @@
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
namespace Icinga\Web;
class Item
use Icinga\Exception\ProgrammingError;
class MenuItem
{
/**
* Item id
@ -245,17 +247,12 @@ class Item
} else {
// Submenu item
list($parentId, $id) = explode('.', $id, 2);
$parent = $this->getChild($parentId);
if ($parent !== null) {
$menuItem = $parent->addChild($id, $itemConfig);
if ($this->hasChild($parentId)) {
$parent = $this->getChild($parentId);
} else {
// Parent does not exist, auto-generate a knot to inform the user
$autoId = 'Auto-generated knot because submenu items refer to a non-existent parent menu item';
if (($auto = $this->getChild($autoId)) === null) {
$auto = $this->addChild($autoId);
}
$menuItem = $auto->addChild($id, $itemConfig);
$parent = $this->addChild($parentId);
}
$menuItem = $parent->addChild($id, $itemConfig);
}
return $menuItem;
}
@ -282,20 +279,33 @@ class Item
return $this->children;
}
/**
* Whether a given child id exists
*
* @param string $id
*
* @return self|$default
*/
public function hasChild($id)
{
return array_key_exists($id, $this->children);
}
/**
* Get child by its id
*
* @param string $id
* @param mixed $default
*
* @return self|$default
* @return MenuItem
* @throws ProgrammingError
*/
public function getChild($id, $default = null)
public function getChild($id)
{
if (array_key_exists($id, $this->children)) {
if ($this->hasChild($id)) {
return $this->children[$id];
}
return $default;
throw new ProgrammingError(sprintf('Trying to get invalid Menu child "%s"', $id));
}