<commit_before><?php

namespace Kunstmaan\AdminBundle\Helper\Menu;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;

class SimpleMenuAdaptor implements MenuAdaptorInterface
{

    /**
     * @var SecurityContext
     */
    private $securityContext;

    /**
     * @var array
     */
    private $menuItems;

    public function __construct(SecurityContext $securityContext, array $menuItems)
    {
        $this->securityContext = $securityContext;
        $this->menuItems = $menuItems;
    }

    /**
     * In this method you can add children for a specific parent, but also remove and change the already created
     * children
     *
     * @param MenuBuilder $menu The MenuBuilder
     * @param MenuItem[] &$children The current children
     * @param MenuItem|null $parent The parent Menu item
     * @param Request $request The Request
     */
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
    {
        if (null === $parent) {
            return;
        }

        foreach ($this->menuItems as $item) {
            if ($item['parent'] != $parent->getRoute()) {
                continue;
            }

            if ($item['role'] && false === $this->securityContext->isGranted($item['role'])) {
                continue;
            }

            $menuItem = new TopMenuItem($menu);
            $menuItem
                ->setRoute($item['route'], $item['params'])
                ->setLabel($item['label'])
                ->setUniqueId($item['route'])
                ->setParent($parent);

            if ($request && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
                $menuItem->setActive(true);
                $parent->setActive(true);
            }

            $children[] = $menuItem;
        }
    }
}
<commit_msg>Allow `SimpleMenuAdaptor` to create items in main menu<commit_after><?php

namespace Kunstmaan\AdminBundle\Helper\Menu;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;

class SimpleMenuAdaptor implements MenuAdaptorInterface
{

    /**
     * @var SecurityContext
     */
    private $securityContext;

    /**
     * @var array
     */
    private $menuItems;

    public function __construct(SecurityContext $securityContext, array $menuItems)
    {
        $this->securityContext = $securityContext;
        $this->menuItems = $menuItems;
    }

    /**
     * In this method you can add children for a specific parent, but also remove and change the already created
     * children
     *
     * @param MenuBuilder $menu The MenuBuilder
     * @param MenuItem[] &$children The current children
     * @param MenuItem|null $parent The parent Menu item
     * @param Request $request The Request
     */
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
    {
        foreach ($this->menuItems as $item) {
            if (false === $this->parentMatches($parent, $item)) {
                continue;
            }

            if ($item['role'] && false === $this->securityContext->isGranted($item['role'])) {
                continue;
            }

            $menuItem = new TopMenuItem($menu);
            $menuItem
                ->setRoute($item['route'], $item['params'])
                ->setLabel($item['label'])
                ->setUniqueId($item['route'])
                ->setParent($parent);

            if ($request && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
                $menuItem->setActive(true);
                $parent->setActive(true);
            }

            $children[] = $menuItem;
        }
    }

    /**
     * @param MenuItem $parent
     * @param array    $item
     *
     * @return bool
     */
    private function parentMatches(MenuItem $parent = null, $item)
    {
        if (null === $parent) {
            return null === $item['parent'];
        }

        return $item['parent'] === $parent->getRoute();
    }
}

{'fraction_non_alphanumeric': 0.1310879573540102, 'fraction_numerical': 0.000484613520717228, 'mean_word_length': 2.1632183908045977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '826054', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1077, 'n_words': 265}