1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Stdlib |
5
|
|
|
* |
6
|
|
|
* Platine Stdlib is a the collection of frequently used php features |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Stdlib |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Php.php |
33
|
|
|
* |
34
|
|
|
* The PHP helper class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Stdlib\Helper |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Stdlib\Helper; |
48
|
|
|
|
49
|
|
|
use Throwable; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class Php |
53
|
|
|
* @package Platine\Stdlib\Helper |
54
|
|
|
*/ |
55
|
|
|
class Php |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Call by callback |
59
|
|
|
* @param callable|array<mixed> $callback |
60
|
|
|
* @param mixed ...$args |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public static function call($callback, ...$args) |
65
|
|
|
{ |
66
|
|
|
if (is_string($callback)) { |
67
|
|
|
// className::method |
68
|
|
|
if (strpos($callback, '::') > 0) { |
69
|
|
|
$callback = explode('::', $callback, 2); |
70
|
|
|
} elseif (function_exists($callback)) { //function |
71
|
|
|
return $callback(...$args); |
72
|
|
|
} |
73
|
|
|
} elseif (is_object($callback) && method_exists($callback, '__invoke')) { |
74
|
|
|
return $callback(...$args); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_array($callback)) { |
78
|
|
|
[$obj, $method] = $callback; |
79
|
|
|
|
80
|
|
|
return is_object($obj) |
81
|
|
|
? $obj->{$method}(...$args) |
82
|
|
|
: $obj::$method(...$args); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//Race condition |
86
|
|
|
//@codeCoverageIgnoreStart |
87
|
|
|
return $callback(...$args); |
88
|
|
|
//@codeCoverageIgnoreEnd |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Call by array |
93
|
|
|
* @param callable|array<mixed> $callback |
94
|
|
|
* @param array<int, mixed> $args |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public static function callArray($callback, array $args) |
99
|
|
|
{ |
100
|
|
|
return self::call($callback, ...$args); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Print variables |
105
|
|
|
* @param mixed ...$vars |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public static function printVars(...$vars): string |
109
|
|
|
{ |
110
|
|
|
$string = ''; |
111
|
|
|
foreach ($vars as $var) { |
112
|
|
|
$string .= print_r($var, true) . PHP_EOL; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return (string) preg_replace("/Array\n\s+\(/", 'Array (', $string); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Export variable |
120
|
|
|
* @param mixed $var |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public static function exportVar($var): string |
124
|
|
|
{ |
125
|
|
|
$string = var_export($var, true); |
126
|
|
|
|
127
|
|
|
return (string) preg_replace( |
128
|
|
|
'/=>\s+\n\s+array \(/', |
129
|
|
|
'=> array (', |
130
|
|
|
$string |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Convert an Exception to string |
136
|
|
|
* @param Throwable $err |
137
|
|
|
* @param string $title |
138
|
|
|
* @param bool $debug |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public static function exceptionToString( |
142
|
|
|
Throwable $err, |
143
|
|
|
string $title = '', |
144
|
|
|
bool $debug = false |
145
|
|
|
): string { |
146
|
|
|
$className = get_class($err); |
147
|
|
|
if (!$debug) { |
148
|
|
|
return sprintf( |
149
|
|
|
'%s %s(code:%d) %s', |
150
|
|
|
$title, |
151
|
|
|
$className, |
152
|
|
|
$err->getCode(), |
153
|
|
|
$err->getMessage() |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return sprintf( |
158
|
|
|
'%s%s(code:%d) %s at %s line %d', |
159
|
|
|
$title ? $title . '-' : '', |
160
|
|
|
$className, |
161
|
|
|
$err->getCode(), |
162
|
|
|
$err->getMessage(), |
163
|
|
|
$err->getFile(), |
164
|
|
|
$err->getLine() |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Convert an Exception to array |
170
|
|
|
* @param Throwable $err |
171
|
|
|
* @param bool $debug |
172
|
|
|
* @return array<string, mixed> |
173
|
|
|
*/ |
174
|
|
|
public static function exceptionToArray( |
175
|
|
|
Throwable $err, |
176
|
|
|
bool $debug = false |
177
|
|
|
): array { |
178
|
|
|
if (!$debug) { |
179
|
|
|
return [ |
180
|
|
|
'code' => $err->getCode(), |
181
|
|
|
'error' => $err->getMessage() |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return [ |
186
|
|
|
'code' => $err->getCode(), |
187
|
|
|
'error' => sprintf('(%s) %s', get_class($err), $err->getMessage()), |
188
|
|
|
'file' => sprintf('at %s line %d', $err->getFile(), $err->getLine()), |
189
|
|
|
'trace' => $err->getTraceAsString(), |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|