|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
14
|
|
|
|
|
15
|
|
|
class Util |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Filesystem */ |
|
18
|
|
|
protected static $fs; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns a Symfony\Component\Filesystem instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @return Filesystem |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public static function getFS(): Filesystem |
|
26
|
|
|
{ |
|
27
|
1 |
|
if (!self::$fs instanceof Filesystem) { |
|
28
|
1 |
|
self::$fs = new Filesystem(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
return self::$fs; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Checks if a date is valid. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $date |
|
38
|
|
|
* @param string $format |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function isDateValid(string $date, string $format = 'Y-m-d'): bool |
|
43
|
|
|
{ |
|
44
|
1 |
|
$d = \DateTime::createFromFormat($format, $date); |
|
45
|
|
|
|
|
46
|
1 |
|
return $d && $d->format($format) === $date; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Date to DateTime. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $date |
|
53
|
|
|
* |
|
54
|
|
|
* @return \DateTime |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public static function dateToDatetime($date): \DateTime |
|
57
|
|
|
{ |
|
58
|
|
|
// DateTime |
|
59
|
1 |
|
if ($date instanceof \DateTime) { |
|
60
|
1 |
|
return $date; |
|
61
|
|
|
} |
|
62
|
|
|
// timestamp or 'AAAA-MM-DD' |
|
63
|
1 |
|
if (is_numeric($date)) { |
|
64
|
1 |
|
return (new \DateTime())->setTimestamp($date); |
|
65
|
|
|
} |
|
66
|
|
|
// string (ie: '01/01/2019', 'today') |
|
67
|
1 |
|
return new \DateTime($date); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Formats a class name. |
|
72
|
|
|
* |
|
73
|
|
|
* ie: "Cecil\Step\PostProcessHtml" become "PostProcessHtml" |
|
74
|
|
|
* |
|
75
|
|
|
* @param object $class |
|
76
|
|
|
* @param array $options |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public static function formatClassName($class, array $options = []): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
$lowercase = false; |
|
83
|
1 |
|
extract($options, EXTR_IF_EXISTS); |
|
84
|
|
|
|
|
85
|
1 |
|
$className = substr(strrchr(get_class($class), '\\'), 1); |
|
86
|
1 |
|
if ($lowercase) { |
|
87
|
1 |
|
$className = strtolower($className); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $className; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tests if a string is an URL. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $url |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public static function isUrl(string $url): bool |
|
101
|
|
|
{ |
|
102
|
1 |
|
return (bool) preg_match('~^(?:f|ht)tps?://~i', $url); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Tests if a remote file exists. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $remoteFile |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public static function isRemoteFileExists(string $remoteFile): bool |
|
113
|
|
|
{ |
|
114
|
1 |
|
$handle = @fopen($remoteFile, 'r'); |
|
115
|
1 |
|
if (is_resource($handle)) { |
|
116
|
1 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Converts an array of strings into a path. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string[] $path |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public static function joinPath(string ...$path): string |
|
130
|
|
|
{ |
|
131
|
|
|
array_walk($path, function (&$value, $key) { |
|
132
|
1 |
|
$value = str_replace('\\', '/', $value); |
|
133
|
1 |
|
$value = rtrim($value, '/'); |
|
134
|
1 |
|
$value = $key == 0 ? $value : ltrim($value, '/'); |
|
135
|
1 |
|
}); |
|
136
|
|
|
|
|
137
|
1 |
|
return implode('/', $path); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Converts an array of strings into a system path. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string[] $path |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public static function joinFile(string ...$path): string |
|
148
|
|
|
{ |
|
149
|
|
|
array_walk($path, function (&$value, $key) use (&$path) { |
|
150
|
1 |
|
$value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value); |
|
151
|
1 |
|
$value = rtrim($value, DIRECTORY_SEPARATOR); |
|
152
|
1 |
|
$value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR); |
|
153
|
|
|
// unset entry with empty value |
|
154
|
1 |
|
if (empty($value)) { |
|
155
|
1 |
|
unset($path[$key]); |
|
156
|
|
|
} |
|
157
|
1 |
|
}); |
|
158
|
|
|
|
|
159
|
1 |
|
return implode(DIRECTORY_SEPARATOR, $path); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Converts an array to a string. |
|
164
|
|
|
* |
|
165
|
|
|
* ie: [0 => 'A', 1 => 'B'] become '0:A, 1:B' |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $array |
|
168
|
|
|
* @param string $separator Separtor between the key and the value in the result string |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function arrayToString(array $array, string $separator = ':'): string |
|
173
|
|
|
{ |
|
174
|
|
|
$string = ''; |
|
175
|
|
|
|
|
176
|
|
|
foreach ($array as $key => $value) { |
|
177
|
|
|
$string .= sprintf('%s%s%s, ', $key, $separator, $value); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return substr($string, 0, -2); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Combines an array into a string. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $array |
|
187
|
|
|
* @param string $keyToKey The key that become the key of the new array |
|
188
|
|
|
* @param string $keyToValue The key that become the value of the new array |
|
189
|
|
|
* @param string $separator The separtor between the key and the value in the result string |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public static function combineArrayToString( |
|
194
|
|
|
array $array, |
|
195
|
|
|
string $keyToKey, |
|
196
|
|
|
string $keyToValue, |
|
197
|
|
|
string $separator = ':' |
|
198
|
|
|
): string { |
|
199
|
1 |
|
$string = ''; |
|
200
|
|
|
|
|
201
|
1 |
|
foreach ($array as $subArray) { |
|
202
|
1 |
|
$string .= sprintf('%s%s%s, ', $subArray[$keyToKey], $separator, $subArray[$keyToValue]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
return substr($string, 0, -2); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* file_get_contents() function with error handler. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $filename |
|
212
|
|
|
* |
|
213
|
|
|
* @return string|false |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public static function fileGetContents($filename) |
|
216
|
|
|
{ |
|
217
|
1 |
|
set_error_handler( |
|
218
|
|
|
function ($severity, $message, $file, $line) { |
|
219
|
1 |
|
throw new \ErrorException($message, 0, $severity, $file, $line, null); |
|
220
|
1 |
|
} |
|
221
|
|
|
); |
|
222
|
|
|
|
|
223
|
|
|
try { |
|
224
|
1 |
|
$return = file_get_contents($filename); |
|
225
|
1 |
|
} catch (\Exception $e) { |
|
226
|
1 |
|
$return = false; |
|
227
|
|
|
} |
|
228
|
1 |
|
restore_error_handler(); |
|
229
|
|
|
|
|
230
|
1 |
|
return $return; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Returns MIME content type and subtype of a file. |
|
235
|
|
|
* |
|
236
|
|
|
* ie: ['text', 'text/plain'] |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $filename |
|
239
|
|
|
* |
|
240
|
|
|
* @return string[] |
|
241
|
|
|
*/ |
|
242
|
1 |
|
public static function getMimeType(string $filename): array |
|
243
|
|
|
{ |
|
244
|
1 |
|
if (false === $subtype = mime_content_type($filename)) { |
|
245
|
|
|
throw new \Exception(sprintf('Can\'t get MIME content type of "%s"', $filename)); |
|
246
|
|
|
} |
|
247
|
1 |
|
$type = explode('/', $subtype)[0]; |
|
248
|
|
|
|
|
249
|
|
|
return [ |
|
250
|
1 |
|
$type, |
|
251
|
1 |
|
$subtype, |
|
252
|
|
|
]; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|