|
|
@@ -39,12 +39,14 @@ class Path
|
|
|
* ['','foo.jpg'] > 'foo.jpg'
|
|
|
* ['dir','0','a.jpg'] > 'dir/0/a.jpg'
|
|
|
*
|
|
|
+ * @param string ...$paths
|
|
|
* @return string
|
|
|
*/
|
|
|
- public static function join(): string
|
|
|
+ public static function join(string|int ...$paths): string
|
|
|
{
|
|
|
- $paths = array_filter(func_get_args(), static function ($s) { return $s !== ''; });
|
|
|
- return preg_replace('#/+#','/',implode('/', $paths));
|
|
|
+ $paths = array_map(function ($x) { return "".$x; }, $paths);
|
|
|
+ $paths = array_filter($paths, static function ($s) { return $s !== ''; });
|
|
|
+ return preg_replace('#/+#','/', implode('/', $paths));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -59,6 +61,13 @@ class Path
|
|
|
return glob(self::join($path, $glob));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Reads the content of a file.
|
|
|
+ *
|
|
|
+ * @param string $path The path to the file to be read.
|
|
|
+ * @return string The content of the file.
|
|
|
+ * @throws RuntimeException If the file could not be read.
|
|
|
+ */
|
|
|
public static function read(string $path): string
|
|
|
{
|
|
|
$content = file_get_contents($path);
|
|
|
@@ -79,4 +88,33 @@ class Path
|
|
|
$fileLocator = new FileLocator($directories);
|
|
|
return $fileLocator->locate($filename, null, false)[0];
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Recursively removes a directory and all its contents.
|
|
|
+ *
|
|
|
+ * @param string $path The path to the directory to be removed.
|
|
|
+ * @return bool Returns true if the directory was successfully removed, false otherwise.
|
|
|
+ */
|
|
|
+ protected static function rmtree(string $path): bool
|
|
|
+ {
|
|
|
+ if (!file_exists($path)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_dir($path)) {
|
|
|
+ return unlink($path);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (scandir($path) as $item) {
|
|
|
+ if ($item == '.' || $item == '..') {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!self::rmtree($path . DIRECTORY_SEPARATOR . $item)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return rmdir($path);
|
|
|
+ }
|
|
|
}
|