1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Export; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
7
|
|
|
use App\Http\Kernel; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use RecursiveDirectoryIterator; |
10
|
|
|
use RecursiveIteratorIterator; |
11
|
|
|
use Spatie\Export\Concerns\Messenger; |
12
|
|
|
|
13
|
|
|
class Exporter |
14
|
|
|
{ |
15
|
|
|
use Messenger; |
16
|
|
|
|
17
|
|
|
/** @var \App\Http\Kernel */ |
18
|
|
|
protected $kernel; |
19
|
|
|
|
20
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
21
|
|
|
protected $filesystem; |
22
|
|
|
|
23
|
|
|
/** @var string[] */ |
24
|
|
|
protected $paths = []; |
25
|
|
|
|
26
|
|
|
/** @var string[] */ |
27
|
|
|
protected $include = []; |
28
|
|
|
|
29
|
|
|
/** @var string[] */ |
30
|
|
|
protected $exclude = []; |
31
|
|
|
|
32
|
|
|
/** @var \Illuminate\Console\Command */ |
33
|
|
|
protected $cli; |
34
|
|
|
|
35
|
|
|
public function __construct(Kernel $kernel, Filesystem $filesystem) |
36
|
|
|
{ |
37
|
|
|
$this->kernel = $kernel; |
38
|
|
|
$this->filesystem = $filesystem; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function paths(array $paths): Exporter |
42
|
|
|
{ |
43
|
|
|
$this->paths = array_merge($this->paths, $paths); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function include(array $include): Exporter |
49
|
|
|
{ |
50
|
|
|
$this->include = array_merge($this->include, $include); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function exclude(array $exclude): Exporter |
56
|
|
|
{ |
57
|
|
|
$this->exclude = array_merge($this->exclude, $exclude); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function export(): void |
63
|
|
|
{ |
64
|
|
|
$this->exportPaths(); |
65
|
|
|
|
66
|
|
|
$this->exportIncludedFiles(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function exportPaths(): void |
70
|
|
|
{ |
71
|
|
|
$this->cli->comment("Exporting paths..."); |
72
|
|
|
|
73
|
|
|
$progressBar = $this->cli->getOutput()->createProgressBar(count($this->paths)); |
74
|
|
|
|
75
|
|
|
$progressBar->start(); |
76
|
|
|
|
77
|
|
|
foreach ($this->paths as $path) { |
78
|
|
|
$response = $this->kernel->handle( |
79
|
|
|
Request::create($path, 'GET') |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$targetPath = '/'.ltrim($path . '/index.html', '/'); |
83
|
|
|
|
84
|
|
|
$progressBar->advance(); |
85
|
|
|
|
86
|
|
|
$contents = str_replace('http://localhost/', '/', $response->content()); |
87
|
|
|
$contents = str_replace('http://localhost', '/', $contents); |
88
|
|
|
|
89
|
|
|
$this->filesystem->put($targetPath, $contents); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$progressBar->finish(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function exportIncludedFiles(): void |
96
|
|
|
{ |
97
|
|
|
$this->cli->comment("\nExporting files..."); |
98
|
|
|
|
99
|
|
|
foreach ($this->include as ['source' => $source, 'target' => $target]) { |
100
|
|
|
if (is_file($source)) { |
101
|
|
|
$this->exportIncludedFile($source, $target); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$this->exportIncludedDirectory($source, $target); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function exportIncludedFile(string $source, string $target): void |
109
|
|
|
{ |
110
|
|
|
if ($this->excludes($source)) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$target = '/'.ltrim($target, '/'); |
115
|
|
|
|
116
|
|
|
$this->filesystem->put($target, file_get_contents($source)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function exportIncludedDirectory(string $source, string $target): void |
120
|
|
|
{ |
121
|
|
|
$iterator = new RecursiveIteratorIterator( |
122
|
|
|
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
123
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
foreach ($iterator as $item) { |
127
|
|
|
if ($item->isDir()) { |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->exportIncludedFile($item->getPathname(), $target.'/'.$iterator->getSubPathName()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function excludes(string $source): bool |
136
|
|
|
{ |
137
|
|
|
foreach ($this->exclude as $pattern) { |
138
|
|
|
if (preg_match($pattern, $source)) { |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setCli(Command $cli) |
147
|
|
|
{ |
148
|
|
|
$this->cli = $cli; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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..