env()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 10
nc 5
nop 2
dl 0
loc 22
rs 9.2222
c 1
b 0
f 1
1
<?php
2
3
if (!function_exists('jlog')) {
4
    /**
5
     * Log function for development
6
     *
7
     * @param mixed $messages message
8
     *
9
     * @return void
10
     */
11
    function jlog($messages)
12
    {
13
        \Jidaikobo\Log::write($messages);
14
    }
15
}
16
17
if (!function_exists('__')) {
18
    /**
19
     * Translate the given text and replace placeholders.
20
     *
21
     * @param  string $key     The message key.
22
     * @param  string $default The default message if the key does not exist.
23
     * @param  array  $replace Variables to replace in the message.
24
     *
25
     * @return string
26
     */
27
    function __(string $key, string $default = '', array $replace = []): string
28
    {
29
        return \Jidaikobo\Kontiki\Utils\Lang::get($key, $default, $replace);
30
    }
31
}
32
33
if (!function_exists('e')) {
34
    /**
35
     * Escape the given string for safe HTML output.
36
     *
37
     * @param  string|null $value The string to escape. Null values are converted to an empty string.
38
     *
39
     * @return string
40
     */
41
    function e(?string $value): string
42
    {
43
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
44
    }
45
}
46
47
if (!function_exists('env')) {
48
    /**
49
     * Get an environment variable from $_SERVER or $_ENV, or return a default value.
50
     *
51
     * @param string $key The environment variable key.
52
     * @param mixed $default The default value if the key does not exist.
53
     * @return mixed
54
     */
55
    function env(string $key, $default = null)
56
    {
57
        static $cache = [];
58
59
        if (array_key_exists($key, $cache)) {
60
            return $cache[$key];
61
        }
62
63
        $value = $default;
64
65
        // Check in $_SERVER
66
        if (isset($_SERVER[$key]) && $_SERVER[$key] !== '') {
67
            $value = $_SERVER[$key];
68
        }
69
70
        // Check in $_ENV
71
        if (isset($_ENV[$key]) && $_ENV[$key] !== '') {
72
            $value = $_ENV[$key];
73
        }
74
75
        $cache[$key] = $value;
76
        return $value;
77
    }
78
}
79
80
if (!function_exists('setenv')) {
81
    /**
82
     * Set an environment variable.
83
     *
84
     * @param string $key The environment variable key.
85
     * @param mixed $value The value to set. If null, the variable will be removed.
86
     * @return void
87
     */
88
    function setenv(string $key, $value): void
89
    {
90
        if ($value === null) {
91
            unset($_SERVER[$key], $_ENV[$key]);
92
            return;
93
        }
94
95
        if (!is_scalar($value)) {
96
            throw new \InvalidArgumentException("setenv() only accepts scalar values (string, int, float, bool).");
97
        }
98
99
        $_SERVER[$key] = (string) $value;
100
        $_ENV[$key] = (string) $value;
101
    }
102
}
103
104
if (!function_exists('performance')) {
105
    function performance($timer = false): void
106
    {
107
        \Jidaikobo\Kontiki\Bootstrap::performance($timer);
108
    }
109
}
110
111
if (!function_exists('homeUrl')) {
112
    function homeUrl(): string
113
    {
114
        return env('BASEURL');
115
    }
116
}
117