-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.php
99 lines (78 loc) · 2.39 KB
/
Common.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
<?php
class Common
{
static $serverConfig;
static $localesMsgs = [];
static function init()
{
self::$serverConfig = json_decode(
file_get_contents(
self::joinPath(__DIR__, 'config.json')
)
);
foreach (['fr', 'en'] as $locale) {
self::$localesMsgs[$locale] = [];
foreach (['client', 'db'] as $part) {
$filename = self::joinPath(__DIR__ . '/locales', $locale, $part . '.json');
$fileContent = file_get_contents($filename);
self::$localesMsgs[$locale][$part] = json_decode($fileContent, true);
}
}
}
static function log($data, bool $html = false, bool $return = false)
{
$type = gettype($data);
// var_dump($type);
switch ($type) {
case 'array':
case 'object':
$out = print_r($data, true);
break;
default:
$out = $data . PHP_EOL;
break;
}
if ($html)
$finalString = '<div style="white-space: pre-wrap;line-height: 8px;">'
. '<br>'
. preg_replace(
'/(?:<br \/>\n?)+/',
'<br><br>',
nl2br(
str_replace(' ', ' ', $out)
)
) . '<br>'
. '</div>';
else
$finalString = $out;
if ($return)
return $finalString;
else
echo $finalString;
}
static function error_log(...$data)
{
error_log(self::log(array_shift($data), false, true));
while ($nextData = array_shift($data)) {
$stringToLog = self::log($nextData, false, true);
foreach (explode(PHP_EOL, $stringToLog) as $line)
error_log(' ' . $line);
}
}
static function randomIdentifier($length)
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
// $characters = 'AB';
$out = '';
for ($i = 0; $i < $length; ++$i) {
$out .= $characters[random_int(0, strlen($characters) - 1)];
}
return $out;
}
static function joinPath(...$args)
{
// echo implode(' - ', $args).'<br>';
return implode('/', $args);
}
}
Common::init();