thephpleague / flysystem

Abstraction for local and remote filesystems
https://flysystem.thephpleague.com
MIT License
13.23k stars 825 forks source link

Implement touch #1752

Closed nono303 closed 2 months ago

nono303 commented 3 months ago

Feature Request

Hi, touch function might be interesting to implement

Q A
Flysystem Version 3.16.0
Adapter Name ziparchive
Adapter version 3.23.1

Scenario / Use-case

In my case, to rmolder file not 'touched' during my process

Summary

Flysystem

 src/FilesystemOperationFailed.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/FilesystemOperationFailed.php b/src/FilesystemOperationFailed.php
index 27c726b1..42a22574 100644
--- a/src/FilesystemOperationFailed.php
+++ b/src/FilesystemOperationFailed.php
@@ -13,6 +13,7 @@ interface FilesystemOperationFailed extends FilesystemException
     public const OPERATION_FILE_EXISTS = 'FILE_EXISTS';
     public const OPERATION_CREATE_DIRECTORY = 'CREATE_DIRECTORY';
     public const OPERATION_DELETE = 'DELETE';
+   public const OPERATION_TOUCH = 'TOUCH';
     public const OPERATION_DELETE_DIRECTORY = 'DELETE_DIRECTORY';
     public const OPERATION_MOVE = 'MOVE';
     public const OPERATION_RETRIEVE_METADATA = 'RETRIEVE_METADATA';
 src/Filesystem.php | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/Filesystem.php b/src/Filesystem.php
index 5e42f3a8..257b9939 100644
--- a/src/Filesystem.php
+++ b/src/Filesystem.php
@@ -85,6 +85,11 @@ class Filesystem implements FilesystemOperator
     {
         $this->adapter->delete($this->pathNormalizer->normalizePath($location));
     }
+   
+   public function touch(string $location): void
+    {
+        $this->adapter->touch($this->pathNormalizer->normalizePath($location));
+    }

     public function deleteDirectory(string $location): void
     {
 src/UnableToTouchFile.php | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/src/UnableToTouchFile.php b/src/UnableToTouchFile.php
new file mode 100644
index 00000000..ff54139f
--- /dev/null
+++ b/src/UnableToTouchFile.php
@@ -0,0 +1,45 @@
+<?php
+
+declare(strict_types=1);
+
+namespace League\Flysystem;
+
+use RuntimeException;
+use Throwable;
+
+final class UnableToTouchFile extends RuntimeException implements FilesystemOperationFailed
+{
+    /**
+     * @var string
+     */
+    private $location = '';
+
+    /**
+     * @var string
+     */
+    private $reason;
+
+    public static function atLocation(string $location, string $reason = '', Throwable $previous = null): UnableToTouchFile
+    {
+        $e = new static(rtrim("Unable to touch file located at: {$location}. {$reason}"), 0, $previous);
+        $e->location = $location;
+        $e->reason = $reason;
+
+        return $e;
+    }
+
+    public function operation(): string
+    {
+        return FilesystemOperationFailed::OPERATION_DELETE;
+    }
+
+    public function reason(): string
+    {
+        return $this->reason;
+    }
+
+    public function location(): string
+    {
+        return $this->location;
+    }
+}

ziparchive

 ZipArchiveAdapter.php | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/ZipArchiveAdapter.php b/ZipArchiveAdapter.php
index c73ca55..b79aec8 100644
--- a/ZipArchiveAdapter.php
+++ b/ZipArchiveAdapter.php
@@ -142,6 +142,19 @@ final class ZipArchiveAdapter implements FilesystemAdapter
             throw UnableToDeleteFile::atLocation($path, $statusString);
         }
     }
+   
+    public function touch(string $path): void
+    {
+        $prefixedPath = $this->pathPrefixer->prefixPath($path);
+        $zipArchive = $this->zipArchiveProvider->createZipArchive();
+        $success = $zipArchive->setMtimeName($prefixedPath,time());
+        $statusString = $zipArchive->getStatusString();
+        $zipArchive->close();
+
+        if ( ! $success) {
+            throw UnableToTouchFile::atLocation($path, $statusString);
+        }
+    }

     public function deleteDirectory(string $path): void
     {
frankdejonge commented 2 months ago

Touch is not something that translates to cloud filesystems. Since this package aims to normalise across file storage, not just be a local filesystem abstraction, I don't see this falling into scope without causing abstraction leak or fragmentation.

nono303 commented 2 months ago

Hi @frankdejonge, Thx for your answer. Effectively, touch is not native for storage object in cloud FS but might be useful cf. https://stackoverflow.com/questions/13455168/is-there-a-way-to-touch-a-file-in-amazon-s3