|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Foundation; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\HydeKernelContract; |
|
6
|
|
|
use Hyde\Framework\Exceptions\BaseUrlNotSetException; |
|
7
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Contains helpers and logic for resolving web paths for compiled files. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Hyperlinks |
|
13
|
|
|
{ |
|
14
|
|
|
protected HydeKernelContract $kernel; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(HydeKernelContract $kernel) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->kernel = $kernel; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Format a link to an HTML file, allowing for pretty URLs, if enabled. |
|
23
|
|
|
* |
|
24
|
|
|
* @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest |
|
25
|
|
|
*/ |
|
26
|
|
|
public function formatHtmlPath(string $destination): string |
|
27
|
|
|
{ |
|
28
|
|
|
if (config('site.pretty_urls', false) === true) { |
|
29
|
|
|
if (str_ends_with($destination, '.html')) { |
|
30
|
|
|
if ($destination === 'index.html') { |
|
31
|
|
|
return '/'; |
|
32
|
|
|
} |
|
33
|
|
|
if ($destination === DocumentationPage::getOutputDirectory().'/index.html') { |
|
34
|
|
|
return DocumentationPage::getOutputDirectory().'/'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return substr($destination, 0, -5); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $destination; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Inject the proper number of `../` before the links in Blade templates. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $destination relative to output directory on compiled site |
|
48
|
|
|
* @return string |
|
49
|
|
|
* |
|
50
|
|
|
* @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest |
|
51
|
|
|
*/ |
|
52
|
|
|
public function relativeLink(string $destination): string |
|
53
|
|
|
{ |
|
54
|
|
|
if (str_starts_with($destination, '../')) { |
|
55
|
|
|
return $destination; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$nestCount = substr_count($this->kernel->currentPage(), '/'); |
|
|
|
|
|
|
59
|
|
|
$route = ''; |
|
60
|
|
|
if ($nestCount > 0) { |
|
61
|
|
|
$route .= str_repeat('../', $nestCount); |
|
62
|
|
|
} |
|
63
|
|
|
$route .= $this->formatHtmlPath($destination); |
|
64
|
|
|
|
|
65
|
|
|
return str_replace('//', '/', $route); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets a relative web link to the given image stored in the _site/media folder. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function image(string $name): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (str_starts_with($name, 'http')) { |
|
74
|
|
|
return $name; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->relativeLink('media/'.basename($name)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return a qualified URI path, if SITE_URL is set in .env, else return false. |
|
82
|
|
|
* |
|
83
|
|
|
* @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $path optional relative path suffix. Omit to return base url. |
|
86
|
|
|
* @return string|false |
|
87
|
|
|
*/ |
|
88
|
|
|
public function uriPath(string $path = ''): string|false |
|
89
|
|
|
{ |
|
90
|
|
|
if (config('site.url', false)) { |
|
91
|
|
|
return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if a site base URL has been set in config (or .env). |
|
99
|
|
|
*/ |
|
100
|
|
|
public function hasSiteUrl(): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return ! blank(config('site.url')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return a qualified URI path to the supplied path if a base URL is set. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $path optional relative path suffix. Omit to return base url. |
|
109
|
|
|
* @param string|null $default optional default value to return if no site url is set. |
|
110
|
|
|
* @return string |
|
111
|
|
|
* |
|
112
|
|
|
* @throws BaseUrlNotSetException If no site URL is set and no default is provided |
|
113
|
|
|
*/ |
|
114
|
|
|
public function url(string $path = '', ?string $default = null): string |
|
115
|
|
|
{ |
|
116
|
|
|
$path = $this->formatHtmlPath(trim($path, '/')); |
|
117
|
|
|
|
|
118
|
|
|
if ($this->hasSiteUrl()) { |
|
119
|
|
|
return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($default !== null) { |
|
123
|
|
|
return $default.'/'.($path ?? ''); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
throw new BaseUrlNotSetException(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|