From 2d1ed31445684362d8079d161218d70e082766a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Wed, 29 Jan 2025 10:21:55 +0100 Subject: [PATCH] Read/Write from/to league/flysystem --- .../src/Reader/ListFilesReader.php | 74 +++++++++++++++++++ .../src/Writer/WriteToFileWriter.php | 40 ++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/batch-league-flysystem/src/Reader/ListFilesReader.php create mode 100644 src/batch-league-flysystem/src/Writer/WriteToFileWriter.php diff --git a/src/batch-league-flysystem/src/Reader/ListFilesReader.php b/src/batch-league-flysystem/src/Reader/ListFilesReader.php new file mode 100644 index 00000000..51fb7f6f --- /dev/null +++ b/src/batch-league-flysystem/src/Reader/ListFilesReader.php @@ -0,0 +1,74 @@ + $file->isFile(); + } + + public static function acceptDirectoriesOnly(): \Closure + { + return fn(StorageAttributes $file) => $file->isDir(); + } + + public static function transformContent(): \Closure + { + return fn(StorageAttributes $file, FilesystemReader $filesystem) => $filesystem->read($file->path()); + } + + public static function transformPublicUrl(): \Closure + { + return fn(StorageAttributes $file, FilesystemReader $filesystem) => $filesystem->publicUrl($file->path()); + } + + public function read(): iterable + { + $location = ''; + if ($this->location !== null) { + $location = $this->location->get($this->jobExecution); + } + if (!\is_string($location)) { + throw UnexpectedValueException::type('string', $location); + } + + foreach ($this->filesystem->listContents($location, $this->listDeepFiles) as $file) { + $acceptContent = true; + if ($this->acceptContent !== null) { + $acceptContent = ($this->acceptContent)($file); + } + if (!$acceptContent) { + continue; + } + + if ($this->transformContent !== null) { + $file = ($this->transformContent)($file); + } + + yield $file; + } + } +} diff --git a/src/batch-league-flysystem/src/Writer/WriteToFileWriter.php b/src/batch-league-flysystem/src/Writer/WriteToFileWriter.php new file mode 100644 index 00000000..7d093d06 --- /dev/null +++ b/src/batch-league-flysystem/src/Writer/WriteToFileWriter.php @@ -0,0 +1,40 @@ +location !== null) { + $location = $this->location->get($this->jobExecution); + } + if (!\is_string($location)) { + throw UnexpectedValueException::type('string', $location); + } + + foreach ($items as $item) { + $this->filesystem->write($location, $item); + } + // TODO: Implement write() method. + } +}