|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace { |
|
6
|
|
|
use Hyde\Foundation\HydeKernel; |
|
7
|
|
|
use Hyde\Support\Filesystem\MediaFile; |
|
8
|
|
|
|
|
9
|
|
|
if (! function_exists('hyde')) { |
|
10
|
|
|
/** |
|
11
|
|
|
* Get the available HydeKernel instance. |
|
12
|
|
|
*/ |
|
13
|
|
|
function hyde(): HydeKernel |
|
14
|
|
|
{ |
|
15
|
|
|
return HydeKernel::getInstance(); |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
if (defined('HYDE_COMPATIBILITY_MODE') && HYDE_COMPATIBILITY_MODE === true) { |
|
|
|
|
|
|
20
|
|
|
// Don't declare these functions when running in compatibility mode. |
|
21
|
|
|
} else { |
|
22
|
|
|
if (! function_exists('asset')) { |
|
23
|
|
|
/** |
|
24
|
|
|
* Gets a MediaAsset instance for the given file stored in the `_site/media` folder. |
|
25
|
|
|
* The returned value can be cast into a string in Blade views to resole the URL. |
|
26
|
|
|
* |
|
27
|
|
|
* If a base URL is configured, the image will be returned with a qualified absolute URL. |
|
28
|
|
|
* Otherwise, a relative path will be returned based on the rendered page's location. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist in the `_media` source directory. |
|
31
|
|
|
*/ |
|
32
|
|
|
function asset(string $name): MediaFile |
|
33
|
|
|
{ |
|
34
|
|
|
return hyde()->asset($name); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (! function_exists('route')) { |
|
39
|
|
|
/** |
|
40
|
|
|
* Get a page route instance by its key. Casting it to a string will return a relative link to the page. |
|
41
|
|
|
*/ |
|
42
|
|
|
function route(string $key): ?Hyde\Support\Models\Route |
|
43
|
|
|
{ |
|
44
|
|
|
return hyde()->routes()->getRoute($key); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (! function_exists('url')) { |
|
49
|
|
|
/** |
|
50
|
|
|
* Get a qualified URL to the supplied path if a base URL is set. |
|
51
|
|
|
*/ |
|
52
|
|
|
function url(string $path = ''): string |
|
53
|
|
|
{ |
|
54
|
|
|
return hyde()->url($path); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
namespace Hyde { |
|
61
|
|
|
use Hyde\Facades\Filesystem; |
|
62
|
|
|
use Hyde\Foundation\HydeKernel; |
|
63
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
64
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
65
|
|
|
|
|
66
|
|
|
use function function_exists; |
|
67
|
|
|
use function array_merge; |
|
68
|
|
|
use function str_replace; |
|
69
|
|
|
use function implode; |
|
70
|
|
|
use function trim; |
|
71
|
|
|
use function md5; |
|
72
|
|
|
|
|
73
|
|
|
if (! function_exists('\Hyde\hyde')) { |
|
74
|
|
|
/** |
|
75
|
|
|
* Get the available HydeKernel instance. |
|
76
|
|
|
*/ |
|
77
|
|
|
function hyde(): HydeKernel |
|
78
|
|
|
{ |
|
79
|
|
|
return HydeKernel::getInstance(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (! function_exists('\Hyde\unslash')) { |
|
84
|
|
|
/** |
|
85
|
|
|
* Remove trailing slashes from the start and end of a string. |
|
86
|
|
|
*/ |
|
87
|
|
|
function unslash(string $string): string |
|
88
|
|
|
{ |
|
89
|
|
|
return trim($string, '/\\'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (! function_exists('\Hyde\unixsum')) { |
|
94
|
|
|
/** |
|
95
|
|
|
* A EOL agnostic wrapper for calculating MD5 checksums. |
|
96
|
|
|
* |
|
97
|
|
|
* This function is not cryptographically secure. |
|
98
|
|
|
*/ |
|
99
|
|
|
function unixsum(string $string): string |
|
100
|
|
|
{ |
|
101
|
|
|
return md5(str_replace(["\r\n", "\r"], "\n", $string)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (! function_exists('\Hyde\unixsum_file')) { |
|
106
|
|
|
/** |
|
107
|
|
|
* Shorthand for {@see unixsum()} but loads a file. |
|
108
|
|
|
*/ |
|
109
|
|
|
function unixsum_file(string $file): string |
|
110
|
|
|
{ |
|
111
|
|
|
return \Hyde\unixsum(Filesystem::getContents($file)); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (! function_exists('\Hyde\make_title')) { |
|
116
|
|
|
function make_title(string $value): string |
|
117
|
|
|
{ |
|
118
|
|
|
return hyde()->makeTitle($value); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (! function_exists('\Hyde\title')) { |
|
123
|
|
|
function title(string $value): string |
|
124
|
|
|
{ |
|
125
|
|
|
return hyde()->makeTitle($value); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (! function_exists('\Hyde\normalize_newlines')) { |
|
130
|
|
|
function normalize_newlines(string $string): string |
|
131
|
|
|
{ |
|
132
|
|
|
return hyde()->normalizeNewlines($string); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (! function_exists('\Hyde\strip_newlines')) { |
|
137
|
|
|
function strip_newlines(string $string): string |
|
138
|
|
|
{ |
|
139
|
|
|
return hyde()->stripNewlines($string); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (! function_exists('\Hyde\trim_slashes')) { |
|
144
|
|
|
function trim_slashes(string $string): string |
|
145
|
|
|
{ |
|
146
|
|
|
return hyde()->trimSlashes($string); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (! function_exists('\Hyde\evaluate_arrayable')) { |
|
151
|
|
|
function evaluate_arrayable(array|Arrayable $array): array |
|
152
|
|
|
{ |
|
153
|
|
|
return $array instanceof Arrayable ? $array->toArray() : $array; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (! function_exists('\Hyde\yaml_encode')) { |
|
158
|
|
|
function yaml_encode(mixed $input, int $inline = 2, int $indent = 4, int $flags = 0): string |
|
159
|
|
|
{ |
|
160
|
|
|
return Yaml::dump($input instanceof Arrayable ? $input->toArray() : $input, $inline, $indent, $flags); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if (! function_exists('\Hyde\yaml_decode')) { |
|
165
|
|
|
function yaml_decode(string $input, int $flags = 0): mixed |
|
166
|
|
|
{ |
|
167
|
|
|
return Yaml::parse($input, $flags); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (! function_exists('\Hyde\path_join')) { |
|
172
|
|
|
function path_join(string $directory, string ...$paths): string |
|
173
|
|
|
{ |
|
174
|
|
|
return implode('/', array_merge([$directory], $paths)); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (! function_exists('\Hyde\normalize_slashes')) { |
|
179
|
|
|
function normalize_slashes(string $string): string |
|
180
|
|
|
{ |
|
181
|
|
|
return str_replace('\\', '/', $string); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|