Completed
Push — master ( be0fee...a8e059 )
by Freek
17:05 queued 12:18
created

FileSelection::expandWildCardPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 9.6666
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 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 = [])
23
    {
24
        return new static($includeFilesAndDirectories);
25
    }
26
27
    /**
28
     * @param array|string $includeFilesAndDirectories
29
     */
30
    public function __construct($includeFilesAndDirectories)
31
    {
32
        if (!is_array($includeFilesAndDirectories)) {
33
            $includeFilesAndDirectories = [$includeFilesAndDirectories];
34
        }
35
36
        $this->includeFilesAndDirectories = $includeFilesAndDirectories;
37
    }
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)
47
    {
48
        if (!is_array($excludeFilesAndDirectories)) {
49
            $excludeFilesAndDirectories = [$excludeFilesAndDirectories];
50
        }
51
52
        $this->excludeFilesAndDirectories = $excludeFilesAndDirectories;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getSelectedFiles()
61
    {
62
        if (count($this->includeFilesAndDirectories) === 0) {
63
            return [];
64
        }
65
66
        $filesToBeIncluded = $this->getAllFilesFromPaths($this->includeFilesAndDirectories);
67
68
        if (count($this->excludeFilesAndDirectories) === 0) {
69
            return $filesToBeIncluded;
70
        }
71
72
        $filesToBeExcluded = $this->getAllFilesFromPaths($this->excludeFilesAndDirectories);
73
74
        $selectedFiles = collect($filesToBeIncluded)
75
            ->filter(function ($file) use ($filesToBeExcluded) {
76
                return !in_array($file, $filesToBeExcluded);
77
            })
78
            ->toArray();
79
80
        $selectedFiles = array_values($selectedFiles);
81
82
        return $selectedFiles;
83
    }
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->expandWildCardPaths($paths);
95
96
        $allFiles = collect($paths)
97
            ->filter(function ($path) {
98
                return file_exists($path);
99
            })
100
            ->map(function ($file) {
101
                return realpath($file);
102
            })
103
            ->reduce(function (Collection $filePaths, $path) {
104
                if (is_dir($path)) {
105
                    return $filePaths->merge($this->getAllFilesFromDirectory($path));
106
                }
107
108
                return $filePaths->push($path);
109
            }, collect())
110
            ->unique()
111
            ->toArray();
112
113
        return $allFiles;
114
    }
115
116
    /**
117
     * Recursively get all the files within a given directory.
118
     *
119
     * @param string $directory
120
     *
121
     * @return array
122
     */
123
    protected function getAllFilesFromDirectory($directory)
124
    {
125
        $finder = (new Finder())
126
            ->ignoreDotFiles(false)
127
            ->ignoreVCS(false)
128
            ->files()
129
            ->in($directory);
130
131
        $filePaths = array_map(function (SplFileInfo $fileInfo) {
132
            return $fileInfo->getPathname();
133
        }, iterator_to_array($finder));
134
135
        $filePaths = array_values($filePaths);
136
137
        return $filePaths;
138
    }
139
140
    /**
141
     * Check all paths in array for a wildcard (*) and build a new array from the results.
142
     *
143
     * @param array $paths
144
     *
145
     * @return array
146
     */
147
    protected function expandWildCardPaths(array $paths)
148
    {
149
        return collect($paths)
150
            ->map(function ($path) {
151
                return glob($path);
152
            })
153
            ->flatten()
154
            ->toArray();
155
    }
156
}
157