Skip to content

Commit c9f90ae

Browse files
Use service locator as registry instead of MountManager
1 parent 4033e4f commit c9f90ae

File tree

5 files changed

+34
-60
lines changed

5 files changed

+34
-60
lines changed

config/services/task.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ services:
66

77
cleverage_flysystem_process.task.file_fetch:
88
class: CleverAge\FlysystemProcessBundle\Task\FileFetchTask
9+
arguments: [!tagged_locator { tag: 'flysystem.storage', index_by: 'storage' }]
910
CleverAge\FlysystemProcessBundle\Task\FileFetchTask:
1011
alias: cleverage_flysystem_process.task.file_fetch
12+
public: true
1113

1214
cleverage_flysystem_process.task.list_content:
1315
class: CleverAge\FlysystemProcessBundle\Task\ListContentTask
16+
arguments: [!tagged_locator { tag: 'flysystem.storage', index_by: 'storage' }]
1417
CleverAge\FlysystemProcessBundle\Task\ListContentTask:
1518
alias: cleverage_flysystem_process.task.list_content
19+
public: true
1620

1721
cleverage_flysystem_process.task.remove_file:
1822
class: CleverAge\FlysystemProcessBundle\Task\RemoveFileTask
1923
arguments:
20-
- '@monolog.logger'
24+
- '@logger'
25+
- !tagged_locator { tag: 'flysystem.storage', index_by: 'storage' }
2126
CleverAge\FlysystemProcessBundle\Task\RemoveFileTask:
2227
alias: cleverage_flysystem_process.task.remove_file
28+
public: true

src/Task/FileFetchTask.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use League\Flysystem\Filesystem;
2020
use League\Flysystem\FilesystemException;
2121
use League\Flysystem\FilesystemOperator;
22+
use Symfony\Component\DependencyInjection\ServiceLocator;
2223
use Symfony\Component\OptionsResolver\OptionsResolver;
2324

2425
/**
@@ -33,6 +34,13 @@ class FileFetchTask extends AbstractConfigurableTask implements IterableTaskInte
3334

3435
protected array $matchingFiles = [];
3536

37+
/**
38+
* @param MountManager|null $mountManager
39+
*/
40+
public function __construct(protected readonly ServiceLocator $storages)
41+
{
42+
}
43+
3644
/**
3745
* @throws \InvalidArgumentException
3846
*/
@@ -41,8 +49,8 @@ public function initialize(ProcessState $state): void
4149
// Configure options
4250
parent::initialize($state);
4351

44-
$this->sourceFS = new Filesystem($this->getOption($state, 'source_filesystem'));
45-
$this->destinationFS = new Filesystem($this->getOption($state, 'destination_filesystem'));
52+
$this->sourceFS = $this->storages->get($this->getOption($state, 'source_filesystem'));
53+
$this->destinationFS = $this->storages->get($this->getOption($state, 'destination_filesystem'));
4654
}
4755

4856
/**
@@ -74,7 +82,7 @@ public function next(ProcessState $state): bool
7482
{
7583
$this->findMatchingFiles($state);
7684

77-
return next($this->matchingFiles);
85+
return false === next($this->matchingFiles) ? false : true;
7886
}
7987

8088
/**
@@ -116,8 +124,6 @@ protected function findMatchingFiles(ProcessState $state): void
116124
*/
117125
protected function doFileCopy(ProcessState $state, string $filename, bool $removeSource): bool|string|null
118126
{
119-
$prefixFrom = $this->getOption($state, 'source_filesystem');
120-
121127
$buffer = $this->sourceFS->readStream($filename);
122128

123129
try {
@@ -132,7 +138,7 @@ protected function doFileCopy(ProcessState $state, string $filename, bool $remov
132138
}
133139

134140
if ($removeSource) {
135-
$this->sourceFS->delete(\sprintf('%s://%s', $prefixFrom, $filename));
141+
$this->sourceFS->delete($filename);
136142
}
137143

138144
return $result ? $filename : null;

src/Task/FilesystemOptionTrait.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Task/ListContentTask.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@
1818
use CleverAge\ProcessBundle\Model\ProcessState;
1919
use League\Flysystem\FilesystemException;
2020
use League\Flysystem\FilesystemOperator;
21+
use Psr\Log\LoggerInterface;
22+
use Symfony\Component\DependencyInjection\ServiceLocator;
2123
use Symfony\Component\OptionsResolver\OptionsResolver;
2224

2325
/**
2426
* Iterate over the content of a filesystem.
2527
*/
2628
class ListContentTask extends AbstractConfigurableTask implements IterableTaskInterface
2729
{
28-
use FilesystemOptionTrait;
29-
3030
protected ?array $fsContent = null;
3131

32-
protected function configureOptions(OptionsResolver $resolver): void
32+
public function __construct(protected readonly ServiceLocator $storages)
3333
{
34-
$this->configureFilesystemOption($resolver, 'filesystem');
34+
}
3535

36+
37+
protected function configureOptions(OptionsResolver $resolver): void
38+
{
39+
$resolver->setRequired('filesystem');
40+
$resolver->setAllowedTypes('filesystem', 'string');
3641
$resolver->setDefault('file_pattern', null);
3742
$resolver->setAllowedTypes('file_pattern', ['null', 'string']);
3843
}
@@ -44,7 +49,7 @@ protected function configureOptions(OptionsResolver $resolver): void
4449
public function execute(ProcessState $state): void
4550
{
4651
if (null === $this->fsContent || null === key($this->fsContent)) {
47-
$filesystem = $this->getFilesystem($state, 'filesystem');
52+
$filesystem = $this->storages->get($this->getOption($state, 'filesystem'));
4853
$pattern = $this->getOption($state, 'file_pattern');
4954

5055
$this->fsContent = $this->getFilteredFilesystemContents($filesystem, $pattern);

src/Task/RemoveFileTask.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@
1717
use CleverAge\ProcessBundle\Model\ProcessState;
1818
use League\Flysystem\FilesystemException;
1919
use Psr\Log\LoggerInterface;
20+
use Symfony\Component\DependencyInjection\ServiceLocator;
2021
use Symfony\Component\OptionsResolver\OptionsResolver;
2122

2223
/**
2324
* Remove a file from a filesystem.
2425
*/
2526
class RemoveFileTask extends AbstractConfigurableTask
2627
{
27-
use FilesystemOptionTrait;
28-
29-
public function __construct(protected LoggerInterface $logger)
28+
public function __construct(protected LoggerInterface $logger, protected readonly ServiceLocator $storages)
3029
{
3130
}
3231

3332
protected function configureOptions(OptionsResolver $resolver): void
3433
{
35-
$this->configureFilesystemOption($resolver, 'filesystem');
34+
$resolver->setRequired('filesystem');
35+
$resolver->setAllowedTypes('filesystem', 'string');
3636
}
3737

3838
public function execute(ProcessState $state): void
3939
{
40-
$filesystem = $this->getFilesystem($state, 'filesystem');
40+
$filesystem = $this->storages->get($this->getOption($state, 'filesystem'));
4141
$filePath = $state->getInput();
4242

4343
try {

0 commit comments

Comments
 (0)