1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\FileOperations; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This file contains all functionality that has to do with file operations (such as iterators) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use unreal4u\Dummy\Logger; |
13
|
|
|
use unreal4u\FileOperations\Filters\CreationTimeFilterIterator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Constructs a list of files (filterable with iterator) according to given input |
17
|
|
|
*/ |
18
|
|
|
abstract class FileSelection implements FileActionInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Object container |
22
|
|
|
* @var \Iterator |
23
|
|
|
*/ |
24
|
|
|
protected $iterator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Whether we are in test mode or not |
28
|
|
|
* |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
protected $isTestMode = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Holds all options |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $options = [ |
44
|
|
|
'maxAge' => 0, |
45
|
|
|
'pattern' => '', |
46
|
|
|
'recursive' => true, |
47
|
|
|
'maxDepth' => -1, |
48
|
|
|
'includeBrokenSymlink' => true, |
49
|
|
|
// 'Filters' => [Filters\creationTimeFilterIterator], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $testMode Whether to enable test mode. Defaults to false |
54
|
|
|
* @param LoggerInterface|null $logger |
55
|
|
|
*/ |
56
|
|
|
public function __construct(bool $testMode = false, LoggerInterface $logger = null) |
57
|
|
|
{ |
58
|
|
|
$this->isTestMode = $testMode; |
59
|
|
|
if ($logger === null) { |
60
|
|
|
$logger = new Logger(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->logger = $logger; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the defaults |
68
|
|
|
* |
69
|
|
|
* @param array $options |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
private function setDefaults(array $options = []): self |
73
|
|
|
{ |
74
|
|
|
$this->options = $options + $this->options; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initializes the iterator and retrieves list of files |
80
|
|
|
* |
81
|
|
|
* @param string $path |
82
|
|
|
* The path we want to check |
83
|
|
|
* @param array $options |
84
|
|
|
* Array with options, for now these are 'pattern', 'maxAge', 'recursive' and 'includeBrokenSymlink' |
85
|
|
|
* @return FileActionInterface Returns same object for easy method concatenation |
86
|
|
|
*/ |
87
|
|
|
public function constructFileList(string $path, array $options = []): FileActionInterface |
88
|
|
|
{ |
89
|
|
|
$this->setDefaults($options); |
90
|
|
|
|
91
|
|
|
if ($this->options['recursive'] === true) { |
92
|
|
|
$this->iterator = new \RecursiveIteratorIterator( |
93
|
|
|
new \RecursiveDirectoryIterator( |
94
|
|
|
$path, |
95
|
|
|
\FilesystemIterator::SKIP_DOTS | \RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
96
|
|
|
), |
97
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST |
98
|
|
|
); |
99
|
|
|
$this->iterator->setMaxDepth($this->options['maxDepth']); |
100
|
|
|
} else { |
101
|
|
|
$this->iterator = new \IteratorIterator(new \directoryIterator($path)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->options['pattern'] !== '') { |
105
|
|
|
$this->iterator = new \RegexIterator($this->iterator, $this->options['pattern']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/*if (! empty($this->_options['includeBrokenSymlink'])) { |
109
|
|
|
$this->_iterator = new Filters\symlinksFilterIterator($this->_iterator, true); |
110
|
|
|
}*/ |
111
|
|
|
|
112
|
|
|
if ($this->options['maxAge'] !== 0) { |
113
|
|
|
$this->iterator = new CreationTimeFilterIterator($this->iterator, $this->options['maxAge']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates a JSON string with all filenames, no content, just filenames |
121
|
|
|
*/ |
122
|
|
|
public function __toString(): string |
123
|
|
|
{ |
124
|
|
|
$result = []; |
125
|
|
|
|
126
|
|
|
if (!empty($this->iterator)) { |
127
|
|
|
foreach ($this->iterator as $file) { |
128
|
|
|
$result[] = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return \json_encode($result); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|