]> _ Git - cubist_util.git/commitdiff
wip #6516 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 Nov 2023 17:02:13 +0000 (18:02 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 Nov 2023 17:02:13 +0000 (18:02 +0100)
src/Files/Files.php
src/Files/IVirtualDirectoryErrorListener.php [new file with mode: 0644]
src/Files/VirtualDirectory.php

index 59415900eee9b742394a88c55c20df63ee0397ee..77da144be8b9c89483ea4f254583d5cf00f40676 100644 (file)
@@ -109,6 +109,16 @@ class Files
         return $f->getFilename();
     }
 
+    public static function isNotEmpty($file)
+    {
+        return file_exists($file) && filesize($file) > 0;
+    }
+
+    public static function isEmpty($file)
+    {
+        return !static::isNotEmpty($file);
+    }
+
     public static function getExtension($file)
     {
         $p = parse_url($file);
diff --git a/src/Files/IVirtualDirectoryErrorListener.php b/src/Files/IVirtualDirectoryErrorListener.php
new file mode 100644 (file)
index 0000000..73bfd0b
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+namespace Cubist\Util\Files;
+
+interface IVirtualDirectoryErrorListener
+{
+    public function handleVirtualDirectoryError($message);
+}
\ No newline at end of file
index c1e8db223e7b1e5b53f5ab7e0a638fa173877d7d..ec0efa412cd13b5ef16e8ffa136f19f22baf08c1 100644 (file)
@@ -14,6 +14,9 @@ class VirtualDirectory
     protected $_tmp;
     protected $_dirs = array();
 
+    /** @var IVirtualDirectoryErrorListener|null */
+    protected $_errorListener = null;
+
     public function __construct($path)
     {
         $this->_path = Files::mkdir($path);
@@ -23,10 +26,35 @@ class VirtualDirectory
         $this->_tmp = array();
     }
 
-    public function copy($from, $to, $prepend = false,$check=true)
+    /**
+     * @return IVirtualDirectoryErrorListener|null
+     */
+    public function getErrorListener(): ?IVirtualDirectoryErrorListener
+    {
+        return $this->_errorListener;
+    }
+
+    /**
+     * @param IVirtualDirectoryErrorListener|null $errorListener
+     */
+    public function setErrorListener(?IVirtualDirectoryErrorListener $errorListener): void
+    {
+        $this->_errorListener = $errorListener;
+    }
+
+    protected function throwError($message)
+    {
+        if (null === $this->_errorListener) {
+            throw new \Exception($message);
+        }
+        $this->_errorListener->handleVirtualDirectoryError($message);
+    }
+
+
+    public function copy($from, $to, $prepend = false, $check = true)
     {
         if ($check && !file_exists($from)) {
-            throw new \Exception(sprintf('File "%s" doesn\'t exist (to %s)', $from, $to));
+            $this->throwError(sprintf('File "%s" doesn\'t exist (to %s)', $from, $to));
         }
         $realto = $this->path($to);
         if (!$realto) {
@@ -46,7 +74,7 @@ class VirtualDirectory
     public function copyDirectory($from, $to)
     {
         if (!file_exists($from)) {
-            throw new \Exception(sprintf('Directory %s doen\'t exist', $from));
+            $this->throwError(sprintf('Directory %s doen\'t exist', $from));
         }
         $this->_directories[$this->path($to)] = $from;
         return $this;
@@ -81,7 +109,7 @@ class VirtualDirectory
             }
 
             if (!file_exists($from)) {
-                throw new \Exception(sprintf('Failed to copy %s to %s. Source doesn\'t exist', $from, $to));
+                $this->throwError(sprintf('Failed to copy %s to %s. Source doesn\'t exist', $from, $to));
             }
 
             if (!file_exists($to) || filesize($from) !== filesize($to) || filemtime($from) !== filemtime($to)) {
@@ -89,7 +117,7 @@ class VirtualDirectory
                 $from_esc = escapeshellarg($from);
                 `cp -p $from_esc $to_esc`;
                 if (!file_exists($to)) {
-                    throw new \Exception(sprintf('Failed to copy %s to %s', $from, $to));
+                    $this->throwError(sprintf('Failed to copy %s to %s', $from, $to));
                 }
             }
         }