|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class GeneralLib |
|
20
|
|
|
*/ |
|
21
|
|
|
class GeneralLib |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Collects and returns some information about the render time. First call will start start, the others will return. |
|
25
|
|
|
* |
|
26
|
|
|
* @return array |
|
27
|
|
|
* |
|
28
|
|
|
* @codeCoverageIgnore - don't test core functions. |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function renderStat() : array |
|
31
|
|
|
{ |
|
32
|
|
|
static $stat; |
|
33
|
|
|
|
|
34
|
|
|
// Set timer |
|
35
|
|
|
if (!isset($stat)) { |
|
36
|
|
|
$stat = [ |
|
37
|
|
|
'start_time' => microtime(true), |
|
38
|
|
|
'end_time' => null, |
|
39
|
|
|
'duration' => 0, |
|
40
|
|
|
'memory' => 0, |
|
41
|
|
|
'memory_bytes' => 0, |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
return $stat; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Get time |
|
48
|
|
|
$stat['end_time'] = microtime(true); |
|
49
|
|
|
$stat['duration'] = bcsub((string) $stat['end_time'], (string) $stat['start_time'], 4); |
|
50
|
|
|
|
|
51
|
|
|
// Memory peak |
|
52
|
|
|
$units = ['bytes', 'KB', 'MB', 'GB', 'TB']; |
|
53
|
|
|
$bytes = max(memory_get_peak_usage(true), 0); |
|
54
|
|
|
$stat['memory_bytes'] = number_format($bytes).' bytes'; |
|
55
|
|
|
|
|
56
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
|
57
|
|
|
$pow = min($pow, count($units) - 1); |
|
58
|
|
|
$bytes /= (1 << (10 * $pow)); |
|
59
|
|
|
$stat['memory'] = round($bytes, 2).' '.$units[$pow]; |
|
60
|
|
|
|
|
61
|
|
|
return $stat; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Merge config arrays in the correct way. |
|
66
|
|
|
* This rewrites the given key->value pairs and does not make key->array(value1, value2) like the |
|
67
|
|
|
* `array_merge_recursive` does. |
|
68
|
|
|
* |
|
69
|
|
|
* @throws InvalidArgumentException |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
33 |
|
public static function mergeArrayOverwrite() |
|
73
|
|
|
{ |
|
74
|
33 |
|
if (func_num_args() < 2) { |
|
75
|
1 |
|
throw new InvalidArgumentException( |
|
76
|
1 |
|
__CLASS__ . '::' . __METHOD__ . ' needs two or more array arguments', |
|
77
|
1 |
|
1000 |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
33 |
|
$arrays = func_get_args(); |
|
81
|
33 |
|
$merged = []; |
|
82
|
|
|
|
|
83
|
33 |
|
while ($arrays) { |
|
|
|
|
|
|
84
|
33 |
|
$array = array_shift($arrays); |
|
85
|
33 |
|
if (!is_array($array)) { |
|
86
|
1 |
|
throw new InvalidArgumentException( |
|
87
|
1 |
|
__CLASS__ . '::' . __METHOD__ . ' encountered a non array argument', |
|
88
|
1 |
|
1001 |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
33 |
|
if (!$array) { |
|
|
|
|
|
|
92
|
33 |
|
continue; |
|
93
|
|
|
} |
|
94
|
33 |
|
foreach ($array as $key => $value) { |
|
95
|
33 |
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
|
96
|
1 |
|
$merged[$key] = self::mergeArrayOverwrite($merged[$key], $value); |
|
97
|
|
|
} else { |
|
98
|
33 |
|
$merged[$key] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
33 |
|
return $merged; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.