Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/batch-league-flysystem/src/Reader/ListFilesReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Bridge\League\Flysystem\Reader;

use League\Flysystem\FilesystemReader;
use League\Flysystem\StorageAttributes;
use Yokai\Batch\Exception\UnexpectedValueException;
use Yokai\Batch\Job\Item\ItemReaderInterface;
use Yokai\Batch\Job\JobExecutionAwareInterface;
use Yokai\Batch\Job\JobExecutionAwareTrait;
use Yokai\Batch\Job\Parameters\JobParameterAccessorInterface;

final class ListFilesReader implements ItemReaderInterface, JobExecutionAwareInterface
{
use JobExecutionAwareTrait;

public function __construct(
private FilesystemReader $filesystem,
private JobParameterAccessorInterface|null $location = null,
private bool $listDeepFiles = false,
private \Closure|null $acceptContent = null,
private \Closure|null $transformContent = null,
) {
}

public static function acceptFilesOnly(): \Closure
{
return fn(StorageAttributes $file) => $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;
}
}
}
40 changes: 40 additions & 0 deletions src/batch-league-flysystem/src/Writer/WriteToFileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Bridge\League\Flysystem\Writer;

use League\Flysystem\FilesystemReader;
use League\Flysystem\FilesystemWriter;
use Yokai\Batch\Exception\UnexpectedValueException;
use Yokai\Batch\Job\Item\ItemWriterInterface;
use Yokai\Batch\Job\JobExecutionAwareInterface;
use Yokai\Batch\Job\JobExecutionAwareTrait;
use Yokai\Batch\Job\Parameters\JobParameterAccessorInterface;

final class WriteToFileWriter implements ItemWriterInterface, JobExecutionAwareInterface
{
use JobExecutionAwareTrait;

public function __construct(
private FilesystemWriter $filesystem,
private JobParameterAccessorInterface|null $location = null,
) {
}

public function write(iterable $items): void
{
$location = '';
if ($this->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);

Check failure on line 36 in src/batch-league-flysystem/src/Writer/WriteToFileWriter.php

View workflow job for this annotation

GitHub Actions / PhpStan

Parameter #2 $contents of method League\Flysystem\FilesystemWriter::write() expects string, mixed given.
}
// TODO: Implement write() method.
}
}
Loading