Explorar el Código

add the Path::rmtree() method

Olivier Massot hace 1 año
padre
commit
ff0f4215c8
Se han modificado 2 ficheros con 44 adiciones y 5 borrados
  1. 3 2
      src/Service/Cron/Job/CleanTempFiles.php
  2. 41 3
      src/Service/Utils/Path.php

+ 3 - 2
src/Service/Cron/Job/CleanTempFiles.php

@@ -10,6 +10,7 @@ use App\Service\Cron\BaseCronJob;
 use App\Service\Cron\CronjobInterface;
 use App\Service\File\Storage\LocalStorage;
 use App\Service\Utils\DatesUtils;
+use DateTime;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\DBAL\Connection;
 use Doctrine\DBAL\ConnectionException;
@@ -131,9 +132,9 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
     }
 
     /**
-     * Purge File tables and returns the number of deleted records
+     * Purge File table and returns the number of deleted records
      *
-     * @param $maxDate
+     * @param DateTime $maxDate
      * @return int
      */
     protected function purgeFiles(\DateTime $maxDate): int {

+ 41 - 3
src/Service/Utils/Path.php

@@ -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);
+    }
 }