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(callable|array $callback, mixed ...$args): mixed |
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(callable|array $callback, array $args): mixed |
99
|
|
|
{ |
100
|
|
|
return self::call($callback, ...$args); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Convert an Exception to string |
105
|
|
|
* @param Throwable $err |
106
|
|
|
* @param string $title |
107
|
|
|
* @param bool $debug |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public static function exceptionToString( |
111
|
|
|
Throwable $err, |
112
|
|
|
string $title = '', |
113
|
|
|
bool $debug = false |
114
|
|
|
): string { |
115
|
|
|
$className = get_class($err); |
116
|
|
|
if ($debug === false) { |
117
|
|
|
return sprintf( |
118
|
|
|
'%s %s(code:%d) %s', |
119
|
|
|
$title, |
120
|
|
|
$className, |
121
|
|
|
$err->getCode(), |
122
|
|
|
$err->getMessage() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return sprintf( |
127
|
|
|
'%s%s(code:%d) %s at %s line %d', |
128
|
|
|
$title ? $title . '-' : '', |
129
|
|
|
$className, |
130
|
|
|
$err->getCode(), |
131
|
|
|
$err->getMessage(), |
132
|
|
|
$err->getFile(), |
133
|
|
|
$err->getLine() |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert an Exception to array |
139
|
|
|
* @param Throwable $err |
140
|
|
|
* @param bool $debug |
141
|
|
|
* @return array<string, mixed> |
142
|
|
|
*/ |
143
|
|
|
public static function exceptionToArray( |
144
|
|
|
Throwable $err, |
145
|
|
|
bool $debug = false |
146
|
|
|
): array { |
147
|
|
|
if ($debug === false) { |
148
|
|
|
return [ |
149
|
|
|
'code' => $err->getCode(), |
150
|
|
|
'error' => $err->getMessage() |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return [ |
155
|
|
|
'code' => $err->getCode(), |
156
|
|
|
'error' => sprintf('(%s) %s', get_class($err), $err->getMessage()), |
157
|
|
|
'file' => sprintf('at %s line %d', $err->getFile(), $err->getLine()), |
158
|
|
|
'trace' => $err->getTraceAsString(), |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|