|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
|
6
|
|
|
use Hyde\Framework\Services\RoutingService; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
class DocumentationSidebar extends NavigationMenu |
|
11
|
|
|
{ |
|
12
|
|
|
public function generate(): self |
|
13
|
|
|
{ |
|
14
|
|
|
RoutingService::getInstance()->getRoutesForModel(DocumentationPage::class)->each(function (Route $route) { |
|
15
|
|
|
$this->items->push(NavItem::fromRoute($route)->setPriority($this->getPriorityForRoute($route))); |
|
16
|
|
|
}); |
|
17
|
|
|
|
|
18
|
|
|
return $this; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function hasGroups(): bool |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->items->map(function (NavItem $item) { |
|
24
|
|
|
return $item->getGroup() !== null; |
|
25
|
|
|
})->contains(true); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getGroups(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->items->map(function (NavItem $item) { |
|
31
|
|
|
return $item->getGroup(); |
|
32
|
|
|
})->unique()->toArray(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getItemsInGroup(?string $group): Collection |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->items->filter(function ($item) use ($group) { |
|
38
|
|
|
return $item->getGroup() === $group || $item->getGroup() === Str::slug($group); |
|
39
|
|
|
})->sortBy('priority')->values(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function filterHiddenItems(): Collection |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->items->reject(function (NavItem $item) { |
|
45
|
|
|
return $item->route->getSourceModel()->matter('hidden', false) || ($item->route->getRouteKey() === 'docs/index'); |
|
|
|
|
|
|
46
|
|
|
})->values(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function getPriorityForRoute(Route $route): int |
|
50
|
|
|
{ |
|
51
|
|
|
return $route->getSourceModel()->matter('priority') ?? $this->findPriorityInConfig($route->getSourceModel()->slug); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function findPriorityInConfig(string $slug): int |
|
55
|
|
|
{ |
|
56
|
|
|
$orderIndexArray = config('docs.sidebar_order', []); |
|
57
|
|
|
|
|
58
|
|
|
if (! in_array($slug, $orderIndexArray)) { |
|
59
|
|
|
return 500; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return array_search($slug, $orderIndexArray) + 250; |
|
63
|
|
|
|
|
64
|
|
|
// Adding 250 makes so that pages with a front matter priority that is lower |
|
65
|
|
|
// can be shown first. It's lower than the fallback of 500 so that they |
|
66
|
|
|
// still come first. This is all to make it easier to mix priorities. |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|