<commit_before><?php
namespace Czim\CmsCore\Menu;

use Czim\CmsCore\Contracts\Menu\MenuConfigInterpreterInterface;
use Czim\CmsCore\Contracts\Menu\MenuPermissionsFilterInterface;
use Czim\CmsCore\Contracts\Menu\MenuRepositoryInterface;
use Czim\CmsCore\Contracts\Support\Data\MenuLayoutDataInterface;
use Czim\CmsCore\Contracts\Support\Data\MenuPermissionsIndexDataInterface;
use Exception;
use Illuminate\Support\Collection;
use Czim\CmsCore\Contracts\Auth\AuthenticatorInterface;
use Czim\CmsCore\Contracts\Core\CoreInterface;
use Czim\CmsCore\Contracts\Modules\Data\MenuPresenceInterface;

class MenuRepository implements MenuRepositoryInterface
{
    /**
     * The key under which the layout is cached.
     *
     * @var string
     */
    const CACHE_KEY_LAYOUT = 'cms:menu-layout-data';

    /**
     * The key under which the permissions index is cached.
     *
     * @var string
     */
    const CACHE_KEY_INDEX = 'cms:menu-permissions-index';

    /**
     * @var CoreInterface
     */
    protected $core;

    /**
     * @var AuthenticatorInterface
     */
    protected $auth;

    /**
     * @var MenuConfigInterpreterInterface
     */
    protected $configInterpreter;

    /**
     * @var MenuPermissionsFilterInterface
     */
    protected $permissionsFilter;

    /**
     * Whether the menu data has been prepared, used to prevent
     * performing preparations more than once.
     *
     * @var bool
     */
    protected $prepared = false;

    /**
     * Nested structure of menu presences.
     *
     * @var Collection|MenuPresenceInterface[]
     */
    protected $menuLayout;

    /**
     * Alternative presences that do not appear in the normal menu display.
     *
     * @var Collection|MenuPresenceInterface[]
     */
    protected $alternativePresences;

    /**
     * Whether to ignore permissions during initialization.
     *
     * @var bool
     */
    protected $ignorePermission = false;


    /**
     * @param CoreInterface                  $core
     * @param AuthenticatorInterface         $auth
     * @param MenuConfigInterpreterInterface $configInterpreter
     * @param MenuPermissionsFilterInterface $permissionsFilter
     */
    public function __construct(
        CoreInterface $core,
        AuthenticatorInterface $auth,
        MenuConfigInterpreterInterface $configInterpreter,
        MenuPermissionsFilterInterface $permissionsFilter
    ) {
        $this->core              = $core;
        $this->auth              = $auth;
        $this->configInterpreter = $configInterpreter;
        $this->permissionsFilter = $permissionsFilter;
    }

    /**
     * Sets whether to ignore permissions.
     *
     * @param bool $ignore
     * @return $this
     */
    public function ignorePermission($ignore = true)
    {
        $this->ignorePermission = (bool) $ignore;

        return $this;
    }

    /**
     * Prepares the menu for presentation on demand.
     */
    public function initialize()
    {
        $this->prepareForPresentation();
    }

    /**
     * @return Collection|MenuPresenceInterface[]
     */
    public function getMenuLayout()
    {
        return $this->menuLayout;
    }

    /**
     * @return Collection|MenuPresenceInterface[]
     */
    public function getAlternativePresences()
    {
        return $this->alternativePresences;
    }

    /**
     * Clears cached menu data.
     */
    public function clearCache()
    {
        $this->core->cache()->forget(static::CACHE_KEY_LAYOUT);
        $this->core->cache()->forget(static::CACHE_KEY_INDEX);
    }


    // ------------------------------------------------------------------------------
    //      Processing and preparation
    // ------------------------------------------------------------------------------

    /**
     * Prepares CMS data for presentation in the menu views.
     */
    protected function prepareForPresentation()
    {
        if ($this->prepared) {
            return;
        }

        if ( ! ($layout = $this->getCachedLayout())) {
            $layout = $this->configInterpreter->interpretLayout($this->core->moduleConfig('menu.layout', []));
            $this->cacheLayout($layout);
        }

        if ( ! ($permissionsIndex = $this->getCachedPermissionsIndex())) {
            $permissionsIndex = $this->permissionsFilter->buildPermissionsIndex($layout);
            $this->cachePermissionsIndex($permissionsIndex);
        }


        if ( ! $this->ignorePermission && ! $this->auth->admin()) {
            $layout = $this->permissionsFilter->filterLayout($layout, $this->auth->user(), $permissionsIndex);
        }

        $this->menuLayout           = new Collection($layout->layout());
        $this->alternativePresences = new Collection($layout->alternative());
    }

