1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Export; |
4
|
|
|
|
5
|
|
|
use Spatie\Crawler\Crawler; |
6
|
|
|
use RecursiveIteratorIterator; |
7
|
|
|
use RecursiveDirectoryIterator; |
8
|
|
|
use Spatie\Crawler\CrawlInternalUrls; |
9
|
|
|
use Spatie\Export\Concerns\Messenger; |
10
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
11
|
|
|
|
12
|
|
|
class Exporter |
13
|
|
|
{ |
14
|
|
|
use Messenger; |
15
|
|
|
|
16
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
17
|
|
|
protected $filesystem; |
18
|
|
|
|
19
|
|
|
/** @var string[] */ |
20
|
|
|
protected $entries; |
21
|
|
|
|
22
|
|
|
/** @var string[] */ |
23
|
|
|
protected $include; |
24
|
|
|
|
25
|
|
|
/** @var string[] */ |
26
|
|
|
protected $exclude; |
27
|
|
|
|
28
|
|
|
/** @var \Spatie\Crawler\Crawler */ |
29
|
|
|
protected $crawler; |
30
|
|
|
|
31
|
|
|
public function __construct(Filesystem $filesystem) |
32
|
|
|
{ |
33
|
|
|
$this->filesystem = $filesystem; |
34
|
|
|
$this->crawler = (new Crawler(new InternalClient())); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Filesystem |
39
|
|
|
*/ |
40
|
|
|
public function getFilesystem(): Filesystem |
41
|
|
|
{ |
42
|
|
|
return $this->filesystem; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function entries(array $entries): Exporter |
46
|
|
|
{ |
47
|
|
|
$this->entries = array_map(function (string $entry) { |
48
|
|
|
return "http://localhost{$entry}"; |
49
|
|
|
}, $entries); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function include(array $include): Exporter |
55
|
|
|
{ |
56
|
|
|
$this->include = array_map(function ($include) { |
57
|
|
|
return is_array($include) |
58
|
|
|
? $include |
59
|
|
|
: ['source' => $include, 'target' => $include]; |
60
|
|
|
}, $include); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function exclude(array $exclude): Exporter |
66
|
|
|
{ |
67
|
|
|
$this->exclude = $exclude; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function export(): void |
73
|
|
|
{ |
74
|
|
|
$this->exportEntries(); |
75
|
|
|
|
76
|
|
|
$this->exportIncludedFiles(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function exportEntries(): void |
80
|
|
|
{ |
81
|
|
|
foreach ($this->entries as $entry) { |
82
|
|
|
$this->message("[{$entry}]"); |
83
|
|
|
|
84
|
|
|
$crawlObserver = new ExportCrawlObserver($this->filesystem, $entry); |
85
|
|
|
$crawlObserver->onMessage($this->onMessage); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->crawler |
88
|
|
|
->setCrawlObserver($crawlObserver) |
89
|
|
|
->setCrawlProfile(new CrawlInternalUrls($entry)) |
90
|
|
|
->startCrawling($entry); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function exportIncludedFiles(): void |
95
|
|
|
{ |
96
|
|
|
foreach ($this->include as ['source' => $source, 'target' => $target]) { |
97
|
|
|
$this->message("[{$source}]"); |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (is_file($source)) { |
100
|
|
|
$this->exportIncludedFile($source, $target); |
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
$this->exportIncludedDirectory($source, $target); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function exportIncludedFile(string $source, string $target): void |
108
|
|
|
{ |
109
|
|
|
if ($this->excludes($source)) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$target = '/'.ltrim($target, '/'); |
114
|
|
|
|
115
|
|
|
$this->message($target); |
116
|
|
|
|
117
|
|
|
$this->filesystem->put($target, file_get_contents($source)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function exportIncludedDirectory(string $source, string $target): void |
121
|
|
|
{ |
122
|
|
|
$iterator = new RecursiveIteratorIterator( |
123
|
|
|
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
124
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
foreach ($iterator as $item) { |
128
|
|
|
if ($item->isDir()) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->exportIncludedFile($item->getPathname(), $target.'/'.$iterator->getSubPathName()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function excludes(string $source): bool |
137
|
|
|
{ |
138
|
|
|
foreach ($this->exclude as $pattern) { |
139
|
|
|
if (preg_match($pattern, $source)) { |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.