-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
56 lines (51 loc) · 1.19 KB
/
functions.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
<?php
if (! function_exists('attr')) {
function attr(string $text) {
return htmlspecialchars($text);
}
}
if (! function_exists('env')) {
function env(string $key, $fallback = null) {
foreach ($_ENV as $k => $v) {
if ($key === $k) {
return $v;
}
}
$env = require __DIR__ . '/env.php';
foreach ($env as $k => $v) {
if ($key === $k) {
return $v;
}
}
if (func_num_args() > 1) {
return $fallback;
}
throw new \Exception("Environment variable $key not found.");
}
}
if (! function_exists('base_path')) {
function base_path(string $path = '') {
if (trim($path) === '') {
return env('BASE_PATH');
}
$basePath = trim(env('BASE_PATH'), '/');
return $basePath
? sprintf('/%s/%s', $basePath, ltrim($path, '/'))
: sprintf('/%s', ltrim($path, '/'))
;
}
}
if (! function_exists('base_url')) {
function base_url(string $path = '') {
$BASE_PATH = base_path($path);
return $BASE_PATH
? sprintf('%s://%s/%s', env('PROTOCOL'), env('DOMAIN'), ltrim($BASE_PATH, '/'))
: sprintf('%s://%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_NAME'])
;
}
}
if (! function_exists('partial')) {
function partial($name) {
is_callable($name) ? $name() : $name;
}
}