1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Filesystem\Path; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Utility class. |
20
|
|
|
* |
21
|
|
|
* Provides various utility methods for formatting class names, method names, |
22
|
|
|
* joining paths, converting memory sizes, and more. |
23
|
|
|
*/ |
24
|
|
|
class Util |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Formats a class name. |
28
|
|
|
* |
29
|
|
|
* ie: "Cecil\Step\OptimizeHtml" become "OptimizeHtml" |
30
|
|
|
* |
31
|
|
|
* @param object $class |
32
|
|
|
*/ |
33
|
|
|
public static function formatClassName($class, array $options = []): string |
34
|
|
|
{ |
35
|
|
|
$lowercase = false; |
36
|
|
|
extract($options, EXTR_IF_EXISTS); |
37
|
|
|
|
38
|
|
|
$className = substr(strrchr(\get_class($class), '\\'), 1); |
39
|
|
|
if ($lowercase) { |
40
|
|
|
$className = strtolower($className); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $className; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Formats a method name. |
48
|
|
|
* |
49
|
|
|
* ie: "Cecil\Renderer\Extension\Core::asset()" become "asset()" |
50
|
|
|
* |
51
|
|
|
* @param string $method |
52
|
|
|
*/ |
53
|
|
|
public static function formatMethodName(string $method): string |
54
|
|
|
{ |
55
|
|
|
$methodName = explode('::', $method)[1]; |
56
|
|
|
|
57
|
|
|
return $methodName; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Converts an array of strings into a path. |
62
|
|
|
*/ |
63
|
|
|
public static function joinPath(string ...$path): string |
64
|
|
|
{ |
65
|
|
|
$path = array_filter($path, function ($path) { |
66
|
|
|
return !empty($path) && !\is_null($path); |
67
|
|
|
}); |
68
|
|
|
array_walk($path, function (&$value, $key) { |
69
|
|
|
$value = str_replace('\\', '/', $value); |
70
|
|
|
$value = rtrim($value, '/'); |
71
|
|
|
$value = $key == 0 ? $value : ltrim($value, '/'); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
return Path::canonicalize(implode('/', $path)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Converts an array of strings into a system path. |
79
|
|
|
*/ |
80
|
|
|
public static function joinFile(string ...$path): string |
81
|
|
|
{ |
82
|
|
|
array_walk($path, function (&$value, $key) use (&$path) { |
83
|
|
|
$value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value); |
84
|
|
|
$value = rtrim($value, DIRECTORY_SEPARATOR); |
85
|
|
|
$value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR); |
86
|
|
|
// unset entry with empty value |
87
|
|
|
if (empty($value)) { |
88
|
|
|
unset($path[$key]); |
89
|
|
|
} |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
return implode(DIRECTORY_SEPARATOR, $path); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Converts memory size for human. |
97
|
|
|
*/ |
98
|
|
|
public static function convertMemory($size): string |
99
|
|
|
{ |
100
|
|
|
if ($size === 0) { |
101
|
|
|
return '0'; |
102
|
|
|
} |
103
|
|
|
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']; |
104
|
|
|
|
105
|
|
|
return \sprintf('%s %s', round($size / pow(1024, $i = floor(log($size, 1024))), 2), $unit[$i]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Converts microtime interval for human. |
110
|
|
|
*/ |
111
|
|
|
public static function convertMicrotime(float $start): string |
112
|
|
|
{ |
113
|
|
|
$time = microtime(true) - $start; |
114
|
|
|
if ($time < 1) { |
115
|
|
|
return \sprintf('%s ms', round($time * 1000, 0)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return \sprintf('%s s', round($time, 2)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Loads class from the source directory, in the given subdirectory $dir. |
123
|
|
|
*/ |
124
|
|
|
public static function autoload(Builder $builder, string $dir): void |
125
|
|
|
{ |
126
|
|
|
spl_autoload_register(function ($className) use ($builder, $dir) { |
127
|
|
|
$classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php"); |
128
|
|
|
if (is_readable($classFile)) { |
129
|
|
|
require $classFile; |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
// in themes |
133
|
|
|
foreach ($builder->getConfig()->getTheme() ?? [] as $theme) { |
134
|
|
|
$classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php"); |
135
|
|
|
if (is_readable($classFile)) { |
136
|
|
|
require $classFile; |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|