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
|
|
|
public static function renderStat() : array |
29
|
|
|
{ |
30
|
|
|
static $stat; |
31
|
|
|
|
32
|
|
|
// Set timer |
33
|
|
|
if (!isset($stat)) { |
34
|
|
|
$stat = [ |
35
|
|
|
'start_time' => microtime(true), |
36
|
|
|
'end_time' => null, |
37
|
|
|
'duration' => 0, |
38
|
|
|
'memory' => 0, |
39
|
|
|
'memory_bytes' => 0, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
return $stat; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Get time |
46
|
|
|
$stat['end_time'] = microtime(true); |
47
|
|
|
$stat['duration'] = bcsub((string) $stat['end_time'], (string) $stat['start_time'], 4); |
48
|
|
|
|
49
|
|
|
// Memory peak |
50
|
|
|
$units = ['bytes', 'KB', 'MB', 'GB', 'TB']; |
51
|
|
|
$bytes = max(memory_get_peak_usage(true), 0); |
52
|
|
|
$stat['memory_bytes'] = number_format($bytes).' bytes'; |
53
|
|
|
|
54
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
55
|
|
|
$pow = min($pow, count($units) - 1); |
56
|
|
|
$bytes /= (1 << (10 * $pow)); |
57
|
|
|
$stat['memory'] = round($bytes, 2).' '.$units[$pow]; |
58
|
|
|
|
59
|
|
|
return $stat; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Merge config arrays in the correct way. |
64
|
|
|
* This rewrites the given key->value pairs and does not make key->array(value1, value2) like the |
65
|
|
|
* `array_merge_recursive` does. |
66
|
|
|
* |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
27 |
|
public static function mergeArrayOverwrite() |
71
|
|
|
{ |
72
|
27 |
|
if (func_num_args() < 2) { |
73
|
|
|
throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . ' needs two or more array arguments'); |
74
|
|
|
} |
75
|
27 |
|
$arrays = func_get_args(); |
76
|
27 |
|
$merged = []; |
77
|
|
|
|
78
|
27 |
|
while ($arrays) { |
|
|
|
|
79
|
27 |
|
$array = array_shift($arrays); |
80
|
27 |
|
if (!is_array($array)) { |
81
|
|
|
throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . ' encountered a non array argument'); |
82
|
|
|
} |
83
|
27 |
|
if (!$array) { |
|
|
|
|
84
|
27 |
|
continue; |
85
|
|
|
} |
86
|
27 |
|
foreach ($array as $key => $value) { |
87
|
27 |
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
88
|
|
|
$merged[$key] = self::mergeArrayOverwrite($merged[$key], $value); |
89
|
|
|
} else { |
90
|
27 |
|
$merged[$key] = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
27 |
|
return $merged; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.