|
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
|
|
|
/** |
|
144
|
|
|
* Matches a URL against known embedded content patterns. |
|
145
|
|
|
* Supports YouTube, Vimeo, Dailymotion, and GitHub Gists. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $url The URL to check |
|
148
|
|
|
* |
|
149
|
|
|
* @return array|false An associative array with 'type' and 'url' keys if a match is found, or false otherwise |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function matchesUrlPattern(string $url): array|false |
|
152
|
|
|
{ |
|
153
|
|
|
$services = [ |
|
154
|
|
|
'youtube' => [ |
|
155
|
|
|
// https://regex101.com/r/gznM1j/1 |
|
156
|
|
|
'pattern' => '(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})', |
|
157
|
|
|
'baseurl' => 'https://www.youtube-nocookie.com/embed/', |
|
158
|
|
|
'type' => 'video', |
|
159
|
|
|
], |
|
160
|
|
|
'vimeo' => [ |
|
161
|
|
|
// https://regex101.com/r/wCEFhd/1 |
|
162
|
|
|
'pattern' => 'https:\/\/vimeo\.com\/([0-9]+)', |
|
163
|
|
|
'baseurl' => 'https://player.vimeo.com/video/', |
|
164
|
|
|
'type' => 'video', |
|
165
|
|
|
], |
|
166
|
|
|
'dailymotion' => [ |
|
167
|
|
|
// https://regex101.com/r/YKnLPm/1 |
|
168
|
|
|
'pattern' => '(?:https?:\/\/)?(?:www\.)?dailymotion\.com\/video\/([a-z0-9]+)', |
|
169
|
|
|
'baseurl' => 'https://geo.dailymotion.com/player.html?video=', |
|
170
|
|
|
'type' => 'video', |
|
171
|
|
|
], |
|
172
|
|
|
'github_gist' => [ |
|
173
|
|
|
// https://regex101.com/r/y3bm2M/1 |
|
174
|
|
|
'pattern' => 'https:\/\/gist\.github\.com\/([-a-zA-Z0-9_]+\/[-a-zA-Z0-9_]+)', |
|
175
|
|
|
'baseurl' => 'https://gist.github.com/', |
|
176
|
|
|
'type' => 'script', |
|
177
|
|
|
], |
|
178
|
|
|
]; |
|
179
|
|
|
|
|
180
|
|
|
foreach ($services as $service) { |
|
181
|
|
|
if (preg_match('/' . $service['pattern'] . '/is', $url, $matches)) { |
|
182
|
|
|
return [ |
|
183
|
|
|
'type' => $service['type'], |
|
184
|
|
|
'url' => $service['baseurl'] . $matches[1], |
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|