1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Export\Jobs; |
4
|
|
|
|
5
|
|
|
use RecursiveIteratorIterator; |
6
|
|
|
use RecursiveDirectoryIterator; |
7
|
|
|
use Spatie\Export\Destination; |
8
|
|
|
|
9
|
|
|
class IncludeFiles |
10
|
|
|
{ |
11
|
|
|
/** @var string[] */ |
12
|
|
|
protected $includeFiles; |
13
|
|
|
|
14
|
|
|
/** @var string[] */ |
15
|
|
|
protected $excludeFilePatterns; |
16
|
|
|
|
17
|
|
|
public function __construct(array $includeFiles, array $excludeFilePatterns) |
18
|
|
|
{ |
19
|
|
|
$this->includeFiles = $includeFiles; |
20
|
|
|
$this->excludeFilePatterns = $excludeFilePatterns; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function handle(Destination $destination) |
24
|
|
|
{ |
25
|
|
|
foreach ($this->includeFiles as $source => $target) { |
26
|
|
|
if (is_file($source)) { |
27
|
|
|
$this->exportIncludedFile($source, $target, $destination); |
28
|
|
|
} else { |
29
|
|
|
$this->exportIncludedDirectory($source, $target, $destination); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function exportIncludedFile(string $source, string $target, Destination $destination) |
35
|
|
|
{ |
36
|
|
|
if ($this->shouldExclude($source)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$target = '/'.ltrim($target, '/'); |
41
|
|
|
|
42
|
|
|
$destination->write($target, file_get_contents($source)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function exportIncludedDirectory(string $source, string $target, Destination $destination) |
46
|
|
|
{ |
47
|
|
|
$iterator = new RecursiveIteratorIterator( |
48
|
|
|
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
49
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
foreach ($iterator as $item) { |
53
|
|
|
if ($item->isDir()) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->exportIncludedFile( |
58
|
|
|
$item->getPathname(), |
59
|
|
|
$target.'/'.$iterator->getSubPathName(), |
60
|
|
|
$destination |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function shouldExclude(string $source): bool |
66
|
|
|
{ |
67
|
|
|
foreach ($this->excludeFilePatterns as $pattern) { |
68
|
|
|
if (preg_match($pattern, $source)) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|