-
Notifications
You must be signed in to change notification settings - Fork 215
/
Navigation.php
executable file
路114 lines (92 loc) 路 2.77 KB
/
Navigation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace SleepingOwl\Admin;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface;
use SleepingOwl\Admin\Contracts\Navigation\PageInterface;
class Navigation extends \KodiComponents\Navigation\Navigation implements NavigationInterface
{
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' => Str::snake(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;
}
}