Skip to content

Commit f896508

Browse files
#14 Update RemoveFileTask to work with file_pattern or input. Update documentation.
1 parent 6954b40 commit f896508

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

Diff for: docs/reference/tasks/01-FileFetchTask.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ If the option `file_pattern` is not set the input is used as strict filename(s)
1717

1818
If input is set but not corresponding as any file into `source_filesystem` task failed with UnableToReadFile exception.
1919

20-
If FileFetchTask is the fisrt task of you process and you wan to use input, don't forgive to set the `entry_point` task name at process level
20+
If FileFetchTask is the first task of you process and you want to use input, don't forgive to set the `entry_point` task name at process level
2121

2222
Possible outputs
2323
----------------
@@ -27,12 +27,12 @@ Filename of copied file.
2727
Options
2828
-------
2929

30-
| Code | Type | Required | Default | Description |
31-
|--------------------------|:----------:|:---------:|:---------:|-----------------------------------------------------------------------------------------------------------------------------------------------|
32-
| `source_filesystem` | `string` | **X** | | The source flysystem/storage.<br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
33-
| `destination_filesystem` | `string` | **X** | | The source flysystem/storage.<br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
34-
| `file_pattern` | `string` | | null | The file_parttern used in preg_match to match into `source_filesystem` list of files. If not set try to use input as strict filename to match |
35-
| `remove_source` | `bool` | | false | If true delete source file after copy |
30+
| Code | Type | Required | Default | Description |
31+
|--------------------------|:----------:|:---------:|:---------:|----------------------------------------------------------------------------------------------------------------------------------------------|
32+
| `source_filesystem` | `string` | **X** | | The source flysystem/storage.<br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
33+
| `destination_filesystem` | `string` | **X** | | The source flysystem/storage.<br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
34+
| `file_pattern` | `string` | | null | The file_pattern used in preg_match to match into `source_filesystem` list of files. If not set try to use input as strict filename to match |
35+
| `remove_source` | `bool` | | false | If true delete source file after copy |
3636

3737

3838
Examples

Diff for: docs/reference/tasks/03-RemoveFileTask.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Accepted inputs
1313

1414
The filename of the file to remove on `filesystem`.
1515

16+
If the option `file_pattern` is not set the input is used as strict filename(s) to match.
17+
1618
When filename is deleted add a info log.
1719

1820
If filename not found or cannot be deleted on `filesystem` add a warning log.
@@ -25,22 +27,38 @@ None
2527
Options
2628
-------
2729

28-
| Code | Type | Required | Default | Description |
29-
|--------------|:----------:|:----------:|:---------:|-------------------------------------------------------------------------------------------------------------|
30-
| `filesystem` | `string` | **X** | | The source flysystem/storage. <br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
30+
| Code | Type | Required | Default | Description |
31+
|----------------|:----------:|:--------:|:---------:|----------------------------------------------------------------------------------------------------------------------------------------------|
32+
| `filesystem` | `string` | **X** | | The source flysystem/storage. <br/>See config/packages/flysystem.yaml to see configured flysystem/storages. |
33+
| `file_pattern` | `string` | | null | The file_pattern used in preg_match to match into `source_filesystem` list of files. If not set try to use input as strict filename to match |
34+
3135

3236
Examples
3337
--------
34-
* Simple process to remove a file on 'storage.source' via <br>```bin/console cleverage:process:execute my_custom_process --input=foobar.csv -vv```
38+
* Simple process to remove a file on 'storage.source' via <br>```bin/console cleverage:process:execute input_process --input=foobar.csv -vv```
3539
- see config/packages/flysystem.yaml to see configured flysystems/storages.
3640
- remove file with name passed as input
3741
```yaml
3842
#
39-
my_custom_process:
43+
input_process:
4044
entry_point: remove_from_input
4145
tasks:
4246
remove_from_input:
43-
service: '@CleverAge\FlysystemProcessBundle\Task\FileFetchTask'
47+
service: '@CleverAge\FlysystemProcessBundle\Task\RemoveFileTask'
48+
options:
49+
filesystem: 'storage.source'
50+
```
51+
52+
* Simple process to remove a file on 'storage.source' via <br>```bin/console cleverage:process:execute pattern_process -vv```
53+
- see config/packages/flysystem.yaml to see configured flysystems/storages.
54+
- remove files filtered by file_pattern
55+
```yaml
56+
#
57+
pattern_process:
58+
tasks:
59+
remove_from_file_pattern:
60+
service: '@CleverAge\FlysystemProcessBundle\Task\RemoveFileTask'
4461
options:
4562
filesystem: 'storage.source'
63+
file_pattern: '/.csv$/'
4664
```

Diff for: src/Task/RemoveFileTask.php

+29-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
use Symfony\Component\OptionsResolver\OptionsResolver;
2323

2424
/**
25-
* Remove a file from a filesystem.
25+
* Remove files from a filesystem.
2626
*/
2727
class RemoveFileTask extends AbstractConfigurableTask
2828
{
29+
private FilesystemOperator $filesystem;
30+
2931
/**
3032
* @param ServiceLocator<FilesystemOperator> $storages
3133
*/
@@ -37,18 +39,40 @@ protected function configureOptions(OptionsResolver $resolver): void
3739
{
3840
$resolver->setRequired('filesystem');
3941
$resolver->setAllowedTypes('filesystem', 'string');
42+
43+
$resolver->setDefault('file_pattern', null);
44+
$resolver->setAllowedTypes('file_pattern', ['string', 'null']);
4045
}
4146

4247
public function execute(ProcessState $state): void
4348
{
4449
/** @var string $filesystemOption */
4550
$filesystemOption = $this->getOption($state, 'filesystem');
46-
$filesystem = $this->storages->get($filesystemOption);
47-
/** @var string $filePath */
48-
$filePath = $state->getInput();
51+
$this->filesystem = $this->storages->get($filesystemOption);
52+
53+
/** @var ?string $filePattern */
54+
$filePattern = $this->getOption($state, 'file_pattern');
55+
if ($filePattern) {
56+
foreach ($this->filesystem->listContents('/') as $file) {
57+
if ('file' === $file->type() && preg_match($filePattern, $file->path())) {
58+
$this->deleteFile($file->path());
59+
}
60+
}
61+
} else {
62+
/** @var ?string $input */
63+
$input = $state->getInput();
64+
if (!$input) {
65+
throw new \UnexpectedValueException('No pattern neither input provided for the Task');
66+
}
4967

68+
$this->deleteFile($input);
69+
}
70+
}
71+
72+
private function deleteFile(string $filePath): void
73+
{
5074
try {
51-
$filesystem->delete($filePath);
75+
$this->filesystem->delete($filePath);
5276
$result = true;
5377
} catch (FilesystemException) {
5478
$result = false;

0 commit comments

Comments
 (0)