-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHyperlinks.php
175 lines (147 loc) · 5.18 KB
/
Hyperlinks.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
<?php
declare(strict_types=1);
namespace Hyde\Foundation\Kernel;
use Hyde\Facades\Config;
use Hyde\Support\Models\Route;
use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Exceptions\BaseUrlNotSetException;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Illuminate\Support\Str;
use function str_ends_with;
use function str_starts_with;
use function substr_count;
use function file_exists;
use function str_replace;
use function str_repeat;
use function substr;
use function blank;
use function rtrim;
use function trim;
/**
* Contains helpers and logic for resolving web paths for compiled files.
*
* It's bound to the HydeKernel instance, and is an integral part of the framework.
*/
class Hyperlinks
{
protected HydeKernel $kernel;
public function __construct(HydeKernel $kernel)
{
$this->kernel = $kernel;
}
/**
* Format a web link to an HTML file, allowing for pretty URLs, if enabled.
*
* @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFormatHtmlPathTest
*/
public function formatLink(string $destination): string
{
if (Config::getBool('hyde.pretty_urls', false) === true) {
if (str_ends_with($destination, '.html')) {
if ($destination === 'index.html') {
return '/';
}
if (str_ends_with($destination, 'index.html')) {
return substr($destination, 0, -10);
}
return substr($destination, 0, -5);
}
}
return $destination;
}
/**
* Inject the proper number of `../` before the links in Blade templates.
*
* @param string $destination relative to output directory on compiled site
*
* @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFileHelperRelativeLinkTest
*/
public function relativeLink(string $destination): string
{
if (str_starts_with($destination, '../')) {
return $destination;
}
$nestCount = substr_count($this->kernel->currentRouteKey() ?? '', '/');
$route = '';
if ($nestCount > 0) {
$route .= str_repeat('../', $nestCount);
}
$route .= $this->formatLink($destination);
if (Config::getBool('hyde.pretty_urls', false) === true && $route === '/') {
return './';
}
return str_replace('//', '/', $route);
}
/**
* Gets a relative web link to the given file stored in the _site/media folder.
*
* An exception will be thrown if the file does not exist in the _media directory,
* and the second argument is set to true.
*/
public function mediaLink(string $destination, bool $validate = false): string
{
if ($validate && ! file_exists($sourcePath = "{$this->kernel->getMediaDirectory()}/$destination")) {
throw new FileNotFoundException($sourcePath);
}
return $this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination");
}
/**
* Gets a relative web link to the given image stored in the _site/media folder.
* If the image is remote (starts with http) it will be returned as is.
*
* If true is passed as the second argument, and a base URL is set,
* the image will be returned with a qualified absolute URL.
*/
public function asset(string $name, bool $preferQualifiedUrl = false): string
{
if (str_starts_with($name, 'http')) {
return $name;
}
$name = Str::start($name, "{$this->kernel->getMediaOutputDirectory()}/");
if ($preferQualifiedUrl && $this->hasSiteUrl()) {
return $this->url($name);
}
return $this->relativeLink($name);
}
/**
* Check if a site base URL has been set in config (or .env).
*
* The default value is `http://localhost`, which is not considered a valid site URL.
*/
public function hasSiteUrl(): bool
{
$value = Config::getNullableString('hyde.url');
return ! blank($value) && $value !== 'http://localhost';
}
/**
* Return a qualified URL to the supplied path if a base URL is set.
*
* @param string $path An optional relative path suffix. Omit to return the base URL.
*
* @throws BaseUrlNotSetException If no site URL is set and no path is provided.
*/
public function url(string $path = ''): string
{
$path = $this->formatLink(trim($path, '/'));
if (str_starts_with($path, 'http')) {
return $path;
}
if ($this->hasSiteUrl()) {
return rtrim(rtrim(Config::getString('hyde.url'), '/')."/$path", '/');
}
// Since v1.7.0, we return the relative path even if the base URL is not set,
// as this is more likely to be the desired behavior the user's expecting.
if (! blank($path)) {
return $path;
}
// User is trying to get the base URL, but it's not set
throw new BaseUrlNotSetException();
}
/**
* Get a route instance by its key from the kernel's route collection.
*/
public function route(string $key): ?Route
{
return $this->kernel->routes()->get($key);
}
}