index = new Collection(); $this->menu = new Collection(); $this->router = $router; $this->menu_data = $this->processConfigs($menu_data); } public function getMenu($menu_key) { if (!isset($this->menu_data[$menu_key])) return null; return $this->menu_data[$menu_key]; } protected function processConfigs($data) { $pdata = []; error_log(print_r($data, true)); // first layer contains all the instances in config foreach ($data as $instance_data) { // 2nd layer are the groups and the parents foreach ($instance_data as $group => $group_data) { // initialize group data if (!isset($pdata[$group])) { $pdata[$group] = [ 'menu' => new Collection(), 'index' => new Collection(), ]; } $index = $pdata[$group]['index']; $menu = $pdata[$group]['menu']; foreach ($group_data as $mi_data) { // check params if (!isset($mi_data['icon'])) $mi_data['icon'] = null; // instantiate $mi = $this->newItem($index, $mi_data['id'], $mi_data['label'], $mi_data['icon']); // acl if (isset($mi_data['acl'])) $mi->setACLKey($mi_data['acl']); // check parent if (isset($mi_data['parent']) && $mi_data['parent'] != null) { $parent = $index->get($mi_data['parent']); if ($parent == null) continue; $parent->addChild($mi); } else $menu->add($mi); } } } error_log(print_r($pdata, true)); return $pdata; } protected function newItem($index, $id, $label, $icon = null) { $mi = new Item(); $mi->setID($id) ->setLabel($label); // TODO: have a way to set manual links or specify route parameters try { $mi->setLink($this->router->generate($id)); } catch (RouteNotFoundException $e) { // no route, set to # $mi->setLink('javascript:;'); } if ($icon != null) $mi->setIcon($icon); $index->add($mi); return $mi; } }