|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Cecil. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil; |
|
15
|
|
|
|
|
16
|
|
|
class Util |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Formats a class name. |
|
20
|
|
|
* |
|
21
|
|
|
* ie: "Cecil\Step\PostProcessHtml" become "PostProcessHtml" |
|
22
|
|
|
* |
|
23
|
|
|
* @param object $class |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function formatClassName($class, array $options = []): string |
|
26
|
|
|
{ |
|
27
|
|
|
$lowercase = false; |
|
28
|
|
|
extract($options, EXTR_IF_EXISTS); |
|
29
|
|
|
|
|
30
|
|
|
$className = substr(strrchr(\get_class($class), '\\'), 1); |
|
31
|
|
|
if ($lowercase) { |
|
32
|
|
|
$className = strtolower($className); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $className; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Converts an array of strings into a path. |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function joinPath(string ...$path): string |
|
42
|
|
|
{ |
|
43
|
|
|
$path = array_filter($path, function ($path) { |
|
44
|
|
|
return !empty($path) && !\is_null($path); |
|
45
|
|
|
}); |
|
46
|
|
|
array_walk($path, function (&$value, $key) { |
|
47
|
|
|
$value = str_replace('\\', '/', $value); |
|
48
|
|
|
$value = rtrim($value, '/'); |
|
49
|
|
|
$value = $key == 0 ? $value : ltrim($value, '/'); |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
return implode('/', $path); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Converts an array of strings into a system path. |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function joinFile(string ...$path): string |
|
59
|
|
|
{ |
|
60
|
|
|
array_walk($path, function (&$value, $key) use (&$path) { |
|
61
|
|
|
$value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value); |
|
62
|
|
|
$value = rtrim($value, DIRECTORY_SEPARATOR); |
|
63
|
|
|
$value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR); |
|
64
|
|
|
// unset entry with empty value |
|
65
|
|
|
if (empty($value)) { |
|
66
|
|
|
unset($path[$key]); |
|
67
|
|
|
} |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
return implode(DIRECTORY_SEPARATOR, $path); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Converts memory size for human. |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function convertMemory($size): string |
|
77
|
|
|
{ |
|
78
|
|
|
if ($size === 0) { |
|
79
|
|
|
return '0'; |
|
80
|
|
|
} |
|
81
|
|
|
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']; |
|
82
|
|
|
|
|
83
|
|
|
return sprintf('%s %s', round($size / pow(1024, ($i = floor(log($size, 1024)))), 2), $unit[$i]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Converts microtime interval for human. |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function convertMicrotime(float $start): string |
|
90
|
|
|
{ |
|
91
|
|
|
$time = microtime(true) - $start; |
|
92
|
|
|
if ($time < 1) { |
|
93
|
|
|
return sprintf('%s ms', round($time * 1000, 0)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return sprintf('%s s', round($time, 2)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Loads class from the source directory, in the given subdirectory $dir. |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function autoload(Builder $builder, string $dir): void |
|
103
|
|
|
{ |
|
104
|
|
|
spl_autoload_register(function ($className) use ($builder, $dir) { |
|
105
|
|
|
$classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php"); |
|
106
|
|
|
if (file_exists($classFile)) { |
|
107
|
|
|
require $classFile; |
|
108
|
|
|
} |
|
109
|
|
|
}); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|