|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Darkilliant\ProcessBundle\Step; |
|
4
|
|
|
|
|
5
|
|
|
use Darkilliant\ProcessBundle\LoopStateMarker\FileLoopStateMarker; |
|
6
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
class FileFinderIteratorStep extends AbstractConfigurableStep implements IterableStepInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $count = 1; |
|
13
|
|
|
private $progress = 1; |
|
14
|
|
|
/** @var FileLoopStateMarker */ |
|
15
|
|
|
private $loopStateMarker; |
|
16
|
|
|
/** @var Finder */ |
|
17
|
|
|
private $finder; |
|
18
|
|
|
|
|
19
|
7 |
|
public function __construct(FileLoopStateMarker $loopStateMarker, Finder $finder = null) |
|
20
|
|
|
{ |
|
21
|
7 |
|
$this->loopStateMarker = $loopStateMarker; |
|
22
|
7 |
|
$this->finder = $finder; |
|
23
|
7 |
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver |
|
26
|
|
|
{ |
|
27
|
1 |
|
$resolver->setRequired( |
|
28
|
1 |
|
['in', 'recursive', 'depth', 'name', 'date', 'track_loop_state', 'track_loop_state_remove_on_success'] |
|
29
|
|
|
); |
|
30
|
1 |
|
$resolver->setDefault('recursive', true); |
|
31
|
1 |
|
$resolver->setDefault('depth', null); |
|
32
|
1 |
|
$resolver->setDefault('name', null); |
|
33
|
1 |
|
$resolver->setDefault('date', null); |
|
34
|
1 |
|
$resolver->setDefault('track_loop_state', false); |
|
35
|
1 |
|
$resolver->setAllowedTypes('track_loop_state', 'bool'); |
|
36
|
1 |
|
$resolver->setDefault('track_loop_state_remove_on_success', false); |
|
37
|
|
|
|
|
38
|
1 |
|
return parent::configureOptionResolver($resolver); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
public function execute(ProcessState $state) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$finder = $this->getFinder(); |
|
44
|
|
|
|
|
45
|
2 |
|
$finder->files()->in($state->getOptions()['in']); |
|
46
|
|
|
|
|
47
|
2 |
|
if (!$state->getOptions()['recursive']) { |
|
48
|
2 |
|
$finder->depth('< 1'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
if ($state->getOptions()['depth']) { |
|
52
|
2 |
|
$depth = (array) $state->getOptions()['depth']; |
|
53
|
|
|
|
|
54
|
2 |
|
foreach ($depth as $eachDepth) { |
|
55
|
2 |
|
$finder->depth($eachDepth); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if ($state->getOptions()['name']) { |
|
60
|
2 |
|
$nameFilder = (array) $state->getOptions()['name']; |
|
61
|
|
|
|
|
62
|
2 |
|
foreach ($nameFilder as $nameFilderDepth) { |
|
63
|
2 |
|
$finder->name($nameFilderDepth); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
if ($state->getOptions()['date']) { |
|
68
|
2 |
|
$dateFilder = (array) $state->getOptions()['date']; |
|
69
|
|
|
|
|
70
|
2 |
|
foreach ($dateFilder as $dateFilderDepth) { |
|
71
|
2 |
|
$finder->date($dateFilderDepth); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$finder->exclude('_processing'); |
|
76
|
|
|
|
|
77
|
2 |
|
$state->setIterator($this->each($finder)); |
|
78
|
|
|
|
|
79
|
2 |
|
$this->count = $finder->count(); |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function next(ProcessState $state) |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var \SplFileInfo $current */ |
|
85
|
1 |
|
$current = $state->getIterator()->current(); |
|
86
|
|
|
|
|
87
|
1 |
|
$state->setData($current->getPathname()); |
|
88
|
1 |
|
$state->setContext('file_finder_current', $current->getPathname()); |
|
89
|
|
|
|
|
90
|
1 |
|
$this->loopStateMarker->onStartLoop($state); |
|
91
|
|
|
|
|
92
|
1 |
|
$state->getIterator()->next(); |
|
93
|
|
|
|
|
94
|
1 |
|
++$this->progress; |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function valid(ProcessState $state) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $state->getIterator()->valid(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function getProgress(ProcessState $state) |
|
103
|
|
|
{ |
|
104
|
1 |
|
return $this->progress; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function count(ProcessState $state) |
|
108
|
|
|
{ |
|
109
|
1 |
|
return $this->count; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function onSuccessLoop(ProcessState $state) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->loopStateMarker->onSuccessLoop($state); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function onFailedLoop(ProcessState $state) |
|
118
|
|
|
{ |
|
119
|
1 |
|
$this->loopStateMarker->onFailedLoop($state); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
private function getFinder() |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->finder ?: new Finder(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
private function each(Finder $finder): \Traversable |
|
128
|
|
|
{ |
|
129
|
1 |
|
foreach ($finder as $file) { |
|
130
|
1 |
|
yield $file; |
|
131
|
|
|
} |
|
132
|
1 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|