-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
206 lines (179 loc) · 5.52 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
use Monolog\Logger;
use Orkester\GraphQL\Set\SelectionSet;
use Orkester\Manager;
use Orkester\Services\MTrace;
function _M($msg, $params = NULL)
{
return $msg;
}
function mdump(...$var)
{
Manager::getLog()->log(\Monolog\Logger::DEBUG, ...$var);
return $var[0] ?? null;
}
function mfatal(...$var)
{
Manager::getLog()->log(\Monolog\Logger::CRITICAL, ...$var);
return $var[0];
}
function merror(...$var)
{
Manager::getLog()->log(\Monolog\Logger::ERROR, ...$var);
return $var[0];
}
function mwarn(...$var)
{
Manager::getLog()->log(\Monolog\Logger::WARNING, ...$var);
return $var[0];
}
function minfo(...$var)
{
Manager::getLog()->log(\Monolog\Logger::INFO, ...$var);
return $var[0];
}
function mtrace(...$var)
{
Manager::getLog()->log(\Monolog\Logger::INFO, ...$var);
return $var[0];
}
function mconsole($var, $tag = 'CONSOLE', $level = Logger::DEBUG)
{
Manager::getLog()->getLogger()->log($level, json_encode($var), ['tag' => $tag]);
return $var;
}
function mtracestack()
{
return MTrace::tracestack();
}
function buildPath(array $parts): string
{
return implode(DIRECTORY_SEPARATOR, $parts);
}
function array_find($xs, $f)
{
foreach ($xs as $x) {
if (call_user_func($f, $x) === true)
return $x;
}
return null;
}
function array_pop_key(mixed $key, array &$array)
{
$value = $array[$key] ?? null;
unset($array[$key]);
return $value;
}
function group_by(array|\Illuminate\Support\Collection $data, $key): array
{
$accumulator = [];
foreach($data as $element) {
$groupKey = $element[$key];
$accumulator[$groupKey][] = $element;
}
return $accumulator;
}
function method(string $class, string $method): string
{
return "$class::$method";
}
function get_method(string $method, string $class = null): string
{
$class ??= get_called_class();
return "$class::$method";
}
function mrequest($vars, $from = 'ALL', $order = '')
{
if (is_array($vars)) {
foreach ($vars as $v) {
$values[$v] = mrequest($v, $from);
}
return $values;
} else {
$value = NULL;
// Seek in all scope?
if ($from == 'ALL') {
// search in REQUEST
if (is_null($value)) {
$value = isset($_REQUEST[$vars]) ? $_REQUEST[$vars] : NULL;
}
if (is_null($value)) {
// Not found in REQUEST? try GET or POST
// Order? Default is use the same order as defined in php.ini ("EGPCS")
if (!isset($order)) {
$order = ini_get('variables_order');
}
if (strpos($order, 'G') < strpos($order, 'P')) {
$value = isset($_GET[$vars]) ? $_GET[$vars] : NULL;
// If not found, search in post
if (is_null($value)) {
$value = isset($_POST[$vars]) ? $_POST[$vars] : NULL;
}
} else {
$value = isset($_POST[$vars]) ? $_POST[$vars] : NULL;
// If not found, search in get
if (is_null($value)) {
$value = isset($_GET[$vars]) ? $_GET[$vars] : NULL;
}
}
}
// If we still didn't have the value
// let's try in the global scope
if ((is_null($value)) && ((strpos($vars, '[')) === false)) {
$value = isset($_GLOBALS[$vars]) ? $_GLOBALS[$vars] : NULL;
}
// If we still didn't has the value
// let's try in the session scope
if (is_null($value)) {
if ($vars) {
$value = isset($_SESSION[$vars]) ? $_SESSION[$vars] : NULL;
}
}
} else if ($from == 'GET') {
$value = isset($_GET[$vars]) ? $_GET[$vars] : NULL;
} elseif ($from == 'POST') {
$value = isset($_POST[$vars]) ? $_POST[$vars] : NULL;
} elseif ($from == 'SESSION') {
$value = isset($_SESSION[$vars]) ? $_SESSION[$vars] : NULL;
} elseif ($from == 'REQUEST') {
$value = isset($_REQUEST[$vars]) ? $_REQUEST[$vars] : NULL;
}
return $value;
}
}
/**
* Check for valid JSON string
* @param $x
* @return bool
*/
function isJson($x)
{
if (!is_string($x) || trim($x) === "") return false;
return $x === 'null' || (
// Maybe an empty string, array or object
$x === '""' ||
$x === '[]' ||
$x === '{}' ||
// Maybe an encoded JSON string
$x[0] === '"' && substr($x, -1) === '"' ||
// Maybe a numeric array
$x[0] === '[' && substr($x, -1) === ']' ||
// Maybe an associative array
$x[0] === '{' && substr($x, -1) === '}'
) && json_decode($x) !== null;
}
function errorHandler($errno, $errstr, $errfile, $errline)
{
$codes = Manager::getConf('logs.errorCodes');
if (Manager::getLog()) {
Manager::logMessage("[ERROR] [Code] $errno [Error] $errstr [File] $errfile [Line] $errline");
}
}
function shutdown()
{
$error = error_get_last();
if ($error) {
errorHandler($error['type'], $error['message'], $error['file'], $error['line']);
}
Manager::getLog()->log(Logger::INFO, "SHUTDOWN");
}