1 | <?php |
||
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 = []): self |
||
26 | { |
||
27 | return new static($includeFilesAndDirectories); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param array|string $includeFilesAndDirectories |
||
32 | */ |
||
33 | public function __construct($includeFilesAndDirectories = []) |
||
34 | { |
||
35 | $this->includeFilesAndDirectories = collect($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): self |
||
48 | { |
||
49 | $this->excludeFilesAndDirectories = $this->excludeFilesAndDirectories->merge($this->sanitize($excludeFilesAndDirectories)); |
||
50 | |||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | public function shouldFollowLinks(bool $shouldFollowLinks): self |
||
55 | { |
||
56 | $this->shouldFollowLinks = $shouldFollowLinks; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return \Generator|string[] |
||
63 | */ |
||
64 | public function selectedFiles() |
||
65 | { |
||
66 | if ($this->includeFilesAndDirectories->isEmpty()) { |
||
67 | return []; |
||
68 | } |
||
69 | |||
70 | $finder = (new Finder()) |
||
71 | ->ignoreDotFiles(false) |
||
72 | ->ignoreVCS(false); |
||
73 | |||
74 | if ($this->shouldFollowLinks) { |
||
75 | $finder->followLinks(); |
||
76 | } |
||
77 | |||
78 | foreach ($this->includedFiles() as $includedFile) { |
||
79 | yield $includedFile; |
||
80 | } |
||
81 | |||
82 | if (! count($this->includedDirectories())) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $finder->in($this->includedDirectories()); |
||
87 | |||
88 | foreach ($finder->getIterator() as $file) { |
||
89 | if ($this->shouldExclude($file)) { |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | yield $file->getPathname(); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | protected function includedFiles(): array |
||
103 | |||
104 | protected function includedDirectories(): array |
||
110 | |||
111 | protected function shouldExclude(string $path): bool |
||
121 | |||
122 | /** |
||
123 | * @param string|array $paths |
||
124 | * |
||
125 | * @return \Illuminate\Support\Collection |
||
126 | */ |
||
127 | protected function sanitize($paths): Collection |
||
143 | } |
||
144 |