    /**
     * Returns whether the CMS is configured to cache menu data.<commit_msg>Fixed menu repository init optimization<commit_after><?php
namespace Czim\CmsCore\Menu;

use Czim\CmsCore\Contracts\Menu\MenuConfigInterpreterInterface;
use Czim\CmsCore\Contracts\Menu\MenuPermissionsFilterInterface;
use Czim\CmsCore\Contracts\Menu\MenuRepositoryInterface;
use Czim\CmsCore\Contracts\Support\Data\MenuLayoutDataInterface;
use Czim\CmsCore\Contracts\Support\Data\MenuPermissionsIndexDataInterface;
use Exception;
use Illuminate\Support\Collection;
use Czim\CmsCore\Contracts\Auth\AuthenticatorInterface;
use Czim\CmsCore\Contracts\Core\CoreInterface;
use Czim\CmsCore\Contracts\Modules\Data\MenuPresenceInterface;

class MenuRepository implements MenuRepositoryInterface
{
    /**
     * The key under which the layout is cached.
     *
     * @var string
     */
    const CACHE_KEY_LAYOUT = 'cms:menu-layout-data';

    /**
     * The key under which the permissions index is cached.
     *
     * @var string
     */
    const CACHE_KEY_INDEX = 'cms:menu-permissions-index';

    /**
     * @var CoreInterface
     */
    protected $core;

    /**
     * @var AuthenticatorInterface
     */
    protected $auth;

    /**
     * @var MenuConfigInterpreterInterface
     */
    protected $configInterpreter;

    /**
     * @var MenuPermissionsFilterInterface
     */
    protected $permissionsFilter;

    /**
     * Whether the menu data has been prepared, used to prevent
     * performing preparations more than once.
     *
     * @var bool
     */
    protected $prepared = false;

    /**
     * Nested structure of menu presences.
     *
     * @var Collection|MenuPresenceInterface[]
     */
    protected $menuLayout;

    /**
     * Alternative presences that do not appear in the normal menu display.
     *
     * @var Collection|MenuPresenceInterface[]
     */
    protected $alternativePresences;

    /**
     * Whether to ignore permissions during initialization.
     *
     * @var bool
     */
    protected $ignorePermission = false;


    /**
     * @param CoreInterface                  $core
     * @param AuthenticatorInterface         $auth
     * @param MenuConfigInterpreterInterface $configInterpreter
     * @param MenuPermissionsFilterInterface $permissionsFilter
     */
    public function __construct(
        CoreInterface $core,
        AuthenticatorInterface $auth,
        MenuConfigInterpreterInterface $configInterpreter,
        MenuPermissionsFilterInterface $permissionsFilter
    ) {
        $this->core              = $core;
        $this->auth              = $auth;
        $this->configInterpreter = $configInterpreter;
        $this->permissionsFilter = $permissionsFilter;
    }

    /**
     * Sets whether to ignore permissions.
     *
     * @param bool $ignore
     * @return $this
     */
    public function ignorePermission($ignore = true)
    {
        $this->ignorePermission = (bool) $ignore;

        return $this;
    }

    /**
     * Prepares the menu for presentation on demand.
     */
    public function initialize()
    {
        $this->prepareForPresentation();
    }

    /**
     * @return Collection|MenuPresenceInterface[]
     */
    public function getMenuLayout()
    {
        return $this->menuLayout;
    }

    /**
     * @return Collection|MenuPresenceInterface[]
     */
    public function getAlternativePresences()
    {
        return $this->alternativePresences;
    }

    /**
     * Clears cached menu data.
     */
    public function clearCache()
    {
        $this->core->cache()->forget(static::CACHE_KEY_LAYOUT);
        $this->core->cache()->forget(static::CACHE_KEY_INDEX);
    }


    // ------------------------------------------------------------------------------
    //      Processing and preparation
    // ------------------------------------------------------------------------------

    /**
     * Prepares CMS data for presentation in the menu views.
     */
    protected function prepareForPresentation()
    {
        if ($this->prepared) {
            return;
        }

        if ( ! ($layout = $this->getCachedLayout())) {
            $layout = $this->configInterpreter->interpretLayout($this->core->moduleConfig('menu.layout', []));
            $this->cacheLayout($layout);
        }

        if ( ! ($permissionsIndex = $this->getCachedPermissionsIndex())) {
            $permissionsIndex = $this->permissionsFilter->buildPermissionsIndex($layout);
            $this->cachePermissionsIndex($permissionsIndex);
        }


        if ( ! $this->ignorePermission && ! $this->auth->admin()) {
            $layout = $this->permissionsFilter->filterLayout($layout, $this->auth->user(), $permissionsIndex);
        }

        $this->menuLayout           = new Collection($layout->layout());
        $this->alternativePresences = new Collection($layout->alternative());

        $this->prepared = true;
    }

    /**
     * Returns whether the CMS is configured to cache menu data.
{'fraction_non_alphanumeric': 0.1405145024085272, 'fraction_numerical': 0.0, 'mean_word_length': 2.1909744931327664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, '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': '2616414', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2580, 'n_words': 529}