<commit_before><?php
namespace App\Event\View;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Network\Exception\ForbiddenException;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Menu\MenuBuilder\Menu;
use Menu\MenuBuilder\MenuItemFactory;
use RolesCapabilities\CapabilityTrait;

abstract class BaseMenuListener implements EventListenerInterface
{
    use CapabilityTrait;

    /**
     * Method that does acl check on flat (single level) menu items.
     *
     * @param  \Cake\Event\Event $event Event object
     * @param  array             $menu  Menu
     * @param  array             $user  User
     * @param  string            $type  menu type
     * @return void
     */
    public function beforeRenderFlatMenu(Event $event, array $menu, array $user, $type = 'buttons')
    {
        if (empty($menu)) {
            return;
        }

        $menuBuilder = new Menu();
        foreach ($menu as $item) {
            if (!empty($user) && !$this->_checkAccess($item['url'], $user)) {
                continue;
            }

            $menuItem = MenuItemFactory::createMenuItem($item);
            $menuBuilder->addMenuItem($menuItem);
        }

        if ($type == 'actions') {
            $renderClass = 'Menu\\MenuBuilder\\MenuActionsRender';
        } else {
            $renderClass = 'Menu\\MenuBuilder\\MenuButtonsRender';
        }

        $render = new $renderClass($menuBuilder, $event->subject());
        $event->result = $render->render();

        return;

        // if empty user try to get it from the SESSION
        if (empty($user) && isset($_SESSION)) {
            $user = Hash::get($_SESSION, 'Auth.user');
            // if user still empty add all menu items to Event result and return
            if (empty($user)) {
                foreach ($menu as $item) {
                    $event->result .= $item['html'] . ' ';
                }

                return;
            }
        }

        foreach ($menu as $item) {
            // this is for label like menu items without a url
            if (empty($item['url'])) {
                $event->result .= $item['html'] . ' ';
                continue;
            }

            try {
                if (!$this->_checkAccess($item['url'], $user)) {
                    continue;
                }
                $event->result .= $item['html'] . ' ';
            } catch (ForbiddenException $e) {
                // do nothing
            }
        }
    }
}<commit_msg>Improved a way to build render class name<commit_after><?php
namespace App\Event\View;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Core\Exception\Exception;
use Cake\Network\Exception\ForbiddenException;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Menu\MenuBuilder\Menu;
use Menu\MenuBuilder\MenuItemFactory;
use RolesCapabilities\CapabilityTrait;

abstract class BaseMenuListener implements EventListenerInterface
{
    use CapabilityTrait;

    /**
     * Method that does acl check on flat (single level) menu items.
     *
     * @param  \Cake\Event\Event $event Event object
     * @param  array             $menu  Menu
     * @param  array             $user  User
     * @param  string            $type  menu type
     * @return void
     */
    public function beforeRenderFlatMenu(Event $event, array $menu, array $user, $type = Menu::MENU_BUTTONS_TYPE)
    {
        if (empty($menu)) {
            return;
        }
        $menuBuilder = new Menu();
        foreach ($menu as $item) {
            if (!empty($user) && !$this->_checkAccess($item['url'], $user)) {
                continue;
            }

            $menuItem = MenuItemFactory::createMenuItem($item);
            $menuBuilder->addMenuItem($menuItem);
        }

        $renderClass = 'Menu\\MenuBuilder\\Menu' . ucfirst($type)  . 'Render';
        
        if (!class_exists($renderClass)) {
            throw new Exception('Menu render class [' . $renderClass . '] is not found!');
        }

        $render = new $renderClass($menuBuilder, $event->subject());
        $event->result = $render->render();
    }
}
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.0, 'mean_word_length': 1.926293408929837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5090452', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1115, 'n_words': 284}