diff --git a/DependencyInjection/CatalystMenuExtension.php b/DependencyInjection/CatalystMenuExtension.php index 31f0736..9cd31ec 100644 --- a/DependencyInjection/CatalystMenuExtension.php +++ b/DependencyInjection/CatalystMenuExtension.php @@ -7,8 +7,14 @@ use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; +use Catalyst\MenuBundle\Menu\Collection; +use Catalyst\MenuBundle\Menu\Item; + + class CatalystMenuExtension extends Extension { + const ORDER_DEFAULT = 100; + public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader( @@ -21,10 +27,57 @@ class CatalystMenuExtension extends Extension // will break across multiple configuration files. // Issue can be found here: https://github.com/symfony/symfony/issues/29817 - // $data = $this->processConfigs($configs); + $data = $this->processConfigs($configs); // set acl data for main acl generator $def = $container->getDefinition('catalyst_menu.generator'); - $def->replaceArgument('$menu_data', $configs); + // $def->replaceArgument('$menu_data', $configs); + $def->replaceArgument('$menu_data', $data); + } + + protected function processConfigs($data) + { + $pdata = []; + + // error_log(print_r($data, true)); + + // manual array merge + // 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] = $group_data; + else + { + // append to group data + foreach ($group_data as $menu_data) + $pdata[$group][] = $menu_data; + } + } + } + + // sort group data according to display priority + foreach ($pdata as $group => $group_data) + { + usort($pdata[$group], function($a, $b) { + if (isset($a['order'])) + $a_order = $a['order']; + else + $a_order = self::ORDER_DEFAULT; + + if (isset($b['order'])) + $b_order = $b['order']; + else + $b_order = self::ORDER_DEFAULT; + + return $a_order - $b_order; + }); + } + + return $pdata; } } diff --git a/Service/Generator.php b/Service/Generator.php index 72a1271..2f098c5 100644 --- a/Service/Generator.php +++ b/Service/Generator.php @@ -40,14 +40,21 @@ class Generator { $pdata = []; - error_log(print_r($data, true)); + // TODO: cache this + // 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) + foreach ($data as $group => $group_data) { + // store orphans here in case their parents have not yet been defined + $orphans = []; + // initialize group data if (!isset($pdata[$group])) { @@ -73,12 +80,32 @@ class Generator if (isset($mi_data['acl'])) $mi->setACLKey($mi_data['acl']); + // check for orphans + if (isset($orphans[$mi_data['id']])) + { + error_log('orphan for - ' . $mi_data['id']); + foreach ($orphans[$mi_data['id']] as $orphan) + { + $mi->addChild($orphan); + } + } + // check parent if (isset($mi_data['parent']) && $mi_data['parent'] != null) { $parent = $index->get($mi_data['parent']); + + // if parent has not been set, add to orphans if ($parent == null) + { + if (!isset($orphans[$mi_data['parent']])) + $orphans[$mi_data['parent']] = []; + + error_log('found orphan - ' . $mi_data['parent']); + + $orphans[$mi_data['parent']][] = $mi; continue; + } $parent->addChild($mi); } @@ -86,9 +113,11 @@ class Generator $menu->add($mi); } } + /* } + */ - error_log(print_r($pdata, true)); + // error_log(print_r($pdata, true)); return $pdata; }