<commit_before><?php

namespace SleepingOwl\Admin;

use Route;
use Illuminate\Support\Collection;
use SleepingOwl\Admin\Contracts\Navigation\PageInterface;
use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface;

class Navigation extends \KodiComponents\Navigation\Navigation implements PageInterface
{
    protected $currentPage;
    protected $currentUrl;

    /**
     * Overload current page.
     * @return \KodiComponents\Navigation\Contracts\PageInterface|null
     */
    public function getCurrentPage()
    {
        $this->setAliasesId($this->getPages());
        $this->findActivePage();

        return $this->currentPage;
    }

    /**
     * Set Alias Id to Page.
     * @param Collection $pages
     */
    public function setAliasesId(Collection $pages)
    {
        $pages->each(function (PageInterface $page) {
            $page->setAliasId();

            if ($page->getPages()->count()) {
                $this->setAliasesId($page->getPages());
            }
        });
    }

    /**
     * @param string $url
     * @param array $foundPages
     */
    protected function findActive($url, array &$foundPages)
    {
        $this->findPageByAliasId($this->getPages(), $url);
    }

    /**
     * @param Collection $pages
     * @param $url
     */
    protected function findPageByAliasId(Collection $pages, $url)
    {
        $pages->each(function (PageInterface $page) use ($url) {
            $urlPath = parse_url($url, PHP_URL_PATH);

            if (Route::current()) {
                $parameters = collect(Route::current()->parameters());

                if ($parameters->has('adminModel')) {
                    $routeUrl = route('admin.model', [
                        'adminModel' => snake_case(class_basename($parameters->get('adminModel'))),
                    ]);

                    $urlPath = parse_url($routeUrl, PHP_URL_PATH);
                }
            }

            if ($urlPath) {
                if (md5($urlPath) == $page->getAliasId()) {
                    $this->currentPage = $page;

                    return;
                }
            }

            $this->findPageByAliasId($page->getPages(), $url);
        });
    }

    /**
     * @return bool
     */
    protected function findActivePage()
    {
        if (! is_null($this->currentPage)) {
            return true;
        }

        $foundPages = [];

        $url = $this->getCurrentUrl();

        $this->findActive($url, $foundPages);

        if (! is_null($this->currentPage)) {
            $this->currentPage->setActive();
        }

        if (config('navigation.aliases')) {
            $this->findActiveByAliases(
                ltrim(parse_url($url, PHP_URL_PATH), '/')
            );
        }

        return false;
    }

    /**
     * Переопределяем метод toArray(), вносим класс, treeview для активации меню tree в adminlte 2.4
     * @return array
     */
    public function toArray()
    {
        if ($this->isActive() and ! $this->hasClassProperty($class = config('navigation.class.active', 'active'))) {
            $this->setHtmlAttribute('class', $class);
        }
        if ($this->hasChild() and ! $this->hasClassProperty($class = config('navigation.class.has_child', 'treeview'))) {
            $this->setHtmlAttribute('class', $class);
        }
        return parent::toArray() + [
                'hasChild' => $this->hasChild(),
                'id' => $this->getId(),
                'title' => $this->getTitle(),
                'icon' => $this->getIcon(),
                'priority' => $this->getPriority(),
                'url' => $this->getUrl(),
                'path' => $this->getPath(),
                'isActive' => $this->isActive(),
                'attributes' => $this->htmlAttributesToString(),
                'badges' => $this->getBadges()->sortBy(function (BadgeInterface $badge) {
                    return $badge->getPriority();
                }),
            ];
    }
}<commit_msg>update navigation.php<commit_after><?php

namespace SleepingOwl\Admin;

use Route;
use Illuminate\Support\Collection;
use SleepingOwl\Admin\Contracts\Navigation\PageInterface;
use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface;
use KodiComponents\Support\HtmlAttributes;

class Navigation extends \KodiComponents\Navigation\Navigation implements NavigationInterface
{

    protected $currentPage;
    protected $currentUrl;

    /**
     * Переопределяем метод toArray(), вносим класс, treeview для активации меню tree в adminlte 2.4
     * @return array
     */
    public function __construct(){
        $class = config('navigation.class.has_child', 'treeview')
        $this->setHtmlAttribute('class', $class);
    }

    /**
     * Overload current page.
     * @return \KodiComponents\Navigation\Contracts\PageInterface|null
     */
    public function getCurrentPage()
    {
        $this->setAliasesId($this->getPages());
        $this->findActivePage();

        return $this->currentPage;
    }

    /**
     * Set Alias Id to Page.
     * @param Collection $pages
     */
    public function setAliasesId(Collection $pages)
    {
        $pages->each(function (PageInterface $page) {
            $page->setAliasId();

            if ($page->getPages()->count()) {
                $this->setAliasesId($page->getPages());
            }
        });
    }

    /**
     * @param string $url
     * @param array $foundPages
     */
    protected function findActive($url, array &$foundPages)
    {
        $this->findPageByAliasId($this->getPages(), $url);
    }

    /**
     * @param Collection $pages
     * @param $url
     */
    protected function findPageByAliasId(Collection $pages, $url)
    {
        $pages->each(function (PageInterface $page) use ($url) {
            $urlPath = parse_url($url, PHP_URL_PATH);

            if (Route::current()) {
                $parameters = collect(Route::current()->parameters());

                if ($parameters->has('adminModel')) {
                    $routeUrl = route('admin.model', [
                        'adminModel' => snake_case(class_basename($parameters->get('adminModel'))),
                    ]);

                    $urlPath = parse_url($routeUrl, PHP_URL_PATH);
                }
            }

            if ($urlPath) {
                if (md5($urlPath) == $page->getAliasId()) {
                    $this->currentPage = $page;

                    return;
                }
            }

            $this->findPageByAliasId($page->getPages(), $url);
        });
    }

    /**
     * @return bool
     */
    protected function findActivePage()
    {
        if (! is_null($this->currentPage)) {
            return true;
        }

        $foundPages = [];

        $url = $this->getCurrentUrl();

        $this->findActive($url, $foundPages);

        if (! is_null($this->currentPage)) {
            $this->currentPage->setActive();
        }

        if (config('navigation.aliases')) {
            $this->findActiveByAliases(
                ltrim(parse_url($url, PHP_URL_PATH), '/')
            );
        }

        return false;
    }
}
{'fraction_non_alphanumeric': 0.14512726761355646, 'fraction_numerical': 0.0008437631837997469, 'mean_word_length': 1.9040424663127806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, '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': '3904101', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1964, 'n_words': 346}