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 = []) |
||
29 | |||
30 | /** |
||
31 | * @param array|string $includeFilesAndDirectories |
||
32 | */ |
||
33 | public function __construct($includeFilesAndDirectories) |
||
38 | |||
39 | /** |
||
40 | * Do not included the given files and directories. |
||
41 | * |
||
42 | * @param array|string $excludeFilesAndDirectories |
||
43 | * |
||
44 | * @return \Spatie\Backup\Tasks\Backup\FileSelection |
||
45 | */ |
||
46 | public function excludeFilesFrom($excludeFilesAndDirectories) |
||
52 | |||
53 | /** |
||
54 | * Enable or disable the following of symlinks. |
||
55 | * |
||
56 | * @param bool $shouldFollowLinks |
||
57 | * |
||
58 | * @return \Spatie\Backup\Tasks\Backup\FileSelection |
||
59 | */ |
||
60 | public function shouldFollowLinks($shouldFollowLinks) |
||
66 | |||
67 | /** |
||
68 | * @return \Illuminate\Support\Collection |
||
69 | */ |
||
70 | public function getSelectedFiles() |
||
71 | { |
||
72 | if ($this->includeFilesAndDirectories->isEmpty()) { |
||
73 | return collect(); |
||
74 | } |
||
75 | |||
76 | $filesToBeIncluded = $this->getAllFilesFromPaths($this->includeFilesAndDirectories); |
||
77 | |||
78 | if ($this->excludeFilesAndDirectories->isEmpty()) { |
||
79 | return $filesToBeIncluded; |
||
80 | } |
||
81 | |||
82 | return $filesToBeIncluded |
||
83 | ->reject(function ($path) { |
||
84 | return $this->excludeFilesAndDirectories |
||
85 | ->contains(function ($key, $excludedPath) use ($path) { |
||
86 | return starts_with($path, $excludedPath); |
||
87 | }); |
||
88 | }) |
||
89 | ->values(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Make a unique array of all files from a given array of files and directories. |
||
94 | * |
||
95 | * @param \Illuminate\Support\Collection $paths |
||
96 | * |
||
97 | * @return \Illuminate\Support\Collection |
||
98 | */ |
||
99 | protected function getAllFilesFromPaths(Collection $paths) |
||
100 | { |
||
101 | return $paths |
||
102 | ->filter(function ($path) { |
||
103 | return file_exists($path); |
||
104 | }) |
||
105 | ->map(function ($file) { |
||
106 | return realpath($file); |
||
107 | }) |
||
108 | ->reduce(function (Collection $filePaths, $path) { |
||
109 | if (is_dir($path)) { |
||
110 | return $filePaths->merge($this->getAllFilesFromDirectory($path)); |
||
111 | } |
||
112 | |||
113 | return $filePaths->push($path); |
||
114 | }, collect()) |
||
115 | ->unique(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Recursively get all the files within a given directory. |
||
120 | * |
||
121 | * @param string $directory |
||
122 | * |
||
123 | * @return \Illuminate\Support\Collection |
||
124 | */ |
||
125 | protected function getAllFilesFromDirectory($directory) |
||
143 | |||
144 | /** |
||
145 | * Fully expand paths, and reject non-existing paths. |
||
146 | * |
||
147 | * @param $paths |
||
148 | * |
||
149 | * @return \Illuminate\Support\Collection |
||
150 | */ |
||
151 | protected function createPathCollection($paths) |
||
164 | } |
||
165 |