From 7008756cdf4d2cd69ff309a677730291becd08f6 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 24 Nov 2023 18:02:13 +0100 Subject: [PATCH] wip #6516 @0.5 --- src/Files/Files.php | 10 ++++++ src/Files/IVirtualDirectoryErrorListener.php | 8 +++++ src/Files/VirtualDirectory.php | 38 +++++++++++++++++--- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/Files/IVirtualDirectoryErrorListener.php diff --git a/src/Files/Files.php b/src/Files/Files.php index 5941590..77da144 100644 --- a/src/Files/Files.php +++ b/src/Files/Files.php @@ -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 index 0000000..73bfd0b --- /dev/null +++ b/src/Files/IVirtualDirectoryErrorListener.php @@ -0,0 +1,8 @@ +_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)); } } } -- 2.39.5