<?php
/**
* Created by PhpStorm.
* User: eph
* Date: 30/06/17
* Time: 10:10
*/
namespace Oi\MenuBundle\Profiler;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class MenuDataCollector extends DataCollector
{
private $menu = [];
private $profile = [];
private $header = [];
private $footer = [];
private $roles = [];
private $selected = [];
private $treeSelected = false;
/**
* MenuDataCollector constructor.
* @param array $menu
*/
public function __construct(array $menu, array $profile, array $header, array $footer)
{
$this->menu = $menu;
$this->profile = $profile;
$this->header = $header;
$this->footer = $footer;
}
private function menuSort(&$menu, $lvl = 0)
{
foreach ($menu as $k => $voice) {
if (isset($menu[$k]['submenu'])) {
$this->menuSort($menu[$k]['submenu'], $lvl + 1);
if (count($this->selected) > 0 && $this->treeSelected) {
array_unshift($this->selected, $menu[$k]);
}
if ($lvl == 0) {
$this->treeSelected = false;
}
}
if (!is_numeric($k)) {
$menu[$k]['key'] = $k;
}
if (isset($menu[$k]['route'])) {
if ($menu[$k]['route'] == $this->data['route']) {
$this->treeSelected = true;
$this->selected[] = $menu[$k];
}
}
}
usort($menu, function ($a, $b) {
if (!isset($a['order'])) {
$a['order'] = 1000;
}
if (!isset($b['order'])) {
$b['order'] = 1000;
}
return $a['order'] > $b['order'];
});
}
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$this->data = [];
$this->data['route'] = $request->get('_route');
$this->menuSort($this->menu);
$this->data['menu'] = $this->menu;
$this->menuSort($this->profile);
$this->data['profile'] = $this->profile;
$this->menuSort($this->header);
$this->data['header'] = $this->header;
$this->menuSort($this->footer);
$this->data['footer'] = $this->footer;
$this->data['selected'] = $this->selected;
}
public function getMenu()
{
return $this->data['menu'];
}
public function getProfile()
{
return $this->data['profile'];
}
public function getHeader()
{
return $this->data['header'];
}
public function getFooter()
{
return $this->data['footer'];
}
public function getRoute()
{
return $this->data['route'];
}
public function getSelected()
{
return $this->data['selected'];
}
public function getName()
{
return 'oi_menu';
}
public function reset() {
}
}