84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Catalyst\MenuBundle\DependencyInjection;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
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(
|
|
$container,
|
|
new FileLocator(__DIR__ . '/../Resources/config')
|
|
);
|
|
$loader->load('services.yaml');
|
|
|
|
// NOTE: cannot use processConfiguration here since having the key as an identifier
|
|
// will break across multiple configuration files.
|
|
// Issue can be found here: https://github.com/symfony/symfony/issues/29817
|
|
|
|
$data = $this->processConfigs($configs);
|
|
|
|
// set acl data for main acl generator
|
|
// $def = $container->getDefinition('catalyst_menu.generator');
|
|
$def = $container->getDefinition('Catalyst\\MenuBundle\\Service\\Generator');
|
|
// $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;
|
|
}
|
|
}
|