1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit; |
12
|
|
|
|
13
|
|
|
use Phalcon\Debug\Dump; |
14
|
|
|
use Phalcon\Text; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Utils |
18
|
|
|
* |
19
|
|
|
* @author Julien Turbide <[email protected]> |
20
|
|
|
* @copyright Zemit Team <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* @version 1.0 |
24
|
|
|
* |
25
|
|
|
* @package Zemit |
26
|
|
|
*/ |
27
|
|
|
class Utils |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Remove time and memory limits |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public static function setUnlimitedRuntime() { |
34
|
|
|
ini_set('memory_limit', -1); |
35
|
|
|
ini_set('max_execution_time', 0); |
36
|
|
|
ini_set('max_input_time', 0); |
37
|
|
|
set_time_limit(0); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set max upload file size and post size |
42
|
|
|
* @param string $size |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public static function setMaxUploadFileSize(string $size = '2M') { |
46
|
|
|
ini_set('upload_max_filesize', $size); |
47
|
|
|
ini_set('post_max_size', $size); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $class |
52
|
|
|
* |
53
|
|
|
* @return bool|string |
54
|
|
|
*/ |
55
|
|
|
public static function getNamespace(object $class): string |
56
|
|
|
{ |
57
|
|
|
return substr(get_class($class), 0, strrpos(get_class($class), "\\")); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param object|string $class |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function getDirname($class): string |
65
|
|
|
{ |
66
|
|
|
return dirname((new \ReflectionClass($class))->getFileName()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return a human readable memory usage array (in MB) |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
public static function getMemoryUsage(): array |
74
|
|
|
{ |
75
|
|
|
$toMb = 1048576.2; |
76
|
|
|
return [ |
77
|
|
|
'memory' => (memory_get_usage() / $toMb) . ' MB', |
78
|
|
|
'memoryPeak' => (memory_get_peak_usage() / $toMb) . ' MB', |
79
|
|
|
'realMemory' => (memory_get_usage(true) / $toMb) . ' MB', |
80
|
|
|
'realMemoryPeak' => (memory_get_peak_usage(true) / $toMb) . ' MB', |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Dump the passed variables and end the script. |
86
|
|
|
* |
87
|
|
|
* @param mixed |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public static function dd() |
91
|
|
|
{ |
92
|
|
|
call_user_func_array('dd', func_get_args()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Dump the passed variables without end the script. |
97
|
|
|
* |
98
|
|
|
* @param mixed |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public static function dump() |
102
|
|
|
{ |
103
|
|
|
call_user_func_array('dump', func_get_args()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Call a function |
108
|
|
|
* - Uncamelize the method name |
109
|
|
|
* |
110
|
|
|
* @param $name string |
111
|
|
|
* @param $arguments array |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
* @throws \Exception Throw exception if function name can't be found |
115
|
|
|
*/ |
116
|
|
|
public function __call(string $name, array $arguments) |
117
|
|
|
{ |
118
|
|
|
return self::__callStatic($name, $arguments); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Call a function |
123
|
|
|
* - Uncamelize the method name |
124
|
|
|
* |
125
|
|
|
* @param $name string |
126
|
|
|
* @param $arguments array |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
* @throws \Exception Throw exception if function name can't be found |
130
|
|
|
*/ |
131
|
|
|
public static function __callStatic(string $name, array $arguments) |
132
|
|
|
{ |
133
|
|
|
$functionName = Text::uncamelize($name); |
134
|
|
|
if (function_exists($functionName)) { |
135
|
|
|
return call_user_func_array($functionName, func_get_args()); |
136
|
|
|
} |
137
|
|
|
else { |
138
|
|
|
throw new \Exception("Function {$functionName} does not exists."); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!function_exists('dd')) { |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Dump the passed variables and end the script. |
147
|
|
|
* |
148
|
|
|
* @param mixed |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
function dd() |
152
|
|
|
{ |
153
|
|
|
call_user_func_array('dump', func_get_args()); |
154
|
|
|
exit(1); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!function_exists('dump')) { |
159
|
|
|
/** |
160
|
|
|
* Dump the passed variables without end the script. |
161
|
|
|
* |
162
|
|
|
* @param mixed |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
function dump() |
166
|
|
|
{ |
167
|
|
|
array_map( |
168
|
|
|
function ($variable) { |
169
|
|
|
$dump = new Dump([], true); |
170
|
|
|
$ret = $dump->variable($variable); |
171
|
|
|
if (PHP_SAPI === 'cli') { |
172
|
|
|
$ret = strip_tags($ret) . PHP_EOL; |
173
|
|
|
} |
174
|
|
|
echo $ret; |
175
|
|
|
}, |
176
|
|
|
func_get_args() |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.