|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Export; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Spatie\Export\Jobs\CrawlSite; |
|
8
|
|
|
use Spatie\Export\Jobs\ExportPath; |
|
9
|
|
|
use Spatie\Export\Jobs\IncludeFile; |
|
10
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
|
11
|
|
|
use Spatie\Export\Jobs\CleanDestination; |
|
12
|
|
|
|
|
13
|
|
|
class Exporter |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var \Illuminate\Contracts\Bus\Dispatcher */ |
|
16
|
|
|
protected $dispatcher; |
|
17
|
|
|
|
|
18
|
|
|
/** @var UrlGenerator */ |
|
19
|
|
|
protected $urlGenerator; |
|
20
|
|
|
|
|
21
|
|
|
/** @var boolean */ |
|
22
|
|
|
protected $cleanBeforeExport = false; |
|
23
|
|
|
|
|
24
|
|
|
/** @var boolean */ |
|
25
|
|
|
protected $crawl = false; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string[] */ |
|
28
|
|
|
protected $paths = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string[] */ |
|
31
|
|
|
protected $includeFiles = []; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string[] */ |
|
34
|
|
|
protected $excludeFilePatterns = []; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(Dispatcher $dispatcher, UrlGenerator $urlGenerator) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->dispatcher = $dispatcher; |
|
39
|
|
|
$this->urlGenerator = $urlGenerator; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function cleanBeforeExport(bool $cleanBeforeExport): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->cleanBeforeExport = $cleanBeforeExport; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function crawl(bool $crawl): self |
|
50
|
|
|
{ |
|
51
|
|
|
$this->crawl = $crawl; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function paths(...$paths): self |
|
57
|
|
|
{ |
|
58
|
|
|
$paths = is_array($paths[0]) ? $paths[0] : $paths; |
|
59
|
|
|
|
|
60
|
|
|
$this->paths = array_merge($this->paths, $paths); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function urls(...$urls): self |
|
66
|
|
|
{ |
|
67
|
|
|
$urls = is_array($urls[0]) ? $urls[0] : $urls; |
|
68
|
|
|
|
|
69
|
|
|
$this->paths(array_map(function (string $url): string { |
|
70
|
|
|
return Str::replaceFirst($this->urlGenerator->to('/'), '', $url); |
|
71
|
|
|
}, $urls)); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function includeFiles(array $includeFiles): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->includeFiles = array_merge($this->includeFiles, $includeFiles); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function excludeFilePatterns(array $excludeFilePatterns): self |
|
84
|
|
|
{ |
|
85
|
|
|
$this->excludeFilePatterns = array_merge($this->excludeFilePatterns, $excludeFilePatterns); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function export() |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->cleanBeforeExport) { |
|
93
|
|
|
$this->dispatcher->dispatchNow( |
|
94
|
|
|
new CleanDestination() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($this->crawl) { |
|
99
|
|
|
$this->dispatcher->dispatchNow( |
|
100
|
|
|
new CrawlSite() |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->paths as $path) { |
|
105
|
|
|
$this->dispatcher->dispatchNow( |
|
106
|
|
|
new ExportPath($path) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
foreach ($this->includeFiles as $source => $target) { |
|
111
|
|
|
$this->dispatcher->dispatchNow( |
|
112
|
|
|
new IncludeFile($source, $target, $this->excludeFilePatterns) |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..