-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeneratesNavigationMenu.php
202 lines (175 loc) · 5.27 KB
/
GeneratesNavigationMenu.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Hyde\Framework\Actions;
use Hyde\Framework\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\CollectionService;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\Pure;
/**
* Generate the dynamic navigation menu.
*/
class GeneratesNavigationMenu
{
/**
* The current page route string.
*
* Used to check if a given link is active,
* and more importantly it is needed to
* assemble the relative link paths.
*
* @example 'posts/my-new-post.html'
* @example 'index.html'
*
* @var string
*/
public string $currentPage;
/**
* The created array of navigation links.
*
* @var array
*/
public array $links;
/**
* Construct the class.
*
* @param string $currentPage
*/
public function __construct(string $currentPage)
{
$this->currentPage = $currentPage;
$this->links = $this->getLinks();
}
/**
* Create the link array.
*
* @return array
*/
private function getLinks(): array
{
$links = $this->getLinksFromConfig();
// Automatically add top level pages
foreach ($this->getListOfCustomPages() as $slug) {
$title = $this->getTitleFromSlug($slug);
// Only add the automatic link if it is not present in the config array
if (! in_array($title, array_column($links, 'title'))) {
$links[] = [
'title' => $title,
'route' => $this->getRelativeRoutePathForSlug($slug),
'current' => $this->currentPage == $slug,
'priority' => $slug == 'index' ? 100 : 999,
];
}
}
// Add extra links
// If the documentation feature is enabled...
if (Features::hasDocumentationPages()) {
// And there is no link to the docs...
if (! in_array('Docs', array_column($links, 'title'))) {
// But a suitable file exists...
if (file_exists('_docs/index.md') || file_exists('_docs/readme.md')) {
// Then we can add a link.
$links[] = [
'title' => 'Docs',
'route' => $this->getRelativeRoutePathForSlug(
file_exists('_docs/index.md')
? Hyde::docsDirectory().'/index'
: Hyde::docsDirectory().'/readme'
),
'current' => false,
'priority' => 500,
];
}
}
}
// Remove config defined blacklisted links
foreach ($links as $key => $link) {
if (in_array(Str::slug($link['title']), config('hyde.navigationMenuBlacklist', []))) {
unset($links[$key]);
}
}
// Sort
$columns = array_column($links, 'priority');
array_multisort($columns, SORT_ASC, $links);
return $links;
}
/**
* Get the custom navigation links from the config, if there are any.
*
* @return array
*/
public function getLinksFromConfig(): array
{
$configLinks = config('hyde.navigationMenuLinks', []);
$links = [];
if (sizeof($configLinks) > 0) {
foreach ($configLinks as $link) {
$links[] = [
'title' => $link['title'],
'route' => $link['destination'] ?? $this->getRelativeRoutePathForSlug($link['slug']),
'current' => isset($link['slug']) && $this->currentPage == $link['slug'],
'priority' => $link['priority'] ?? 999,
];
}
}
return $links;
}
/**
* Get the page title.
*
* @param string $slug
* @return string
*/
public function getTitleFromSlug(string $slug): string
{
if ($slug == 'index') {
return 'Home';
}
return Hyde::titleFromSlug($slug);
}
/**
* Get a list of all the top level pages.
*
* @return array
*/
#[Pure]
private function getListOfCustomPages(): array
{
$array = [];
foreach (glob(Hyde::path('_pages/*.blade.php')) as $path) {
$array[] = basename($path, '.blade.php');
}
return array_unique(
array_merge(
$array,
CollectionService::getMarkdownPageList()
)
);
}
/**
* Inject the proper number of `../` before the links.
*
* @param string $slug
* @return string
*/
private function getRelativeRoutePathForSlug(string $slug): string
{
$nestCount = substr_count($this->currentPage, '/');
$route = '';
if ($nestCount > 0) {
$route .= str_repeat('../', $nestCount);
}
$route .= $slug.'.html';
return $route;
}
/**
* Static helper to get the array of navigation links.
*
* @param string $currentPage
* @return array
*/
public static function getNavigationLinks(string $currentPage = 'index'): array
{
$generator = new self($currentPage);
return $generator->links;
}
}