120 lines
3.2 KiB
PHP
120 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Catalyst\MenuBundle\Service;
|
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
|
use Symfony\Component\Config\ConfigCache;
|
|
use Symfony\Component\Config\Resource\FileResource;
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
|
|
use Catalyst\MenuBundle\Menu\Collection;
|
|
use Catalyst\MenuBundle\Menu\Item;
|
|
|
|
|
|
class Generator
|
|
{
|
|
protected $index;
|
|
protected $menu;
|
|
|
|
protected $router;
|
|
protected $menu_data;
|
|
|
|
public function __construct(RouterInterface $router, $menu_data)
|
|
{
|
|
$this->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;
|
|
}
|
|
}
|