From 745db03fc31a5f87195b425615722b58cdf2e94b Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Fri, 20 Dec 2024 18:29:02 +0100 Subject: [PATCH 1/4] #155 Implement InputFileReaderTask, InputLineReaderTask and LineReaderTask --- src/Task/File/InputFileReaderTask.php | 31 ++++++++++++++ src/Task/File/InputLineReaderTask.php | 32 ++++++++++++++ src/Task/File/LineReaderTask.php | 62 +++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/Task/File/InputFileReaderTask.php create mode 100644 src/Task/File/InputLineReaderTask.php create mode 100644 src/Task/File/LineReaderTask.php diff --git a/src/Task/File/InputFileReaderTask.php b/src/Task/File/InputFileReaderTask.php new file mode 100644 index 00000000..06c41812 --- /dev/null +++ b/src/Task/File/InputFileReaderTask.php @@ -0,0 +1,31 @@ +getInput()) { + $options['filename'] = $state->getInput(); + } + + return $options; + } + + protected function configureOptions(OptionsResolver $resolver): void + { + parent::configureOptions($resolver); + $resolver->remove('filename'); + } +} diff --git a/src/Task/File/InputLineReaderTask.php b/src/Task/File/InputLineReaderTask.php new file mode 100644 index 00000000..ce6c0feb --- /dev/null +++ b/src/Task/File/InputLineReaderTask.php @@ -0,0 +1,32 @@ +getInput()) { + $options['filename'] = $state->getInput(); + } + + return $options; + } + + protected function configureOptions(OptionsResolver $resolver): void + { + parent::configureOptions($resolver); + $resolver->remove('filename'); + } +} diff --git a/src/Task/File/LineReaderTask.php b/src/Task/File/LineReaderTask.php new file mode 100644 index 00000000..6942b9c3 --- /dev/null +++ b/src/Task/File/LineReaderTask.php @@ -0,0 +1,62 @@ +getOptions($state); + $filename = $options['filename']; + + if ($this->file instanceof \SplFileObject + && $this->file->getPathname() !== $filename) { + $this->file = null; + } + + if (!$this->file instanceof \SplFileObject) { + if (!file_exists($filename)) { + throw new \UnexpectedValueException("File does not exist: '{$filename}'"); + } + + if (!is_readable($filename)) { + throw new \UnexpectedValueException("File is not readable: '{$filename}'"); + } + + $this->file = new \SplFileObject($filename); + $this->file->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); + $this->file->rewind(); + } + + $state->setOutput($this->file->current()); + $this->file->next(); + } + + public function next(ProcessState $state): bool + { + if (!$this->file instanceof \SplFileObject) { + throw new \LogicException('No File initialized'); + } + + return !$this->file->eof(); + } + + protected function configureOptions(OptionsResolver $resolver): void + { + $resolver->setRequired(['filename']); + $resolver->setAllowedTypes('filename', ['string']); + } +} From 75a89e5229b0c247096c896a4b76fe155442580d Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Thu, 2 Jan 2025 15:51:14 +0100 Subject: [PATCH 2/4] #155 Add docs for [Input][FileReader|LineReader|FolderBrowser]Tasks --- docs/index.md | 9 ++-- docs/reference/tasks/file_reader_task.md | 40 ++++++++++++++++ docs/reference/tasks/folder_browser_task.md | 43 +++++++++++++++++ .../reference/tasks/input_file_reader_task.md | 41 ++++++++++++++++ .../tasks/input_folder_browser_task.md | 47 +++++++++++++++++++ .../reference/tasks/input_line_reader_task.md | 42 +++++++++++++++++ docs/reference/tasks/line_reader_task.md | 41 ++++++++++++++++ 7 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 docs/reference/tasks/file_reader_task.md create mode 100644 docs/reference/tasks/folder_browser_task.md create mode 100644 docs/reference/tasks/input_file_reader_task.md create mode 100644 docs/reference/tasks/input_folder_browser_task.md create mode 100644 docs/reference/tasks/input_line_reader_task.md create mode 100644 docs/reference/tasks/line_reader_task.md diff --git a/docs/index.md b/docs/index.md index 96f54c07..3a469759 100644 --- a/docs/index.md +++ b/docs/index.md @@ -55,11 +55,14 @@ - [YamlWriterTask] - File - [FileMoverTask] - - [FileReaderTask] + - [FileReaderTask](reference/tasks/file_reader_task.md) - [FileRemoverTask] - [FileWriterTask] - - [FolderBrowserTask] - - [InputFolderBrowserTask] + - [FolderBrowserTask](reference/tasks/folder_browser_task.md) + - [InputFileReaderTask](reference/tasks/input_file_reader_task.md) + - [InputFolderBrowserTask](reference/tasks/input_folder_browser_task.md) + - [InputLineReaderTask](reference/tasks/input_line_reader_task.md) + - [LineReaderTask](reference/tasks/line_reader_task.md) - Flow manipulation - [AggregateIterableTask](reference/tasks/aggregate_iterable_task.md) - [InputAggregatorTask](reference/tasks/input_aggregator_task.md) diff --git a/docs/reference/tasks/file_reader_task.md b/docs/reference/tasks/file_reader_task.md new file mode 100644 index 00000000..e0097a13 --- /dev/null +++ b/docs/reference/tasks/file_reader_task.md @@ -0,0 +1,40 @@ +FileReaderTask +============= + +Reads a file and return raw content as a string + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\FileReaderTask` + +Accepted inputs +--------------- + +Input is ignored + +Possible outputs +---------------- + +`string`: raw content of the file. +Underlying method is [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php). + +Options +------- + +| Code | Type | Required | Default | Description | +|------------|----------|:---------:|----------|------------------------------------------| +| `filename` | `string` | **X** | | Path of the file to read from (absolute) | + +Example +------- + +```yaml +# Task configuration level +code: + service: '@CleverAge\ProcessBundle\Task\File\FileReaderTask' + options: + filename: 'path/to/file.txt' +``` + + diff --git a/docs/reference/tasks/folder_browser_task.md b/docs/reference/tasks/folder_browser_task.md new file mode 100644 index 00000000..003c3e7b --- /dev/null +++ b/docs/reference/tasks/folder_browser_task.md @@ -0,0 +1,43 @@ +FolderBrowserTask +============= + +Reads a folder and iterate on each file, returning absolute path as string. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\FolderBrowserTask` +* **Iterable task** + +Accepted inputs +--------------- + +Input is ignored + +Possible outputs +---------------- + +`string`: absolute path of the file. +Underlying method is [Symfony Finder component](https://symfony.com/doc/current/components/finder.html). + +Options +------- + +| Code | Type | Required | Default | Description | +|-------------------|-------------------------------|:---------:|---------------------------|------------------------------------| +| `folder_path` | `string` | **X** | | Path of the directory to read from | +| `name_pattern` | `null` or `string` or `array` | | null | Restrict files using a pattern | +| `empty_log_level` | `string` | | Psr\Log\LogLevel::WARNING | From Psr\Log\LogLevel constants | + +Example +------- + +```yaml +# Task configuration level +code: + service: '@CleverAge\ProcessBundle\Task\File\FolderBrowserTask' + options: + folder_path: '%kernel.project_dir%/var/data' +``` + + diff --git a/docs/reference/tasks/input_file_reader_task.md b/docs/reference/tasks/input_file_reader_task.md new file mode 100644 index 00000000..d960ba21 --- /dev/null +++ b/docs/reference/tasks/input_file_reader_task.md @@ -0,0 +1,41 @@ +InputFileReaderTask +============= + +Reads a file and return raw content as a string + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\InputFileReaderTask` + +Accepted inputs +--------------- + +`string`: file path + +Possible outputs +---------------- + +`string`: raw content of the file. +Underlying method is [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php). + +Options +------- + +None + +Example +------- + +```yaml +# Task configuration level +entry: + service: '@CleverAge\ProcessBundle\Task\File\FolderBrowserTask' + options: + folder_path: '%kernel.project_dir%/var/data' + outputs: read +read: + service: '@CleverAge\ProcessBundle\Task\File\InputFileReaderTask' +``` + + diff --git a/docs/reference/tasks/input_folder_browser_task.md b/docs/reference/tasks/input_folder_browser_task.md new file mode 100644 index 00000000..e95e2401 --- /dev/null +++ b/docs/reference/tasks/input_folder_browser_task.md @@ -0,0 +1,47 @@ +InputFolderBrowserTask +============= + +Reads a folder and iterate on each file, returning absolute path as string. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\InputFolderBrowserTask` +* **Iterable task** + +Accepted inputs +--------------- + +`string`: folder path + +Possible outputs +---------------- + +`string`: absolute path of the file. +Underlying method is [Symfony Finder component](https://symfony.com/doc/current/components/finder.html). + +Options +------- + +| Code | Type | Required | Default | Description | +|--------------------|----------|:---------:|---------|---------------------------------------| +| `base_folder_path` | `string` | | | Concatenated with input `folder_path` | + +Example +------- + +```yaml +# Task configuration level +entry: + service: '@CleverAge\ProcessBundle\Task\ConstantOutputTask' + options: + output: '/var/data' + outputs: directory +directory: + service: '@CleverAge\ProcessBundle\Task\File\InputFolderBrowserTask' + options: + base_folder_path: '%kernel.project_dir%' + outputs: read +``` + + diff --git a/docs/reference/tasks/input_line_reader_task.md b/docs/reference/tasks/input_line_reader_task.md new file mode 100644 index 00000000..da185453 --- /dev/null +++ b/docs/reference/tasks/input_line_reader_task.md @@ -0,0 +1,42 @@ +InputLineReaderTask +============= + +Reads a file and iterate on each line, returning content as string. Skips empty lines. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\InputLineReaderTask` +* **Iterable task** + +Accepted inputs +--------------- + +`string`: file path + +Possible outputs +---------------- + +`string`: foreach line, it will return content as string. +Underlying method is [SplFileObject](https://www.php.net/manual/en/class.splfileobject.php). + +Options +------- + +None + +Example +------- + +```yaml +# Task configuration level +entry: + service: '@CleverAge\ProcessBundle\Task\File\FolderBrowserTask' + options: + folder_path: '%kernel.project_dir%/var/data' + outputs: read +read: + service: '@CleverAge\ProcessBundle\Task\File\InputLineReaderTask' +``` + + diff --git a/docs/reference/tasks/line_reader_task.md b/docs/reference/tasks/line_reader_task.md new file mode 100644 index 00000000..8c5f2e4e --- /dev/null +++ b/docs/reference/tasks/line_reader_task.md @@ -0,0 +1,41 @@ +LineReaderTask +============= + +Reads a file and iterate on each line, returning content as string. Skips empty lines. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\LineReaderTask` +* **Iterable task** + +Accepted inputs +--------------- + +Input is ignored + +Possible outputs +---------------- + +`string`: foreach line, it will return content as string. +Underlying method is [SplFileObject](https://www.php.net/manual/en/class.splfileobject.php). + +Options +------- + +| Code | Type | Required | Default | Description | +|------------|----------|:---------:|----------|------------------------------------------| +| `filename` | `string` | **X** | | Path of the file to read from (absolute) | + +Example +------- + +```yaml +# Task configuration level +code: + service: '@CleverAge\ProcessBundle\Task\File\LineReaderTask' + options: + filename: 'path/to/file.txt' +``` + + From 55d4667532b227ec4b6e3cfda911fd3d6e9ed993 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Thu, 2 Jan 2025 15:58:16 +0100 Subject: [PATCH 3/4] #155 php-cs-fixer --- src/Task/File/InputFileReaderTask.php | 10 +++++++++- src/Task/File/InputLineReaderTask.php | 11 +++++++++-- src/Task/File/LineReaderTask.php | 10 +++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Task/File/InputFileReaderTask.php b/src/Task/File/InputFileReaderTask.php index 06c41812..5c638ff9 100644 --- a/src/Task/File/InputFileReaderTask.php +++ b/src/Task/File/InputFileReaderTask.php @@ -2,9 +2,17 @@ declare(strict_types=1); +/* + * This file is part of the CleverAge/ProcessBundle package. + * + * Copyright (c) Clever-Age + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace CleverAge\ProcessBundle\Task\File; -use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; use CleverAge\ProcessBundle\Model\ProcessState; use Symfony\Component\OptionsResolver\OptionsResolver; diff --git a/src/Task/File/InputLineReaderTask.php b/src/Task/File/InputLineReaderTask.php index ce6c0feb..6ae852b6 100644 --- a/src/Task/File/InputLineReaderTask.php +++ b/src/Task/File/InputLineReaderTask.php @@ -2,10 +2,17 @@ declare(strict_types=1); +/* + * This file is part of the CleverAge/ProcessBundle package. + * + * Copyright (c) Clever-Age + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace CleverAge\ProcessBundle\Task\File; -use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; -use CleverAge\ProcessBundle\Model\IterableTaskInterface; use CleverAge\ProcessBundle\Model\ProcessState; use Symfony\Component\OptionsResolver\OptionsResolver; diff --git a/src/Task/File/LineReaderTask.php b/src/Task/File/LineReaderTask.php index 6942b9c3..da103ab0 100644 --- a/src/Task/File/LineReaderTask.php +++ b/src/Task/File/LineReaderTask.php @@ -2,9 +2,17 @@ declare(strict_types=1); +/* + * This file is part of the CleverAge/ProcessBundle package. + * + * Copyright (c) Clever-Age + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace CleverAge\ProcessBundle\Task\File; -use CleverAge\ProcessBundle\Filesystem\CsvFile; use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; use CleverAge\ProcessBundle\Model\IterableTaskInterface; use CleverAge\ProcessBundle\Model\ProcessState; From f0480fa4d45774805481f2f57d740f49a85dfd53 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 8 Jan 2025 15:54:27 +0100 Subject: [PATCH 4/4] #155 Update FolderBrowserTask doc for name_pattern --- docs/reference/tasks/folder_browser_task.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/reference/tasks/folder_browser_task.md b/docs/reference/tasks/folder_browser_task.md index 003c3e7b..3dcc44ef 100644 --- a/docs/reference/tasks/folder_browser_task.md +++ b/docs/reference/tasks/folder_browser_task.md @@ -23,11 +23,11 @@ Underlying method is [Symfony Finder component](https://symfony.com/doc/current/ Options ------- -| Code | Type | Required | Default | Description | -|-------------------|-------------------------------|:---------:|---------------------------|------------------------------------| -| `folder_path` | `string` | **X** | | Path of the directory to read from | -| `name_pattern` | `null` or `string` or `array` | | null | Restrict files using a pattern | -| `empty_log_level` | `string` | | Psr\Log\LogLevel::WARNING | From Psr\Log\LogLevel constants | +| Code | Type | Required | Default | Description | +|-------------------|-----------------------------|:---------:|---------------------------|----------------------------------------------------------------------------------------| +| `folder_path` | `string` | **X** | | Path of the directory to read from | +| `name_pattern` | `null`, `string` or `array` | | null | Restrict files using a pattern (a regexp, a glob, or a string) or an array of patterns | +| `empty_log_level` | `string` | | Psr\Log\LogLevel::WARNING | From Psr\Log\LogLevel constants | Example -------