Completed
Push — master ( fdf75e...b90538 )
by Freek
02:17
created

FileSelection::getAllFilesFromPaths()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 2
eloc 12
c 4
b 0
f 1
nc 1
nop 1
dl 0
loc 18
rs 9.4285
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
            echo 'empty';
75
            return collect();
76
        }
77
78
        var_dump($this->includeFilesAndDirectories->all());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->includeF...AndDirectories->all()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
79
80
        $filesToBeIncluded = $this->getAllFilesFromPaths($this->includeFilesAndDirectories);
81
82
        if ($this->excludeFilesAndDirectories->isEmpty()) {
83
            return $filesToBeIncluded;
84
        }
85
86
        return $filesToBeIncluded
87
            ->reject(function ($path) {
88
                return $this->excludeFilesAndDirectories
89
                    ->contains(function ($key, $excludedPath) use ($path) {
90
                        return starts_with($path, $excludedPath);
91
                    });
92
            })
93
            ->values();
94
    }
95
96
    /**
97
     * Make a unique array of all files from a given array of files and directories.
98
     *
99
     * @param \Illuminate\Support\Collection $paths
100
     *
101
     * @return \Illuminate\Support\Collection
102
     */
103
    protected function getAllFilesFromPaths(Collection $paths)
104
    {
105
        return $paths
106
            ->filter(function ($path) {
107
                return file_exists($path);
108
            })
109
            ->map(function ($file) {
110
                return realpath($file);
111
            })
112
            ->reduce(function (Collection $filePaths, $path) {
113
                if (is_dir($path)) {
114
                    return $filePaths->merge($this->getAllFilesFromDirectory($path));
115
                }
116
117
                return $filePaths->push($path);
118
            }, collect())
119
            ->unique();
120
    }
121
122
    /**
123
     * Recursively get all the files within a given directory.
124
     *
125
     * @param string $directory
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    protected function getAllFilesFromDirectory($directory)
130
    {
131
        $finder = (new Finder())
132
            ->ignoreDotFiles(false)
133
            ->ignoreVCS(false)
134
            ->files()
135
            ->in($directory);
136
137
        if ($this->shouldFollowLinks) {
138
            $finder->followLinks();
139
        }
140
141
        return collect(iterator_to_array($finder))
142
            ->map(function (SplFileInfo $fileInfo) {
143
                return $fileInfo->getPathname();
144
            })
145
            ->values();
146
    }
147
148
    /**
149
     * Fully expand paths, and reject non-existing paths.
150
     *
151
     * @param $paths
152
     *
153
     * @return \Illuminate\Support\Collection
154
     */
155
    protected function createPathCollection($paths)
156
    {
157
        return collect($paths)
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