Repository navigation
Expand file tree
/
Copy pathFolderBrowserTask.php
More file actions
147 lines (130 loc) · 4.49 KB
/
Copy pathFolderBrowserTask.php
File metadata and controls
147 lines (130 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
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\Exception\LogicException;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\IterableTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Browse a folder an iterate each file for output.
*/
class FolderBrowserTask extends AbstractConfigurableTask implements IterableTaskInterface
{
/**
* @var \Iterator<string, SplFileInfo>|array<SplFileInfo>|null
*/
protected \Iterator|array|null $files = null;
public function __construct(
protected LoggerInterface $logger,
) {
}
public function execute(ProcessState $state): void
{
$options = $this->getOptions($state);
if (null === $this->files) {
$finder = new Finder();
$finder->files();
if ($options['name_pattern']) {
$finder->name($options['name_pattern']);
}
$this->files = $finder->in($options['folder_path'])->sortByName()->getIterator();
$this->files->rewind();
}
$files = $this->getFilesIterator();
if (!$files->valid()) {
$this->logger->log($options['empty_log_level'], "No item found in path {$options['folder_path']}");
$state->setSkipped(true);
$state->setErrorOutput($options['folder_path']);
$this->files = null;
return;
}
/** @var SplFileInfo $fileInfo */
$fileInfo = $files->current();
$filePath = $fileInfo->getPathname();
$state->addErrorContextValue('current_file_path', $filePath);
$state->setOutput($filePath);
}
/**
* Moves the internal pointer to the next element,
* return true if the task has a next element
* return false if the task has terminated it's iteration.
*/
public function next(ProcessState $state): bool
{
if (!$this->files) {
return false;
}
$files = $this->getFilesIterator();
$files->next();
$state->removeErrorContext('current_file_path');
if (!$files->valid()) {
// Reset the iterator to allow the following iteration
$this->files = null;
return false;
}
return true;
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['folder_path']);
$resolver->setAllowedTypes('folder_path', ['string']);
$resolver->setNormalizer(
'folder_path',
static function (Options $options, $value) {
if (!is_dir($value)) {
throw new InvalidConfigurationException("Folder path does not exists or is not a folder: '{$value}'");
}
if (!is_readable($value)) {
throw new InvalidConfigurationException("Folder path is not readable: '{$value}'");
}
return $value;
}
);
$resolver->setDefaults([
'name_pattern' => null,
'empty_log_level' => LogLevel::WARNING,
]);
$resolver->setAllowedTypes('name_pattern', ['null', 'string', 'array']);
$resolver->setAllowedValues(
'empty_log_level',
[
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
]
);
}
/**
* @return \Iterator<SplFileInfo>
*/
private function getFilesIterator(): \Iterator
{
if (\is_array($this->files)) {
$this->files = new \ArrayIterator($this->files);
}
if (!$this->files instanceof \Iterator) {
throw new LogicException('No folder browsed');
}
return $this->files;
}
}