1 | <?php |
||
9 | class FileSelection |
||
10 | { |
||
11 | /** @var array */ |
||
12 | protected $includeFilesAndDirectories = []; |
||
13 | |||
14 | /** @var array */ |
||
15 | protected $excludeFilesAndDirectories = []; |
||
16 | |||
17 | /** |
||
18 | * @param array|string $includeFilesAndDirectories |
||
19 | * |
||
20 | * @return \Spatie\Backup\Tasks\Backup\FileSelection |
||
21 | */ |
||
22 | public static function create($includeFilesAndDirectories = []) |
||
26 | |||
27 | /** |
||
28 | * @param array|string $includeFilesAndDirectories |
||
29 | */ |
||
30 | 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) |
||
56 | |||
57 | /** |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getSelectedFiles() |
||
84 | |||
85 | /** |
||
86 | * Make a unique array of all files from a given array of files and directories. |
||
87 | * |
||
88 | * @param array $paths |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | protected function getAllFilesFromPaths(array $paths) |
||
93 | { |
||
94 | $paths = $this->checkForWildcardPaths($paths); |
||
95 | |||
96 | $allFiles = collect($paths) |
||
97 | ->filter(function ($path) { |
||
98 | return file_exists($path); |
||
99 | }) |
||
100 | |||
101 | ->map(function ($file) { |
||
102 | return realpath($file); |
||
103 | }) |
||
104 | |||
105 | ->reduce(function (Collection $filePaths, $path) { |
||
106 | if (is_dir($path)) { |
||
107 | return $filePaths->merge($this->getAllFilesFromDirectory($path)); |
||
108 | } |
||
109 | |||
110 | return $filePaths->push($path); |
||
111 | }, collect()) |
||
112 | |||
113 | ->unique() |
||
114 | ->toArray(); |
||
115 | |||
116 | return $allFiles; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Recursively get all the files within a given directory. |
||
121 | * |
||
122 | * @param string $directory |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | protected function getAllFilesFromDirectory($directory) |
||
142 | |||
143 | /** |
||
144 | * Check all paths in array for a wildcard (*) and build a new array from the results. |
||
145 | * |
||
146 | * @param $paths |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | private function checkForWildcardPaths($paths) |
||
160 | } |
||
161 |