vendor/oi/menu/src/Profiler/MenuDataCollector.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: eph
  5.  * Date: 30/06/17
  6.  * Time: 10:10
  7.  */
  8. namespace Oi\MenuBundle\Profiler;
  9. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. class MenuDataCollector extends DataCollector
  14. {
  15.     private $menu = [];
  16.     private $profile = [];
  17.     private $header = [];
  18.     private $footer = [];
  19.     private $roles = [];
  20.     private $selected = [];
  21.     private $treeSelected false;
  22.     /**
  23.      * MenuDataCollector constructor.
  24.      * @param array $menu
  25.      */
  26.     public function __construct(array $menu, array $profile, array $header, array $footer)
  27.     {
  28.         $this->menu $menu;
  29.         $this->profile $profile;
  30.         $this->header $header;
  31.         $this->footer $footer;
  32.     }
  33.     private function menuSort(&$menu$lvl 0)
  34.     {
  35.         foreach ($menu as $k => $voice) {
  36.             if (isset($menu[$k]['submenu'])) {
  37.                 $this->menuSort($menu[$k]['submenu'], $lvl 1);
  38.                 if (count($this->selected) > && $this->treeSelected) {
  39.                     array_unshift($this->selected$menu[$k]);
  40.                 }
  41.                 if ($lvl == 0) {
  42.                     $this->treeSelected false;
  43.                 }
  44.             }
  45.             if (!is_numeric($k)) {
  46.                 $menu[$k]['key'] = $k;
  47.             }
  48.             if (isset($menu[$k]['route'])) {
  49.                 if ($menu[$k]['route'] == $this->data['route']) {
  50.                     $this->treeSelected true;
  51.                     $this->selected[] = $menu[$k];
  52.                 }
  53.             }
  54.         }
  55.         usort($menu, function ($a$b) {
  56.             if (!isset($a['order'])) {
  57.                 $a['order'] = 1000;
  58.             }
  59.             if (!isset($b['order'])) {
  60.                 $b['order'] = 1000;
  61.             }
  62.             return $a['order'] > $b['order'];
  63.         });
  64.     }
  65.     public function collect(Request $requestResponse $response\Throwable $exception null)
  66.     {
  67.         $this->data = [];
  68.         $this->data['route'] = $request->get('_route');
  69.         $this->menuSort($this->menu);
  70.         $this->data['menu'] = $this->menu;
  71.         $this->menuSort($this->profile);
  72.         $this->data['profile'] = $this->profile;
  73.         $this->menuSort($this->header);
  74.         $this->data['header'] = $this->header;
  75.         $this->menuSort($this->footer);
  76.         $this->data['footer'] = $this->footer;
  77.         $this->data['selected'] = $this->selected;
  78.     }
  79.     public function getMenu()
  80.     {
  81.         return $this->data['menu'];
  82.     }
  83.     public function getProfile()
  84.     {
  85.         return $this->data['profile'];
  86.     }
  87.     public function getHeader()
  88.     {
  89.         return $this->data['header'];
  90.     }
  91.     public function getFooter()
  92.     {
  93.         return $this->data['footer'];
  94.     }
  95.     public function getRoute()
  96.     {
  97.         return $this->data['route'];
  98.     }
  99.     public function getSelected()
  100.     {
  101.         return $this->data['selected'];
  102.     }
  103.     public function getName()
  104.     {
  105.         return 'oi_menu';
  106.     }
  107.     public function reset() {
  108.     }
  109. }