1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Backup; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
8
|
|
|
|
9
|
|
|
class FileSelection |
10
|
|
|
{ |
11
|
|
|
/** @var \Illuminate\Support\Collection */ |
12
|
|
|
protected $includeFilesAndDirectories; |
13
|
|
|
|
14
|
|
|
/** @var \Illuminate\Support\Collection */ |
15
|
|
|
protected $excludeFilesAndDirectories; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
protected $shouldFollowLinks = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array|string $includeFilesAndDirectories |
22
|
|
|
* |
23
|
|
|
* @return \Spatie\Backup\Tasks\Backup\FileSelection |
24
|
|
|
*/ |
25
|
|
|
public static function create($includeFilesAndDirectories = []) |
26
|
|
|
{ |
27
|
|
|
return new static($includeFilesAndDirectories); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array|string $includeFilesAndDirectories |
32
|
|
|
*/ |
33
|
|
|
public function __construct($includeFilesAndDirectories) |
34
|
|
|
{ |
35
|
|
|
$this->includeFilesAndDirectories = $this->createPathCollection($includeFilesAndDirectories); |
36
|
|
|
|
37
|
|
|
$this->excludeFilesAndDirectories = collect(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Do not included the given files and directories. |
42
|
|
|
* |
43
|
|
|
* @param array|string $excludeFilesAndDirectories |
44
|
|
|
* |
45
|
|
|
* @return \Spatie\Backup\Tasks\Backup\FileSelection |
46
|
|
|
*/ |
47
|
|
|
public function excludeFilesFrom($excludeFilesAndDirectories) |
48
|
|
|
{ |
49
|
|
|
$this->excludeFilesAndDirectories = $this->createPathCollection($excludeFilesAndDirectories); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Enable or disable the following of symlinks. |
56
|
|
|
* |
57
|
|
|
* @param bool $shouldFollowLinks |
58
|
|
|
* |
59
|
|
|
* @return \Spatie\Backup\Tasks\Backup\FileSelection |
60
|
|
|
*/ |
61
|
|
|
public function shouldFollowLinks($shouldFollowLinks) |
62
|
|
|
{ |
63
|
|
|
$this->shouldFollowLinks = $shouldFollowLinks; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \Illuminate\Support\Collection |
70
|
|
|
*/ |
71
|
|
|
public function getSelectedFiles() |
72
|
|
|
{ |
73
|
|
|
if ($this->includeFilesAndDirectories->isEmpty()) { |
74
|
|
|
return collect(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$filesToBeIncluded = $this->getAllFilesFromPaths($this->includeFilesAndDirectories); |
78
|
|
|
|
79
|
|
|
if ($this->excludeFilesAndDirectories->isEmpty()) { |
80
|
|
|
return $filesToBeIncluded; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $filesToBeIncluded |
84
|
|
|
->reject(function ($path) { |
85
|
|
|
return $this->excludeFilesAndDirectories |
86
|
|
|
->contains(function ($key, $excludedPath) use ($path) { |
87
|
|
|
return starts_with($path, $excludedPath); |
88
|
|
|
}); |
89
|
|
|
}) |
90
|
|
|
->values(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Make a unique array of all files from a given array of files and directories. |
95
|
|
|
* |
96
|
|
|
* @param \Illuminate\Support\Collection $paths |
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\Support\Collection |
99
|
|
|
*/ |
100
|
|
|
protected function getAllFilesFromPaths(Collection $paths) |
101
|
|
|
{ |
102
|
|
|
return $paths |
103
|
|
|
->filter(function ($path) { |
104
|
|
|
return file_exists($path); |
105
|
|
|
}) |
106
|
|
|
->map(function ($file) { |
107
|
|
|
return realpath($file); |
108
|
|
|
}) |
109
|
|
|
->reduce(function (Collection $filePaths, $path) { |
110
|
|
|
if (is_dir($path)) { |
111
|
|
|
return $filePaths->merge($this->getAllFilesFromDirectory($path)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $filePaths->push($path); |
115
|
|
|
}, collect()) |
116
|
|
|
->unique(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Recursively get all the files within a given directory. |
121
|
|
|
* |
122
|
|
|
* @param string $directory |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Support\Collection |
125
|
|
|
*/ |
126
|
|
|
protected function getAllFilesFromDirectory($directory) |
127
|
|
|
{ |
128
|
|
|
$finder = (new Finder()) |
129
|
|
|
->ignoreDotFiles(false) |
130
|
|
|
->ignoreVCS(false) |
131
|
|
|
->files() |
132
|
|
|
->in($directory); |
133
|
|
|
|
134
|
|
|
if ($this->shouldFollowLinks) { |
135
|
|
|
$finder->followLinks(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return collect(iterator_to_array($finder)) |
139
|
|
|
->map(function (SplFileInfo $fileInfo) { |
140
|
|
|
return $fileInfo->getPathname(); |
141
|
|
|
}) |
142
|
|
|
->values(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fully expand paths, and reject non-existing paths. |
147
|
|
|
* |
148
|
|
|
* @param $paths |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Support\Collection |
151
|
|
|
*/ |
152
|
|
|
protected function createPathCollection($paths) |
153
|
|
|
{ |
154
|
|
|
return collect($paths) |
155
|
|
|
->reject(function ($path) { |
156
|
|
|
return $path == ''; |
157
|
|
|
}) |
158
|
|
|
->flatMap(function ($path) { |
159
|
|
|
return glob($path); |
160
|
|
|
}) |
161
|
|
|
->map(function ($path) { |
162
|
|
|
return realpath($path); |
163
|
|
|
}) |
164
|
|
|
->reject(function ($path) { |
165
|
|
|
return $path === false; |
166
|
|
|
}); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|